Skip to content

Commit f143d25

Browse files
authored
fix(storage-uploadthing): files are duplicated to the storage via client uploads (#11518)
When uploading file via client side upload we invalidate it then on the server side with re-uploading. This works fine with most adapters since they just replace the old file under the same key. UploadThing works differently and generates a new key every time. Example of the issue: <img width="611" alt="image" src="https://github.com/user-attachments/assets/9c01b52a-d159-4f32-9f66-3b5fbadab7b4" /> Now, we clear the old file before doing re-upload.
1 parent 7d2480a commit f143d25

File tree

6 files changed

+26
-2
lines changed

6 files changed

+26
-2
lines changed

packages/payload/src/types/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ type PayloadRequestData = {
9090
data?: JsonObject
9191
/** The file on the request, same rules apply as the `data` property */
9292
file?: {
93+
/**
94+
* Context of the file when it was uploaded via client side.
95+
*/
96+
clientUploadContext?: unknown
9397
data: Buffer
9498
mimetype: string
9599
name: string

packages/payload/src/utilities/addDataAndFileToRequest.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export const addDataAndFileToRequest: AddDataAndFileToRequest = async (req) => {
8787

8888
req.file = {
8989
name: filename,
90+
clientUploadContext,
9091
data: Buffer.from(await response.arrayBuffer()),
9192
mimetype: response.headers.get('Content-Type') || mimeType,
9293
size,

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ export const getBeforeChangeHook =
4444
}
4545

4646
const promises = files.map(async (file) => {
47-
await adapter.handleUpload({ collection, data, file, req })
47+
await adapter.handleUpload({
48+
clientUploadContext: file.clientUploadContext,
49+
collection,
50+
data,
51+
file,
52+
req,
53+
})
4854
})
4955

5056
await Promise.all(promises)

packages/plugin-cloud-storage/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010

1111
export interface File {
1212
buffer: Buffer
13+
clientUploadContext?: unknown
1314
filename: string
1415
filesize: number
1516
mimeType: string
@@ -28,6 +29,7 @@ export type ClientUploadsConfig =
2829
| boolean
2930

3031
export type HandleUpload = (args: {
32+
clientUploadContext: unknown
3133
collection: CollectionConfig
3234
data: any
3335
file: File

packages/plugin-cloud-storage/src/utilities/getIncomingFiles.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export function getIncomingFiles({
1616
if (file && data.filename && data.mimeType) {
1717
const mainFile: File = {
1818
buffer: file.data,
19+
clientUploadContext: file.clientUploadContext,
1920
filename: data.filename,
2021
filesize: file.size,
2122
mimeType: data.mimeType,

packages/storage-uploadthing/src/handleUpload.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,18 @@ type HandleUploadArgs = {
1212
}
1313

1414
export const getHandleUpload = ({ acl, utApi }: HandleUploadArgs): HandleUpload => {
15-
return async ({ data, file }) => {
15+
return async ({ clientUploadContext, data, file }) => {
1616
try {
17+
if (
18+
clientUploadContext &&
19+
typeof clientUploadContext === 'object' &&
20+
'key' in clientUploadContext &&
21+
typeof clientUploadContext.key === 'string'
22+
) {
23+
// Clear the old file
24+
await utApi.deleteFiles(clientUploadContext.key)
25+
}
26+
1727
const { buffer, filename, mimeType } = file
1828

1929
const blob = new Blob([buffer], { type: mimeType })

0 commit comments

Comments
 (0)