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.
This commit is contained in:
Sasha
2025-03-04 16:57:30 +02:00
committed by GitHub
parent 7d2480aef9
commit f143d25728
6 changed files with 26 additions and 2 deletions

View File

@@ -12,8 +12,18 @@ type HandleUploadArgs = {
}
export const getHandleUpload = ({ acl, utApi }: HandleUploadArgs): HandleUpload => {
return async ({ data, file }) => {
return async ({ clientUploadContext, data, file }) => {
try {
if (
clientUploadContext &&
typeof clientUploadContext === 'object' &&
'key' in clientUploadContext &&
typeof clientUploadContext.key === 'string'
) {
// Clear the old file
await utApi.deleteFiles(clientUploadContext.key)
}
const { buffer, filename, mimeType } = file
const blob = new Blob([buffer], { type: mimeType })