Skip to content

Commit 9d54267

Browse files
authored
fix(plugin-cloud-storage): prevent deleting original file when duplicating (#14961)
### What Fixes the `beforeChange` hook in the `cloud-storage-plugin` to only delete files from storage when the operation is `update`, not during `create` (which includes duplication). ### Why When duplicating a media document, the `previousDoc` parameter is populated with the source document being duplicated. The existing condition `if (previousDoc)` incorrectly triggered file deletion, removing the original file from cloud storage (R2/S3/etc.) while the original database document still referenced it. This resulted in: - Original document in database with broken file reference - Original file deleted from cloud storage - New duplicated document working correctly ### How - Added `operation` to the destructured hook parameters - Changed the condition from `if (previousDoc)` to `if (previousDoc && operation === 'update')` - Files are now only deleted when genuinely updating an existing document with a new file, not when duplicating
1 parent 744a593 commit 9d54267

1 file changed

Lines changed: 4 additions & 5 deletions

File tree

packages/plugin-cloud-storage/src/hooks/afterChange.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ interface Args {
1111

1212
export const getAfterChangeHook =
1313
({ adapter, collection }: Args): CollectionAfterChangeHook<FileData & TypeWithID> =>
14-
async ({ doc, previousDoc, req }) => {
14+
async ({ doc, operation, previousDoc, req }) => {
1515
try {
1616
const files = getIncomingFiles({ data: doc, req })
1717

1818
if (files.length > 0) {
19-
// If there is a previous doc,
20-
// And we have new files,
21-
// We need to delete the old files before uploading new
22-
if (previousDoc) {
19+
// If there is a previous doc, files and the operation is update,
20+
// delete the old files before uploading the new ones.
21+
if (previousDoc && operation === 'update') {
2322
let filesToDelete: string[] = []
2423

2524
if (typeof previousDoc?.filename === 'string') {

0 commit comments

Comments
 (0)