Add the addFileNameHash option that allows users to remove the hash f… (#4915)

* Add the addFileNameHash option that allows users to remove the hash from file names

* Change option name to preserveFileName

* Revert changes to package-lock.json
This commit is contained in:
GabrielLomba
2018-07-27 10:04:06 -03:00
committed by Florent Vilmart
parent d9cf3ce476
commit 37e6459608
6 changed files with 56 additions and 7 deletions

View File

@@ -232,6 +232,7 @@ The client keys used with Parse are no longer necessary with Parse Server. If yo
#### Advanced options #### Advanced options
* `fileKey` - For migrated apps, this is necessary to provide access to files already hosted on Parse. * `fileKey` - For migrated apps, this is necessary to provide access to files already hosted on Parse.
* `preserveFileName` - Set to true to remove the unique hash added to the file names. Defaults to false.
* `allowClientClassCreation` - Set to false to disable client class creation. Defaults to true. * `allowClientClassCreation` - Set to false to disable client class creation. Defaults to true.
* `enableAnonymousUsers` - Set to false to disable anonymous users. Defaults to true. * `enableAnonymousUsers` - Set to false to disable anonymous users. Defaults to true.
* `auth` - Used to configure support for [3rd party authentication](http://docs.parseplatform.org/parse-server/guide/#oauth-and-3rd-party-authentication). * `auth` - Used to configure support for [3rd party authentication](http://docs.parseplatform.org/parse-server/guide/#oauth-and-3rd-party-authentication).

View File

@@ -62,4 +62,40 @@ describe("FilesController",() =>{
done(); 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 GridStoreAdapter('mongodb://localhost:27017/parse')
spyOn(gridStoreAdapter, 'createFile')
gridStoreAdapter.createFile.and.returnValue(Promise.resolve())
const fileName = 'randomFileName.pdf'
const regexEscapedFileName = fileName.replace(/\./g, "\\$&")
const filesController = new FilesController(gridStoreAdapter, null, { preserveFileName: false })
filesController.createFile(config, fileName)
expect(gridStoreAdapter.createFile).toHaveBeenCalledTimes(1)
expect(gridStoreAdapter.createFile.calls.mostRecent().args[0]).toMatch(`^.{32}_${regexEscapedFileName}$`)
done();
});
it("should not add a unique hash to the file name when the preserveFileName option is false", (done) => {
const config = Config.get(Parse.applicationId)
const gridStoreAdapter = new GridStoreAdapter('mongodb://localhost:27017/parse')
spyOn(gridStoreAdapter, 'createFile')
gridStoreAdapter.createFile.and.returnValue(Promise.resolve())
const fileName = 'randomFileName.pdf'
const filesController = new FilesController(gridStoreAdapter, null, { preserveFileName: true })
filesController.createFile(config, fileName)
expect(gridStoreAdapter.createFile).toHaveBeenCalledTimes(1)
expect(gridStoreAdapter.createFile.calls.mostRecent().args[0]).toEqual(fileName)
done();
});
}); });

View File

@@ -25,9 +25,11 @@ export class FilesController extends AdaptableController {
contentType = mime.getType(filename); contentType = mime.getType(filename);
} }
if (!this.options.preserveFileName) {
filename = randomHexString(32) + '_' + filename; filename = randomHexString(32) + '_' + filename;
}
var location = this.adapter.getFileLocation(config, filename); const location = this.adapter.getFileLocation(config, filename);
return this.adapter.createFile(filename, data, contentType).then(() => { return this.adapter.createFile(filename, data, contentType).then(() => {
return Promise.resolve({ return Promise.resolve({
url: location, url: location,

View File

@@ -82,6 +82,7 @@ export function getFilesController(options: ParseServerOptions): FilesController
databaseURI, databaseURI,
filesAdapter, filesAdapter,
databaseAdapter, databaseAdapter,
preserveFileName,
} = options; } = options;
if (!filesAdapter && databaseAdapter) { if (!filesAdapter && databaseAdapter) {
throw 'When using an explicit database adapter, you must also use an explicit filesAdapter.'; throw 'When using an explicit database adapter, you must also use an explicit filesAdapter.';
@@ -89,7 +90,7 @@ export function getFilesController(options: ParseServerOptions): FilesController
const filesControllerAdapter = loadAdapter(filesAdapter, () => { const filesControllerAdapter = loadAdapter(filesAdapter, () => {
return new GridStoreAdapter(databaseURI); return new GridStoreAdapter(databaseURI);
}); });
return new FilesController(filesControllerAdapter, appId); return new FilesController(filesControllerAdapter, appId, { preserveFileName });
} }
export function getUserController(options: ParseServerOptions): UserController { export function getUserController(options: ParseServerOptions): UserController {

View File

@@ -135,6 +135,12 @@ module.exports.ParseServerOptions = {
"env": "PARSE_SERVER_FILE_KEY", "env": "PARSE_SERVER_FILE_KEY",
"help": "Key for your files" "help": "Key for your files"
}, },
"preserveFileName": {
"env": "PARSE_SERVER_PRESERVE_FILE_NAME",
"help": "Enable (or disable) the addition of a unique hash to the file names",
"action": parsers.booleanParser,
"default": false
},
"userSensitiveFields": { "userSensitiveFields": {
"env": "PARSE_SERVER_USER_SENSITIVE_FIELDS", "env": "PARSE_SERVER_USER_SENSITIVE_FIELDS",
"help": "Personally identifiable information fields in the user table the should be removed for non-authorized users.", "help": "Personally identifiable information fields in the user table the should be removed for non-authorized users.",

View File

@@ -64,6 +64,9 @@ export interface ParseServerOptions {
webhookKey: ?string; webhookKey: ?string;
/* Key for your files */ /* Key for your files */
fileKey: ?string; fileKey: ?string;
/* Enable (or disable) the addition of a unique hash to the file names
:ENV: PARSE_SERVER_PRESERVE_FILE_NAME */
preserveFileName: ?boolean; // = false
/* Personally identifiable information fields in the user table the should be removed for non-authorized users. */ /* Personally identifiable information fields in the user table the should be removed for non-authorized users. */
userSensitiveFields: ?string[]; // = ["email"] userSensitiveFields: ?string[]; // = ["email"]
/* Enable (or disable) anon users, defaults to true /* Enable (or disable) anon users, defaults to true