-
-
Notifications
You must be signed in to change notification settings - Fork 85
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
Move filename validation into adaptor for AWS "directories". Add preserveFileName Support #76
Conversation
index.js
Outdated
@@ -2,6 +2,8 @@ | |||
// | |||
// Stores Parse files in AWS S3. | |||
|
|||
const FilesAdapter = require('parse-server/lib/Adapters/Files/FilesAdapter').FilesAdapter; | |||
const Parse = require('parse/lib/node/Parse').Parse; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I needed to bring in Parse to throw the right error codes.
Codecov Report
@@ Coverage Diff @@
## master #76 +/- ##
==========================================
+ Coverage 94.26% 94.54% +0.27%
==========================================
Files 2 2
Lines 157 165 +8
Branches 33 34 +1
==========================================
+ Hits 148 156 +8
Misses 9 9
Continue to review full report at Codecov.
|
index.js
Outdated
@@ -11,11 +13,12 @@ const awsCredentialsDeprecationNotice = function awsCredentialsDeprecationNotice | |||
'See: https://github.com/parse-server-modules/parse-server-s3-adapter#aws-credentials for details'); | |||
}; | |||
|
|||
class S3Adapter { | |||
class S3Adapter extends FilesAdapter { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seemed like the right thing to do, even though the parse-server PR doesn't actually change the signature.
spec/test.spec.js
Outdated
}, 100), | ||
deleteObject: (params, callback) => setTimeout(() => { | ||
const { Key } = params; | ||
function makeS3Adaptor(options) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code came in handy for more createFile requests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yomybaby Can you test this and provide feedback? I think this should solve your issue.
@dplewis Ok. I'll take a look at it tonight. |
README.md
Outdated
@@ -169,6 +176,8 @@ var s3Options = { | |||
baseUrl: process.env.SPACES_BASE_URL, | |||
region: process.env.SPACES_REGION, | |||
directAccess: true, | |||
preserveFilename: "always", | |||
fileNameCheck: "safe", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW: I'm only using these settings in my app. Happy to remove the others if they are at controversial.
Move filename validation out of the Router and into the FilesAdaptor(parse-community/parse-server#6157) is great idea. How about make this more customizable? like this
|
Thought about it, but use my case was fairly simple. If it's needed in the future, the validation code could simply check the type of the validate/preserveFilename parameter, and do the right thing. I wanted to preserve the same filename check behavior from the old code, while enabling the directory capability. Happy to remove the preserveFilename feature until someone needs it. I'm not using it. |
So don't know when I'll have time to get back to this. @yomybaby Any interest in implementing that on top of my branch? |
@mpatnode Sorry for the late reply. Adding parse module (not parse-server) for Parse.Error should be ok here as we use it in the parse-server-push-adapter https://github.com/parse-community/parse-server-push-adapter/blob/master/package.json#L39 |
Ignore previous comment. Will do! |
43738f4
to
1ec0b10
Compare
@@ -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); |
There was a problem hiding this comment.
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...
}, | ||
generateKey: (filename) => { | ||
return `${Date.now()}_${filename}`; // unique prefix for every filename | ||
} |
There was a problem hiding this comment.
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?
1ec0b10
to
de25224
Compare
de25224
to
e7f6cc3
Compare
README.md
Outdated
validateFilename: (filename) => { | ||
if (filename.length > 1024) { | ||
return new Parse.Error( | ||
Parse.Error.INVALID_FILE_NAME, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dplewis Hmm... this might be problematic?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No longer necessary: parse-community/parse-server#6246
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should throw new Parse.Error
instead of returning
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually ignore that last comment this is correct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dplewis This is happy now: parse-community/parse-server#6246 |
Sorry for mixing the two features here, but they were interdependent. The first was to implement file validation for AWS files. This validation method will be ignored until parse-community/parse-server#6157 is merged.
The end result here is that now you can specify an AWS "directory" path in your filename and it won't be rejected by a Router class that doesn't understand your filesystem. You can now supply methods for filename validation and AWS key generation, so you can decide what's a valid filename, and how you want to deal with duplicate filenames. Note the default behavior for the later needs to be disabled in parse-server:FilesController if you want to use directories in AWS.