Skip to content

Commit

Permalink
feat(s3): add disa le store gey to uplaoder (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
pcholuj authored Oct 7, 2019
1 parent a9fa2f7 commit 2cf27fd
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 7 deletions.
6 changes: 2 additions & 4 deletions manual_tests/upload_new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import * as Path from 'path';
const createFile = (size = 44320) => Buffer.alloc(size);
// Sentry.init({ dsn: 'DSN' });

const fs = new Client('APIKEY');
const fs = new Client('APEkwxKMZTsWNIP0XQsv2z');
fs.on('upload.error', (e) => {
console.log('uploadError', e);
});
Expand All @@ -47,9 +47,7 @@ fs.on('upload.error', (e) => {
// });

try {
fs.upload(createFile(), {}, {
filename: 'HR-mary-oo',
}).then((res) => {
fs.upload('./upload.js').then((res) => {
console.info('Upload done!', res);
});
} catch (e) {
Expand Down
77 changes: 77 additions & 0 deletions src/lib/api/upload/uploaders/s3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,83 @@ describe('Api/Upload/Uploaders/S3', () => {
expect(mockComplete).toHaveBeenCalledWith(expect.objectContaining(testSecurity));

});

it('should respect disableStorageKey option', async () => {
const u = new S3Uploader({
disableStorageKey: true,
path: '/test/',
});

u.setUrl(testHost);
u.setApikey(testApikey);
u.addFile(getSmallTestFile());

const res = await u.execute();
expect(res[0].status).toEqual('test_status');

const storageKeyExpect = {
store: {
location: 's3',
path: '/test/test.txt',
},
};

expect(mockStart).toHaveBeenCalledWith(expect.objectContaining(storageKeyExpect));
expect(mockUpload).toHaveBeenCalledWith(expect.objectContaining(storageKeyExpect));
expect(mockComplete).toHaveBeenCalledWith(expect.objectContaining(storageKeyExpect));

});

it('should respect disableStorageKey option when path is missing', async () => {
const u = new S3Uploader({
disableStorageKey: true,
});

u.setUrl(testHost);
u.setApikey(testApikey);
u.addFile(getSmallTestFile());

const res = await u.execute();
expect(res[0].status).toEqual('test_status');

const storageKeyExpect = {
store: {
location: 's3',
path: '/test.txt',
},
};

expect(mockStart).toHaveBeenCalledWith(expect.objectContaining(storageKeyExpect));
expect(mockUpload).toHaveBeenCalledWith(expect.objectContaining(storageKeyExpect));
expect(mockComplete).toHaveBeenCalledWith(expect.objectContaining(storageKeyExpect));

});

it('should respect disableStorageKey option when path has missing /', async () => {
const u = new S3Uploader({
disableStorageKey: true,
path: '/test',
});

u.setUrl(testHost);
u.setApikey(testApikey);
u.addFile(getSmallTestFile());

const res = await u.execute();
expect(res[0].status).toEqual('test_status');

const storageKeyExpect = {
store: {
location: 's3',
path: '/test/test.txt',
},
};

expect(mockStart).toHaveBeenCalledWith(expect.objectContaining(storageKeyExpect));
expect(mockUpload).toHaveBeenCalledWith(expect.objectContaining(storageKeyExpect));
expect(mockComplete).toHaveBeenCalledWith(expect.objectContaining(storageKeyExpect));
});

});

describe('Upload modes', () => {
Expand Down
20 changes: 17 additions & 3 deletions src/lib/api/upload/uploaders/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,25 @@ export class S3Uploader extends UploaderAbstract {
* @returns
* @memberof S3Uploader
*/
private getStoreOptions() {
return {
private getStoreOptions(id: string): StoreUploadOptions {
let options = {
location: DEFAULT_STORE_LOCATION, // this parameter is required, if not set use default one
...this.storeOptions,
};

if (this.storeOptions.disableStorageKey) {
const payload = this.getPayloadById(id);

if (options.path && options.path.substr(-1) !== '/') {
options.path = `${options.path}/`;
}

options.path = `${options.path ? options.path : '/'}${payload.file.name}`;

delete options.disableStorageKey;
}

return options;
}

/**
Expand Down Expand Up @@ -217,7 +231,7 @@ export class S3Uploader extends UploaderAbstract {

return {
...filterObject(fields, requiredFields),
store: this.getStoreOptions(),
store: this.getStoreOptions(id),
};
}

Expand Down
1 change: 1 addition & 0 deletions src/lib/filelink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export interface StoreBaseParams {
container?: string;
region?: string;
access?: string;
disableStorageKey?: boolean;
}

/**
Expand Down

0 comments on commit 2cf27fd

Please sign in to comment.