Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Revise createFile logic to return modified filenames and location #242

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
31 changes: 24 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,23 @@ class S3Adapter {

// For a given config object, filename, and data, store a file in S3
// Returns a promise containing the S3 object creation response
async createFile(filename, data, contentType, options = {}) {
async createFile(filename, data, contentType, options = {}, config = {}) {

let key_without_prefix = filename;
if (this._generateKey instanceof Function) {
try {
key_without_prefix = this._generateKey(filename, contentType, options);
}catch(e){
throw new Error(e); // throw error if generateKey function fails
}
}

const params = {
Bucket: this._bucket,
Key: this._bucketPrefix + filename,
Key: this._bucketPrefix + key_without_prefix,
Body: data,
};

if (this._generateKey instanceof Function) {
params.Key = this._bucketPrefix + this._generateKey(filename);
}

if (this._fileAcl) {
if (this._fileAcl === 'none') {
delete params.ACL;
Expand Down Expand Up @@ -180,7 +187,17 @@ class S3Adapter {
const endpoint = this._endpoint || `https://${this._bucket}.s3.${this._region}.amazonaws.com`;
const location = `${endpoint}/${params.Key}`;

return Object.assign(response || {}, { Location: location });
let url;
if (Object.keys(config).length != 0) { // if config is passed, we can generate a presigned url here
url = await this.getFileLocation(config, key_without_prefix);
}

return {
location: location, // actual upload location, used for tests
name: key_without_prefix, // filename in storage, consistent with other adapters
s3_response: response, // raw s3 response
...url ? { url: url } : {} // url (optionally presigned) or non-direct access url
};
}

async deleteFile(filename) {
Expand Down
6 changes: 3 additions & 3 deletions lib/optionsFromArguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ const optionsFromArguments = function optionsFromArguments(args) {
} else if (args.length === 2) {
options.bucket = stringOrOptions;
if (typeof args[1] !== 'object') {
throw new Error("Failed to configure S3Adapter. Arguments don't make sense");
throw new Error('Failed to configure S3Adapter. Arguments don\'t make sense');
}
otherOptions = args[1];
} else if (args.length > 2) {
if (typeof args[1] !== 'string' || typeof args[2] !== 'string') {
throw new Error("Failed to configure S3Adapter. Arguments don't make sense");
throw new Error('Failed to configure S3Adapter. Arguments don\'t make sense');
}
options.accessKey = args[0];
options.secretKey = args[1];
Expand Down Expand Up @@ -81,7 +81,7 @@ const optionsFromArguments = function optionsFromArguments(args) {
options.bucket = s3overrides.params.Bucket;
}
} else if (args.length > 2) {
throw new Error("Failed to configure S3Adapter. Arguments don't make sense");
throw new Error('Failed to configure S3Adapter. Arguments don\'t make sense');
}

options = fromOptionsDictionaryOrDefault(options, 's3overrides', s3overrides);
Expand Down
6 changes: 3 additions & 3 deletions spec/test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ describe('S3Adapter tests', () => {
const s3 = getMockS3Adapter(options);
const fileName = 'randomFileName.txt';
const response = s3.createFile(fileName, 'hello world', 'text/utf8').then(value => {
const url = new URL(value.Location);
const url = new URL(value.location);
expect(url.pathname.indexOf(fileName) > 13).toBe(true);
});
promises.push(response);
Expand All @@ -707,7 +707,7 @@ describe('S3Adapter tests', () => {
const s3 = getMockS3Adapter(options);
const fileName = 'foo/randomFileName.txt';
const response = s3.createFile(fileName, 'hello world', 'text/utf8').then(value => {
const url = new URL(value.Location);
const url = new URL(value.location);
expect(url.pathname.substring(1)).toEqual(options.bucketPrefix + fileName);
});
promises.push(response);
Expand All @@ -717,7 +717,7 @@ describe('S3Adapter tests', () => {
const s3 = getMockS3Adapter(options);
const fileName = 'foo/randomFileName.txt';
const response = s3.createFile(fileName, 'hello world', 'text/utf8').then(value => {
const url = new URL(value.Location);
const url = new URL(value.location);
expect(url.pathname.indexOf('foo/')).toEqual(6);
expect(url.pathname.indexOf('random') > 13).toBe(true);
});
Expand Down