Skip to content

Commit

Permalink
Update index.js (#106)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
uzaysan authored Oct 6, 2020
1 parent 8149f44 commit 93427da
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
13 changes: 10 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,16 @@ class S3Adapter {
getFileLocation(config, filename) {
const fileName = filename.split('/').map(encodeURIComponent).join('/');
if (this._directAccess) {
if (this._baseUrl && this._baseUrlDirect) {
return `${this._baseUrl}/${fileName}`;
} if (this._baseUrl) {
if (this._baseUrl) {
if (typeof this._baseUrl === 'function') {
if (this._baseUrlDirect) {
return `${this._baseUrl(config, filename)}/${fileName}`;
}
return `${this._baseUrl(config, filename)}/${this._bucketPrefix + fileName}`;
}
if (this._baseUrlDirect) {
return `${this._baseUrl}/${fileName}`;
}
return `${this._baseUrl}/${this._bucketPrefix + fileName}`;
}
return `https://${this._bucket}.s3.amazonaws.com/${this._bucketPrefix + fileName}`;
Expand Down
43 changes: 43 additions & 0 deletions spec/test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,49 @@ describe('S3Adapter tests', () => {
expect(s3.getFileLocation(testConfig, 'test.png')).toEqual('https://myBucket.s3.amazonaws.com/foo/bar/test.png');
});
});
describe('getFileLocation', () => {
const testConfig = {
mount: 'http://my.server.com/parse',
applicationId: 'xxxx',
};
let options;

beforeEach(() => {
options = {
directAccess: true,
bucketPrefix: 'foo/bar/',
baseUrl: (fileconfig, filename) => {
if (filename.length > 12) {
return 'http://example.com/files';
}
return 'http://example.com/files';
},
};
});

it('should get using the baseUrl', () => {
const s3 = new S3Adapter('accessKey', 'secretKey', 'myBucket', options);
expect(s3.getFileLocation(testConfig, 'test.png')).toEqual('http://example.com/files/foo/bar/test.png');
});

it('should get direct to baseUrl', () => {
options.baseUrlDirect = true;
const s3 = new S3Adapter('accessKey', 'secretKey', 'myBucket', options);
expect(s3.getFileLocation(testConfig, 'test.png')).toEqual('http://example.com/files/test.png');
});

it('should get without directAccess', () => {
options.directAccess = false;
const s3 = new S3Adapter('accessKey', 'secretKey', 'myBucket', options);
expect(s3.getFileLocation(testConfig, 'test.png')).toEqual('http://my.server.com/parse/files/xxxx/test.png');
});

it('should go directly to amazon', () => {
delete options.baseUrl;
const s3 = new S3Adapter('accessKey', 'secretKey', 'myBucket', options);
expect(s3.getFileLocation(testConfig, 'test.png')).toEqual('https://myBucket.s3.amazonaws.com/foo/bar/test.png');
});
});

describe('validateFilename', () => {
let options;
Expand Down

0 comments on commit 93427da

Please sign in to comment.