Skip to content

Commit a7cb381

Browse files
Mike Patnodedplewis
Mike Patnode
authored andcommitted
Allow validateFilename to return a string or Parse Error (#6246)
1 parent b75a73c commit a7cb381

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

spec/FilesController.spec.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ const mockAdapter = {
1616
deleteFile: () => {},
1717
getFileData: () => {},
1818
getFileLocation: () => 'xyz',
19-
validateFilename: () => {},
19+
validateFilename: () => {
20+
return null;
21+
},
2022
};
2123

2224
// Small additional tests to improve overall coverage
@@ -77,6 +79,19 @@ describe('FilesController', () => {
7779
});
7880
});
7981

82+
it('should create a parse error when a string is returned', done => {
83+
const mock2 = mockAdapter;
84+
mock2.validateFilename = () => {
85+
return 'Bad file! No biscuit!';
86+
};
87+
const filesController = new FilesController(mockAdapter);
88+
const error = filesController.validateFilename();
89+
expect(typeof error).toBe('object');
90+
expect(error.message.indexOf('biscuit')).toBe(13);
91+
expect(error.code).toBe(Parse.Error.INVALID_FILE_NAME);
92+
done();
93+
});
94+
8095
it('should add a unique hash to the file name when the preserveFileName option is false', done => {
8196
const config = Config.get(Parse.applicationId);
8297
const gridStoreAdapter = new GridFSBucketAdapter(

src/Controllers/FilesController.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import AdaptableController from './AdaptableController';
44
import { validateFilename, FilesAdapter } from '../Adapters/Files/FilesAdapter';
55
import path from 'path';
66
import mime from 'mime';
7+
const Parse = require('parse').Parse;
78

89
const legacyFilesRegex = new RegExp(
910
'^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}-.*'
@@ -98,7 +99,11 @@ export class FilesController extends AdaptableController {
9899

99100
validateFilename(filename) {
100101
if (typeof this.adapter.validateFilename === 'function') {
101-
return this.adapter.validateFilename(filename);
102+
const error = this.adapter.validateFilename(filename);
103+
if (typeof error !== 'string') {
104+
return error;
105+
}
106+
return new Parse.Error(Parse.Error.INVALID_FILE_NAME, error);
102107
}
103108
return validateFilename(filename);
104109
}

0 commit comments

Comments
 (0)