Skip to content

Commit 7811d49

Browse files
authored
docs: Update Cloud Code file triggers (#962)
1 parent 3c7484c commit 7811d49

File tree

1 file changed

+49
-17
lines changed

1 file changed

+49
-17
lines changed

_includes/cloudcode/cloud-code.md

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -479,56 +479,56 @@ Parse.Cloud.afterDelete(Parse.User, async (request) => {
479479

480480
# File Triggers
481481

482-
## beforeSaveFile
482+
## beforeSave
483483

484-
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.
485485

486486
### Examples
487487

488488
```javascript
489489
// Changing the file name
490-
Parse.Cloud.beforeSaveFile(async (request) => {
490+
Parse.Cloud.beforeSave(Parse.File, async (request) => {
491491
const { file } = request;
492492
const fileData = await file.getData();
493493
const newFile = new Parse.File('a-new-file-name.txt', { base64: fileData });
494494
return newFile;
495495
});
496496

497497
// Returning an already saved file
498-
Parse.Cloud.beforeSaveFile((request) => {
498+
Parse.Cloud.beforeSave(Parse.File, (request) => {
499499
const { user } = request;
500500
const avatar = user.get('avatar'); // this is a Parse.File that is already saved to the user object
501501
return avatar;
502502
});
503503

504504
// Saving a different file from uri
505-
Parse.Cloud.beforeSaveFile((request) => {
505+
Parse.Cloud.beforeSave(Parse.File, (request) => {
506506
const newFile = new Parse.File('some-file-name.txt', { uri: 'www.somewhere.com/file.txt' });
507507
return newFile;
508508
});
509509
```
510510

511511
### Metadata and Tags
512512

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 `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.
514514

515515
Note: not all storage adapters support metadata and tags. Check the documentation for the storage adapter you're using for compatibility.
516516

517517
```javascript
518518
// Adding metadata and tags
519-
Parse.Cloud.beforeSaveFile((request) => {
519+
Parse.Cloud.beforeSave(Parse.File, (request) => {
520520
const { file, user } = request;
521521
file.addMetadata('createdById', user.id);
522522
file.addTag('groupId', user.get('groupId'));
523523
});
524524
```
525525

526-
## afterSaveFile
526+
## afterSave
527527

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:
529529

530530
```javascript
531-
Parse.Cloud.afterSaveFile(async (request) => {
531+
Parse.Cloud.afterSave(Parse.File, async (request) => {
532532
const { file, fileSize, user } = request;
533533
const fileObject = new Parse.Object('FileObject');
534534
fileObject.set('file', file);
@@ -539,20 +539,20 @@ Parse.Cloud.afterSaveFile(async (request) => {
539539
});
540540
```
541541

542-
## beforeDeleteFile
542+
## beforeDelete
543543

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 `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:
545545

546546
```javascript
547-
Parse.Cloud.afterSaveFile(async (request) => {
547+
Parse.Cloud.afterSave(Parse.File, async (request) => {
548548
const { file, user } = request;
549549
const fileObject = new Parse.Object('FileObject');
550550
fileObject.set('fileName', file.name());
551551
fileObject.set('createdBy', user);
552552
await fileObject.save(null, { useMasterKey: true );
553553
});
554554

555-
Parse.Cloud.beforeDeleteFile(async (request) => {
555+
Parse.Cloud.beforeDelete(Parse.File, async (request) => {
556556
const { file, user } = request;
557557
const query = new Parse.Query('FileObject');
558558
query.equalTo('fileName', file.name());
@@ -563,12 +563,12 @@ Parse.Cloud.beforeDeleteFile(async (request) => {
563563
});
564564
```
565565
566-
## afterDeleteFile
566+
## afterDelete
567567
568-
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.
569569
570570
```javascript
571-
Parse.Cloud.afterDeleteFile(async (request) => {
571+
Parse.Cloud.afterDelete(Parse.File, async (request) => {
572572
const { file } = request;
573573
const query = new Parse.Query('FileObject');
574574
query.equalTo('fileName', file.name());
@@ -577,6 +577,38 @@ Parse.Cloud.afterDeleteFile(async (request) => {
577577
});
578578
```
579579
580+
## beforeFind
581+
582+
*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.`);
606+
if (file.name().endsWith('.txt')) {
607+
request.forceDownload = true;
608+
}
609+
});
610+
```
611+
580612
# Find Triggers
581613
582614
## beforeFind

0 commit comments

Comments
 (0)