Skip to content

Allow validateFilename to return a string or Parse Error #6246

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 1 commit into from
Dec 3, 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
17 changes: 16 additions & 1 deletion spec/FilesController.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ const mockAdapter = {
deleteFile: () => {},
getFileData: () => {},
getFileLocation: () => 'xyz',
validateFilename: () => {},
validateFilename: () => {
return null;
},
};

// Small additional tests to improve overall coverage
Expand Down Expand Up @@ -77,6 +79,19 @@ describe('FilesController', () => {
});
});

it('should create a parse error when a string is returned', done => {
const mock2 = mockAdapter;
mock2.validateFilename = () => {
return 'Bad file! No biscuit!';
};
const filesController = new FilesController(mockAdapter);
const error = filesController.validateFilename();
expect(typeof error).toBe('object');
expect(error.message.indexOf('biscuit')).toBe(13);
expect(error.code).toBe(Parse.Error.INVALID_FILE_NAME);
done();
});

it('should add a unique hash to the file name when the preserveFileName option is false', done => {
const config = Config.get(Parse.applicationId);
const gridStoreAdapter = new GridFSBucketAdapter(
Expand Down
7 changes: 6 additions & 1 deletion src/Controllers/FilesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import AdaptableController from './AdaptableController';
import { validateFilename, FilesAdapter } from '../Adapters/Files/FilesAdapter';
import path from 'path';
import mime from 'mime';
const Parse = require('parse').Parse;

const legacyFilesRegex = new RegExp(
'^[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}-.*'
Expand Down Expand Up @@ -98,7 +99,11 @@ export class FilesController extends AdaptableController {

validateFilename(filename) {
if (typeof this.adapter.validateFilename === 'function') {
return this.adapter.validateFilename(filename);
const error = this.adapter.validateFilename(filename);
if (typeof error !== 'string') {
return error;
}
return new Parse.Error(Parse.Error.INVALID_FILE_NAME, error);
}
return validateFilename(filename);
}
Expand Down