Skip to content
This repository was archived by the owner on Jan 16, 2024. It is now read-only.

Commit 60cf803

Browse files
committedApr 27, 2023
fix: #43, not deleting old files
1 parent 7eb8d8b commit 60cf803

File tree

4 files changed

+273
-337
lines changed

4 files changed

+273
-337
lines changed
 

‎dev/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"dotenv": "^8.2.0",
2222
"express": "^4.17.1",
2323
"image-size": "^1.0.2",
24-
"payload": "^1.6.6",
24+
"payload": "^1.7.2",
2525
"probe-image-size": "^7.2.3"
2626
},
2727
"devDependencies": {

‎package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"@aws-sdk/lib-storage": "^3.267.0",
1818
"@azure/storage-blob": "^12.11.0",
1919
"@google-cloud/storage": "^6.4.1",
20-
"payload": "^1.0.27"
20+
"payload": "^1.7.2"
2121
},
2222
"peerDependenciesMeta": {
2323
"@aws-sdk/client-s3": {
@@ -55,7 +55,7 @@
5555
"eslint-plugin-import": "2.25.4",
5656
"eslint-plugin-prettier": "^4.0.0",
5757
"nodemon": "^2.0.6",
58-
"payload": "^1.6.6",
58+
"payload": "^1.7.2",
5959
"prettier": "^2.7.1",
6060
"rimraf": "^4.1.2",
6161
"ts-node": "^9.1.1",
@@ -64,4 +64,4 @@
6464
"dependencies": {
6565
"range-parser": "^1.2.1"
6666
}
67-
}
67+
}

‎src/hooks/beforeChange.ts

+34-5
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,44 @@ interface Args {
1111

1212
export const getBeforeChangeHook =
1313
({ collection, adapter }: Args): CollectionBeforeChangeHook<FileData & TypeWithID> =>
14-
async ({ req, data }) => {
14+
async ({ req, data, originalDoc }) => {
1515
try {
1616
const files = getIncomingFiles({ req, data })
1717

18-
const promises = files.map(async file => {
19-
await adapter.handleUpload({ collection, data, req, file })
20-
})
18+
if (files.length > 0) {
19+
// If there is an original doc,
20+
// And we have new files,
21+
// We need to delete the old files before uploading new
22+
if (originalDoc) {
23+
let filesToDelete: string[] = []
2124

22-
await Promise.all(promises)
25+
if (typeof originalDoc?.filename === 'string') {
26+
filesToDelete.push(originalDoc.filename)
27+
}
28+
29+
if (typeof originalDoc.sizes === 'object') {
30+
filesToDelete = filesToDelete.concat(
31+
Object.values(originalDoc?.sizes || []).map(
32+
resizedFileData => resizedFileData?.filename,
33+
),
34+
)
35+
}
36+
37+
const deletionPromises = filesToDelete.map(async filename => {
38+
if (filename) {
39+
await adapter.handleDelete({ collection, doc: originalDoc, req, filename })
40+
}
41+
})
42+
43+
await Promise.all(deletionPromises)
44+
}
45+
46+
const promises = files.map(async file => {
47+
await adapter.handleUpload({ collection, data, req, file })
48+
})
49+
50+
await Promise.all(promises)
51+
}
2352
} catch (err: unknown) {
2453
req.payload.logger.error(
2554
`There was an error while uploading files corresponding to the collection ${collection.slug} with filename ${data.filename}:`,

‎yarn.lock

+235-328
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
This repository has been archived.