Skip to content

Commit 93427da

Browse files
authored
Update index.js (#106)
* Update index.js * Make baseUrl parameter function make basUrl parameter fnction * Change instanceof to typeof * Update index.js fix-lint * Update index.js fix lint * Update index.js fix lint * Update index.js fix lint * Update test.spec.js add test cases for function baseUrl * Update test.spec.js * Update test.spec.js * Update test.spec.js * Update test.spec.js
1 parent 8149f44 commit 93427da

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,16 @@ class S3Adapter {
159159
getFileLocation(config, filename) {
160160
const fileName = filename.split('/').map(encodeURIComponent).join('/');
161161
if (this._directAccess) {
162-
if (this._baseUrl && this._baseUrlDirect) {
163-
return `${this._baseUrl}/${fileName}`;
164-
} if (this._baseUrl) {
162+
if (this._baseUrl) {
163+
if (typeof this._baseUrl === 'function') {
164+
if (this._baseUrlDirect) {
165+
return `${this._baseUrl(config, filename)}/${fileName}`;
166+
}
167+
return `${this._baseUrl(config, filename)}/${this._bucketPrefix + fileName}`;
168+
}
169+
if (this._baseUrlDirect) {
170+
return `${this._baseUrl}/${fileName}`;
171+
}
165172
return `${this._baseUrl}/${this._bucketPrefix + fileName}`;
166173
}
167174
return `https://${this._bucket}.s3.amazonaws.com/${this._bucketPrefix + fileName}`;

spec/test.spec.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,49 @@ describe('S3Adapter tests', () => {
353353
expect(s3.getFileLocation(testConfig, 'test.png')).toEqual('https://myBucket.s3.amazonaws.com/foo/bar/test.png');
354354
});
355355
});
356+
describe('getFileLocation', () => {
357+
const testConfig = {
358+
mount: 'http://my.server.com/parse',
359+
applicationId: 'xxxx',
360+
};
361+
let options;
362+
363+
beforeEach(() => {
364+
options = {
365+
directAccess: true,
366+
bucketPrefix: 'foo/bar/',
367+
baseUrl: (fileconfig, filename) => {
368+
if (filename.length > 12) {
369+
return 'http://example.com/files';
370+
}
371+
return 'http://example.com/files';
372+
},
373+
};
374+
});
375+
376+
it('should get using the baseUrl', () => {
377+
const s3 = new S3Adapter('accessKey', 'secretKey', 'myBucket', options);
378+
expect(s3.getFileLocation(testConfig, 'test.png')).toEqual('http://example.com/files/foo/bar/test.png');
379+
});
380+
381+
it('should get direct to baseUrl', () => {
382+
options.baseUrlDirect = true;
383+
const s3 = new S3Adapter('accessKey', 'secretKey', 'myBucket', options);
384+
expect(s3.getFileLocation(testConfig, 'test.png')).toEqual('http://example.com/files/test.png');
385+
});
386+
387+
it('should get without directAccess', () => {
388+
options.directAccess = false;
389+
const s3 = new S3Adapter('accessKey', 'secretKey', 'myBucket', options);
390+
expect(s3.getFileLocation(testConfig, 'test.png')).toEqual('http://my.server.com/parse/files/xxxx/test.png');
391+
});
392+
393+
it('should go directly to amazon', () => {
394+
delete options.baseUrl;
395+
const s3 = new S3Adapter('accessKey', 'secretKey', 'myBucket', options);
396+
expect(s3.getFileLocation(testConfig, 'test.png')).toEqual('https://myBucket.s3.amazonaws.com/foo/bar/test.png');
397+
});
398+
});
356399

357400
describe('validateFilename', () => {
358401
let options;

0 commit comments

Comments
 (0)