Skip to content

Move filename validation into adaptor for AWS "directories". Add preserveFileName Support #76

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

Merged
merged 10 commits into from
Dec 11, 2019
Merged
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
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,14 @@ The preferred method is to use the default AWS credentials pattern. If no AWS c
"baseUrlDirect": false, // default value
"signatureVersion": 'v4', // default value
"globalCacheControl": null, // default value. Or 'public, max-age=86400' for 24 hrs Cache-Control
"ServerSideEncryption": 'AES256|aws:kms' //AES256 or aws:kms, or if you do not pass this, encryption won't be done
"ServerSideEncryption": 'AES256|aws:kms', //AES256 or aws:kms, or if you do not pass this, encryption won't be done
"validateFilename": null, // Default to parse-server FilesAdapter::validateFilename.
"generateKey": null // Will default to Parse.FilesController.preserveFileName
}
}
}
```
***Note*** By default Parse.FilesController.preserveFileName will prefix all filenames with a random hex code. You will want to disable that if you enable it here or wish to use S3 "directories".

### using environment variables

Expand Down Expand Up @@ -109,7 +112,16 @@ var s3Adapter = new S3Adapter('accessKey',
directAccess: false,
baseUrl: 'http://images.example.com',
signatureVersion: 'v4',
globalCacheControl: 'public, max-age=86400' // 24 hrs Cache-Control.
globalCacheControl: 'public, max-age=86400', // 24 hrs Cache-Control.
validateFilename: (filename) => {
if (filename.length > 1024) {
return 'Filename too long.';
}
return null; // Return null on success
},
generateKey: (filename) => {
return `${Date.now()}_${filename}`; // unique prefix for every filename
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yomybaby This look good to you?

});

var api = new ParseServer({
Expand Down Expand Up @@ -145,7 +157,9 @@ var s3Options = {
"directAccess": false, // default value
"baseUrl": null // default value
"signatureVersion": 'v4', // default value
"globalCacheControl": null // default value. Or 'public, max-age=86400' for 24 hrs Cache-Control
"globalCacheControl": null, // default value. Or 'public, max-age=86400' for 24 hrs Cache-Control
"validateFilename": () => null, // Anything goes!
"generateKey": (filename) => filename, // Ensure Parse.FilesController.preserveFileName is true!
}

var s3Adapter = new S3Adapter(s3Options);
Expand Down
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class S3Adapter {
this._signatureVersion = options.signatureVersion;
this._globalCacheControl = options.globalCacheControl;
this._encryption = options.ServerSideEncryption;
this._generateKey = options.generateKey;
// Optional FilesAdaptor method
this.validateFilename = options.validateFilename;

const s3Options = {
params: { Bucket: this._bucket },
Expand Down Expand Up @@ -68,6 +71,11 @@ class S3Adapter {
Key: this._bucketPrefix + filename,
Body: data,
};

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

if (this._directAccess) {
params.ACL = 'public-read';
}
Expand Down Expand Up @@ -126,7 +134,7 @@ class S3Adapter {
// The location is the direct S3 link if the option is set,
// otherwise we serve the file through parse-server
getFileLocation(config, filename) {
const fileName = encodeURIComponent(filename);
const fileName = filename.split('/').map(encodeURIComponent).join('/');
if (this._directAccess) {
if (this._baseUrl && this._baseUrlDirect) {
return `${this._baseUrl}/${fileName}`;
Expand Down
4 changes: 4 additions & 0 deletions lib/optionsFromArguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ const optionsFromArguments = function optionsFromArguments(args) {
options.signatureVersion = otherOptions.signatureVersion;
options.globalCacheControl = otherOptions.globalCacheControl;
options.ServerSideEncryption = otherOptions.ServerSideEncryption;
options.generateKey = otherOptions.generateKey;
options.validateFilename = otherOptions.validateFilename;
s3overrides = otherOptions.s3overrides;
}
} else if (args.length === 1) {
Expand All @@ -90,6 +92,8 @@ const optionsFromArguments = function optionsFromArguments(args) {
options = fromEnvironmentOrDefault(options, 'baseUrlDirect', 'S3_BASE_URL_DIRECT', false);
options = fromEnvironmentOrDefault(options, 'signatureVersion', 'S3_SIGNATURE_VERSION', 'v4');
options = fromEnvironmentOrDefault(options, 'globalCacheControl', 'S3_GLOBAL_CACHE_CONTROL', null);
options = fromOptionsDictionaryOrDefault(options, 'generateKey', null);
options = fromOptionsDictionaryOrDefault(options, 'validateFilename', null);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JS code in env vars didn't seem like a good idea to me...


return options;
};
Expand Down
66 changes: 65 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@parse/s3-files-adapter",
"version": "1.3.0",
"version": "1.4.0",
"description": "AWS S3 adapter for parse-server",
"main": "index.js",
"scripts": {
Expand All @@ -25,7 +25,8 @@
},
"homepage": "https://github.com/parse-community/parse-server-s3-adapter#readme",
"dependencies": {
"aws-sdk": "^2.59.0"
"aws-sdk": "^2.59.0",
"parse": "^2.9.1"
},
"devDependencies": {
"codecov": "^3.0.0",
Expand Down
Loading