Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 9 additions & 28 deletions src/cli/Commands.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Command } from 'commander';
import { logger } from '../lib/utils/logger';
import { buildCommand } from './CommandInterface';
import { createBucket } from './create-bucket';
Expand All @@ -7,16 +8,14 @@ import { getDownloadLinks } from './get-download-links';
import { getFileInfo } from './get-filo-info';
import { renameFile } from './rename-file';
import { uploadFile } from './upload-file';
import { uploadFileMultipart } from './upload-file-multipart';
import { uploadFolder } from './upload-folder-zip';

function notifyProgramFinished(programName: string) {
return () => {
logger.info('Program "%s" finished', programName);
};
}

export const uploadFileCommand = buildCommand({
export const uploadFileCommand: Command = buildCommand({
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was not inferring the type successfully.

version: '0.0.1',
command: 'upload-file <path>',
description: 'Upload a file',
Expand All @@ -25,25 +24,7 @@ export const uploadFileCommand = buildCommand({
return uploadFile(path);
});

export const uploadFileMultipartCommand = buildCommand({
version: '0.0.1',
command: 'upload-file-multipart <path>',
description: 'Upload a file using multipart',
options: [],
}).action((path) => {
return uploadFileMultipart(path);
});

export const uploadFolderZipCommand = buildCommand({
version: '0.0.1',
command: 'upload-folder-zip <path>',
description: 'Upload a folder zipped',
options: [],
}).action((path: string) => {
uploadFolder(path).finally(notifyProgramFinished('upload-folder-zip'));
});

export const downloadFileCommand = buildCommand({
export const downloadFileCommand: Command = buildCommand({
version: '0.0.1',
command: 'download-file <fileId> <path>',
description: 'Download a file',
Expand All @@ -52,7 +33,7 @@ export const downloadFileCommand = buildCommand({
downloadFile(fileId, path, 1).finally(notifyProgramFinished('download-file'));
});

export const downloadFileCommandParallel = buildCommand({
export const downloadFileCommandParallel: Command = buildCommand({
version: '0.0.1',
command: 'download-file-parallel <fileId> <path>',
description: 'Download a file',
Expand All @@ -61,7 +42,7 @@ export const downloadFileCommandParallel = buildCommand({
downloadFile(fileId, path, 10).finally(notifyProgramFinished('download-file-parallel'));
});

export const renameFileCommand = buildCommand({
export const renameFileCommand: Command = buildCommand({
version: '0.0.1',
command: 'rename-file <fileId> <newName>',
description: 'Renames a file in the network',
Expand All @@ -70,7 +51,7 @@ export const renameFileCommand = buildCommand({
renameFile(fileId, newName).finally(notifyProgramFinished('rename-file'));
});

export const getFileInfoCommand = buildCommand({
export const getFileInfoCommand: Command = buildCommand({
version: '0.0.1',
command: 'get-file-info <fileId>',
description: 'Gets file info',
Expand All @@ -79,7 +60,7 @@ export const getFileInfoCommand = buildCommand({
getFileInfo(fileId).finally(notifyProgramFinished('get-file-info'));
});

export const createBucketCommand = buildCommand({
export const createBucketCommand: Command = buildCommand({
version: '0.0.1',
command: 'create-bucket <bucketName>',
description: 'Creates a bucket with the given name',
Expand All @@ -88,7 +69,7 @@ export const createBucketCommand = buildCommand({
createBucket(bucketName).finally(notifyProgramFinished('create-bucket'));
});

export const deleteBucketCommand = buildCommand({
export const deleteBucketCommand: Command = buildCommand({
version: '0.0.1',
command: 'delete-bucket <bucketId>',
description: 'Deletes a bucket given its id',
Expand All @@ -97,7 +78,7 @@ export const deleteBucketCommand = buildCommand({
deleteBucket(bucketId).finally(notifyProgramFinished('delete-bucket'));
});

export const getDownloadLinksCommand = buildCommand({
export const getDownloadLinksCommand: Command = buildCommand({
version: '0.0.1',
command: 'get-download-links <bucketId> <fileIdsSeparatedByCommas>',
description: 'Gets download links of file ids',
Expand Down
2 changes: 0 additions & 2 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import * as commands from './Commands';
const program = new Command();

program.addCommand(commands.uploadFileCommand);
program.addCommand(commands.uploadFileMultipartCommand);
program.addCommand(commands.uploadFolderZipCommand);
program.addCommand(commands.downloadFileCommand);
program.addCommand(commands.downloadFileCommandParallel);
program.addCommand(commands.renameFileCommand);
Expand Down
44 changes: 0 additions & 44 deletions src/cli/upload-file-multipart.ts

This file was deleted.

33 changes: 12 additions & 21 deletions src/cli/upload-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,21 @@ export async function uploadFile(filepath: string) {

try {
const network = getEnvironment();

const bucketId = EnvService.instance.get('BUCKET_ID');
const abortController = new AbortController();

const fileId = await new Promise((resolve: (fileId: string) => void, reject) => {
const state = network.upload(bucketId, {
progressCallback: (progress: number) => {
logger.info('Progress %s%', (progress * 100).toFixed(2));
},
finishedCallback: (err: Error | null, res: string | null) => {
if (err) {
return reject(err);
} else if (!res) {
return reject('No response received from Network download');
}
resolve(res);
},
fileSize: statSync(filepath).size,
source: createReadStream(filepath),
});
process.on('SIGINT', () => {
logger.info('Aborting upload');
abortController.abort();
});

process.on('SIGINT', () => {
logger.info('Aborting upload');
network.uploadCancel(state);
});
const fileId = await network.upload(bucketId, {
progressCallback: (progress: number) => {
logger.info('Progress %s%', (progress * 100).toFixed(2));
},
fileSize: statSync(filepath).size,
source: createReadStream(filepath),
abortSignal: abortController.signal,
});

console.log('File %s uploaded', fileId);
Expand Down
125 changes: 0 additions & 125 deletions src/cli/upload-folder-zip.ts

This file was deleted.

6 changes: 4 additions & 2 deletions tests/services/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ import { expect } from 'chai';
import { generateShardMeta } from '../mocks';
import { Bridge } from '../../src/services/api';
import { Methods } from '../../src/lib';
import { AppDetails } from '@internxt/sdk/dist/shared';

const bridgeUrl = 'https://api.internxt.com';
const bridgePass = 'bridgePass';
const bridgeUser = 'fake@user.com';
const bridge = new Bridge({ bridgePass, bridgeUser, bridgeUrl });
const appDetails: AppDetails = { clientName: 'clientName', clientVersion: '1.0.0' };
const bridge = new Bridge({ bridgePass, bridgeUser, bridgeUrl, appDetails });

describe('services/api.ts', () => {
describe('Bridge', () => {
describe('# constructor()', () => {
it('Should throw if bridge url is empty', () => {
expect(() => {
new Bridge({ bridgeUser: 'fake@user.com', bridgePass: 'fakePass', bridgeUrl: '' });
new Bridge({ bridgeUser: 'fake@user.com', bridgePass: 'fakePass', bridgeUrl: '', appDetails });
}).to.throw('Empty bridge url');
});
});
Expand Down
Loading