Move filename validation out of the Router and into the FilesAdaptor (#6157)

* Move filename validation out of the Router and into the FilesAdaptor

* Address PR comments

* Update unittests to handle FilesAdapter interface change

* Make validateFilename optional
This commit is contained in:
Mike Patnode
2019-10-26 19:15:21 -07:00
committed by Diamond Lewis
parent 93fe6b44e4
commit 1c8d4a6519
7 changed files with 93 additions and 22 deletions

View File

@@ -69,6 +69,11 @@ export class FilesRouter {
}
createHandler(req, res, next) {
const config = req.config;
const filesController = config.filesController;
const filename = req.params.filename;
const contentType = req.get('Content-type');
if (!req.body || !req.body.length) {
next(
new Parse.Error(Parse.Error.FILE_SAVE_ERROR, 'Invalid file upload.')
@@ -76,28 +81,12 @@ export class FilesRouter {
return;
}
if (req.params.filename.length > 128) {
next(
new Parse.Error(Parse.Error.INVALID_FILE_NAME, 'Filename too long.')
);
const error = filesController.validateFilename(filename);
if (error) {
next(error);
return;
}
if (!req.params.filename.match(/^[_a-zA-Z0-9][a-zA-Z0-9@\.\ ~_-]*$/)) {
next(
new Parse.Error(
Parse.Error.INVALID_FILE_NAME,
'Filename contains invalid characters.'
)
);
return;
}
const filename = req.params.filename;
const contentType = req.get('Content-type');
const config = req.config;
const filesController = config.filesController;
filesController
.createFile(config, filename, req.body, contentType)
.then(result => {