Skip to content

Commit

Permalink
Merge pull request #51 from internxt/feature/get-download-links
Browse files Browse the repository at this point in the history
[_]: feature/get-download-links
  • Loading branch information
AlexMenor authored Mar 23, 2022
2 parents d1bfece + 408ab85 commit a0afa62
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@internxt/inxt-js",
"version": "1.4.0",
"version": "1.5.0",
"description": "",
"main": "build/index.js",
"types": "build/index.d.ts",
Expand Down
34 changes: 22 additions & 12 deletions src/cli/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { buildCommand } from './CommandInterface';
import { createBucket } from './create-bucket';
import { deleteBucket } from './delete-bucket';
import { downloadFile } from './download-file';
import { getDownloadLinks } from './get-download-links';
import { getFileInfo } from './get-filo-info';
import { renameFile } from './rename-file';
import { uploadFileOneShard, uploadFileMultipleShards } from './upload-file';
Expand All @@ -18,19 +19,19 @@ export const uploadFileCommand = buildCommand({
version: '0.0.1',
command: 'upload-file <path>',
description: 'Upload a file',
options: [{
flags: '-s, --shards [one|multiple]',
required: true
}],
options: [
{
flags: '-s, --shards [one|multiple]',
required: true,
},
],
}).action((path, opts) => {
if (opts.shards === 'multiple') {
return uploadFileMultipleShards(path, 1)
.finally(notifyProgramFinished('upload-file'));
}
return uploadFileMultipleShards(path, 1).finally(notifyProgramFinished('upload-file'));
}
if (opts.shards === 'one') {
return uploadFileOneShard(path)
.finally(notifyProgramFinished('upload-file'));
}
return uploadFileOneShard(path).finally(notifyProgramFinished('upload-file'));
}
});

export const uploadFileCommandParallel = buildCommand({
Expand Down Expand Up @@ -91,7 +92,7 @@ export const createBucketCommand = buildCommand({
version: '0.0.1',
command: 'create-bucket <bucketName>',
description: 'Creates a bucket with the given name',
options: []
options: [],
}).action((bucketName: string) => {
createBucket(bucketName).finally(notifyProgramFinished('create-bucket'));
});
Expand All @@ -100,11 +101,20 @@ export const deleteBucketCommand = buildCommand({
version: '0.0.1',
command: 'delete-bucket <bucketId>',
description: 'Deletes a bucket given its id',
options: []
options: [],
}).action((bucketId: string) => {
deleteBucket(bucketId).finally(notifyProgramFinished('delete-bucket'));
});

export const getDownloadLinksCommand = buildCommand({
version: '0.0.1',
command: 'get-download-links <bucketId> <fileIdsSeparatedByCommas>',
description: 'Gets download links of file ids',
options: [],
}).action((bucketId: string, fileIdsSeparatedByCommas: string) => {
getDownloadLinks(bucketId, fileIdsSeparatedByCommas.split(',')).finally(notifyProgramFinished('get-download-links'));
});

// export const downloadFolderZippedCommand = buildCommand({
// version: '0.0.1',
// name: 'download-folder-zip',
Expand Down
13 changes: 13 additions & 0 deletions src/cli/get-download-links.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { logger } from '../lib/utils/logger';
import { getEnvironment } from './CommandInterface';

export async function getDownloadLinks(bucketId: string, fileIds: string[]): Promise<void> {
const network = getEnvironment();

try {
const response = await network.getDownloadLinks(bucketId, fileIds);
logger.info(`getDownloadLinks response: ${JSON.stringify(response, null, 2)}`);
} catch (err) {
logger.error(`Something went wrong while getting download links ${err.message}`);
}
}
1 change: 1 addition & 0 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ program.addCommand(commands.renameFileCommand);
program.addCommand(commands.getFileInfoCommand);
program.addCommand(commands.createBucketCommand);
program.addCommand(commands.deleteBucketCommand);
program.addCommand(commands.getDownloadLinksCommand);
// program.addCommand(commands.downloadFolderZippedCommand);

program.parse(process.argv);
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { ActionState, ActionTypes, Bucket, EnvironmentConfig } from './api';
import { logger, Logger } from './lib/utils/logger';

import { FileInfo, GetFileInfo } from './api/fileinfo';
import { Bridge, CreateFileTokenResponse } from './services/api';
import { Bridge, CreateFileTokenResponse, GetDownloadLinksResponse } from './services/api';
import { HashStream } from './lib/utils/streams';

type GetBucketsCallback = (err: Error | null, result: any) => void;
Expand Down Expand Up @@ -273,4 +273,8 @@ export class Environment {
return new Bridge(this.config).renameFile(bucketId, fileId, newEncryptedName).start();
});
}

getDownloadLinks(bucketId: string, fileIds: string[]) {
return new Bridge(this.config).getDownloadLinks(bucketId, fileIds).start<GetDownloadLinksResponse>();
}
}
11 changes: 9 additions & 2 deletions src/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export interface CreateFileTokenResponse {
size: number;
token: string;
}
export type GetDownloadLinksResponse = { fileId: string; link: string; index: string }[];

export interface InxtApiI {
getBucketById(bucketId: string, params?: AxiosRequestConfig): INXTRequest;
Expand Down Expand Up @@ -289,12 +290,18 @@ export class Bridge extends InxtApi {
createBucket(bucketName: string) {
const targetUrl = `${this.config.bridgeUrl}/buckets`;

return new INXTRequest(this.config, Methods.Post, targetUrl, { data: { name: bucketName }});
};
return new INXTRequest(this.config, Methods.Post, targetUrl, { data: { name: bucketName } });
}

deleteBucket(bucketId: string) {
const targetUrl = `${this.config.bridgeUrl}/buckets/${bucketId}`;

return new INXTRequest(this.config, Methods.Delete, targetUrl, {});
}

getDownloadLinks(bucketId: string, fileIds: string[]) {
const targetUrl = `${this.config.bridgeUrl}/buckets/${bucketId}/bulk-files?fileIds=${fileIds.join(',')}`;

return new INXTRequest(this.config, Methods.Get, targetUrl, {});
}
}

0 comments on commit a0afa62

Please sign in to comment.