Add file triggers and file meta data (#6344)

* added hint to aggregate

* added support for hint in query

* added else clause to aggregate

* fixed tests

* updated tests

* Add tests and clean up

* added beforeSaveFile and afterSaveFile triggers

* Add support for explain

* added some validation

* added support for metadata and tags

* tests?

* trying tests

* added tests

* fixed failing tests

* added some docs for fileObject

* updated hooks to use Parse.File

* added test for already saved file being returned in hook

* added beforeDeleteFile and afterDeleteFile hooks

* removed contentLength because it's already in the header

* added fileSize param to FileTriggerRequest

* added support for client side metadata and tags

* removed fit test

* removed unused import

* added loging to file triggers

* updated error message

* updated error message

* fixed tests

* fixed typos

* Update package.json

* fixed failing test

* fixed error message

* fixed failing tests (hopefully)

* TESTS!!!

* Update FilesAdapter.js

fixed comment

* added test for changing file name

* updated comments

Co-authored-by: Diamond Lewis <findlewis@gmail.com>
This commit is contained in:
stevestencil
2020-04-02 17:00:15 -04:00
committed by GitHub
parent d48de7d97a
commit a9dba442b1
9 changed files with 604 additions and 43 deletions

View File

@@ -12,8 +12,14 @@ export const Types = {
afterDelete: 'afterDelete',
beforeFind: 'beforeFind',
afterFind: 'afterFind',
beforeSaveFile: 'beforeSaveFile',
afterSaveFile: 'afterSaveFile',
beforeDeleteFile: 'beforeDeleteFile',
afterDeleteFile: 'afterDeleteFile',
};
const FileClassName = '@File';
const baseStore = function() {
const Validators = {};
const Functions = {};
@@ -122,6 +128,10 @@ export function addTrigger(type, className, handler, applicationId) {
add(Category.Triggers, `${type}.${className}`, handler, applicationId);
}
export function addFileTrigger(type, handler, applicationId) {
add(Category.Triggers, `${type}.${FileClassName}`, handler, applicationId);
}
export function addLiveQueryEventHandler(handler, applicationId) {
applicationId = applicationId || Parse.applicationId;
_triggerStore[applicationId] = _triggerStore[applicationId] || baseStore();
@@ -147,6 +157,10 @@ export function getTrigger(className, triggerType, applicationId) {
return get(Category.Triggers, `${triggerType}.${className}`, applicationId);
}
export function getFileTrigger(type, applicationId) {
return getTrigger(FileClassName, type, applicationId);
}
export function triggerExists(
className: string,
type: string,
@@ -672,3 +686,61 @@ export function runLiveQueryEventHandlers(
}
_triggerStore[applicationId].LiveQuery.forEach(handler => handler(data));
}
export function getRequestFileObject(triggerType, auth, fileObject, config) {
const request = {
...fileObject,
triggerName: triggerType,
master: false,
log: config.loggerController,
headers: config.headers,
ip: config.ip,
};
if (!auth) {
return request;
}
if (auth.isMaster) {
request['master'] = true;
}
if (auth.user) {
request['user'] = auth.user;
}
if (auth.installationId) {
request['installationId'] = auth.installationId;
}
return request;
}
export async function maybeRunFileTrigger(triggerType, fileObject, config, auth) {
const fileTrigger = getFileTrigger(triggerType, config.applicationId);
if (typeof fileTrigger === 'function') {
try {
const request = getRequestFileObject(
triggerType,
auth,
fileObject,
config
);
const result = await fileTrigger(request);
logTriggerSuccessBeforeHook(
triggerType,
'Parse.File',
{ ...fileObject.file.toJSON(), fileSize: fileObject.fileSize },
result,
auth,
)
return result || fileObject;
} catch (error) {
logTriggerErrorBeforeHook(
triggerType,
'Parse.File',
{ ...fileObject.file.toJSON(), fileSize: fileObject.fileSize },
auth,
error,
);
throw error;
}
}
return fileObject;
}