You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
With the `beforeSaveFile` method you can run custom Cloud Code before any file is saved. Returning a new `Parse.File` will save the new file instead of the one sent by the client.
484
+
With the `beforeSave` method you can run custom Cloud Code before any file is saved. Returning a new `Parse.File` will save the new file instead of the one sent by the client.
Adding Metadata and Tags to your files allows you to add additional bits of data to the files that are stored within your storage solution (i.e AWS S3). The `beforeSaveFile` hook is a great place to set the metadata and/or tags on your files.
513
+
Adding Metadata and Tags to your files allows you to add additional bits of data to the files that are stored within your storage solution (i.e AWS S3). The `beforeSave` hook is a great place to set the metadata and/or tags on your files.
514
514
515
515
Note: not all storage adapters support metadata and tags. Check the documentation for the storage adapter you're using for compatibility.
516
516
517
517
```javascript
518
518
// Adding metadata and tags
519
-
Parse.Cloud.beforeSaveFile((request) => {
519
+
Parse.Cloud.beforeSave(Parse.File, (request) => {
520
520
const { file, user } = request;
521
521
file.addMetadata('createdById', user.id);
522
522
file.addTag('groupId', user.get('groupId'));
523
523
});
524
524
```
525
525
526
-
## afterSaveFile
526
+
## afterSave
527
527
528
-
The `afterSaveFile` method is a great way to keep track of all of the files stored in your app. For example:
528
+
The `afterSave` method is a great way to keep track of all of the files stored in your app. For example:
You can run custom Cloud Code before any file gets deleted. For example, lets say you want to add logic that only allows files to be deleted by the user who created it. You could use a combination of the `afterSaveFile` and the `beforeDeleteFile` methods as follows:
544
+
You can run custom Cloud Code before any file gets deleted. For example, lets say you want to add logic that only allows files to be deleted by the user who created it. You could use a combination of the `afterSave` and the `beforeDelete` methods as follows:
In the above `beforeDeleteFile` example the `FileObject` collection is used to keep track of saved files in your app. The `afterDeleteFile` trigger is a good place to clean up these objects once a file has been successfully deleted.
568
+
In the above `beforeDelete` example the `FileObject` collection is used to keep track of saved files in your app. The `afterDelete` trigger is a good place to clean up these objects once a file has been successfully deleted.
*Available only on parse-server cloud code starting 8.1.0*
583
+
584
+
The beforeFind trigger allows you to intercept file retrieval requests and perform logic before the file is served. This is useful for adding access control, logging, or modifying the file's response behavior.
585
+
586
+
```javascript
587
+
Parse.Cloud.beforeFind(Parse.File, (request) => {
588
+
const { file, user, log } = request;
589
+
log.info(`User ${user.id} is trying to access file ${file.name()}`);
590
+
if (!user ||!user.get('isAdmin')) {
591
+
throw'Access denied: only admins can view this file';
592
+
}
593
+
});
594
+
```
595
+
596
+
## afterFind
597
+
598
+
*Available only on parse-server cloud code starting 8.1.0*
599
+
600
+
The afterFind trigger allows you to modify the behavior of a file download after it has been retrieved. For example, you can enforce a forced download of the file or log usage statistics.
601
+
602
+
```javascript
603
+
Parse.Cloud.afterFind(Parse.File, (request) => {
604
+
const { file, log } = request;
605
+
log.info(`File ${file.name()} was served to the user.`);
0 commit comments