Skip to content

Commit ea3778b

Browse files
authored
fix: upload processed client files from temp file for upload-thing storage (#19010)
Follow-up to #18388 for #18333. If Payload processed a direct-client upload, UploadThing could store a zero-byte file. The document still reported the processed file size. The adapter now uploads from the temporary file that holds the processed bytes, the same as Vercel Blob, GCS, and R2 after #18388. - For client uploads, Payload keeps the file on disk and does not load it into memory. After processing, the new bytes are in the temporary file and `file.buffer` is empty. - When `tempFilePath` is present, the UploadThing adapter reads the bytes from that file.
1 parent 33b9740 commit ea3778b

3 files changed

Lines changed: 48 additions & 1 deletion

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { randomUUID } from 'crypto'
2+
import { rm, writeFile } from 'fs/promises'
3+
import { tmpdir } from 'os'
4+
import path from 'path'
5+
import { expect, it } from 'vitest'
6+
7+
import { createUploadthingAdapter } from './adapter.js'
8+
9+
it('should upload the temporary file contents', async () => {
10+
const bytes = Buffer.from('processed image contents')
11+
const tempFilePath = path.join(tmpdir(), `uploadthing-upload-${randomUUID()}`)
12+
let storedFile: File | undefined
13+
const utApi = {
14+
uploadFiles: (file: File) => {
15+
storedFile = file
16+
return { data: { name: file.name, key: 'stored-key' } }
17+
},
18+
}
19+
const adapter = createUploadthingAdapter({ acl: 'public-read', utApi: utApi as never })({
20+
collection: { slug: 'media' },
21+
} as never)
22+
23+
await writeFile(tempFilePath, bytes)
24+
25+
try {
26+
await adapter.handleUpload({
27+
data: {},
28+
file: {
29+
buffer: Buffer.alloc(0),
30+
filename: 'processed.png',
31+
mimeType: 'image/png',
32+
tempFilePath,
33+
},
34+
storageFilePath: 'processed.png',
35+
} as never)
36+
37+
expect(Buffer.from(await storedFile!.arrayBuffer())).toEqual(bytes)
38+
} finally {
39+
await rm(tempFilePath, { force: true })
40+
}
41+
})

‎packages/storage-uploadthing/src/adapter.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export function createUploadthingAdapter({
5656
data,
5757
filename: file.filename,
5858
mimeType: file.mimeType,
59+
tempFilePath: file.tempFilePath,
5960
utApi,
6061
})
6162

‎packages/storage-uploadthing/src/uploadFile.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { UTApi } from 'uploadthing/server'
22

3+
import { openAsBlob } from 'fs'
34
import { APIError } from 'payload'
45
import { UTFile } from 'uploadthing/server'
56

@@ -12,6 +13,7 @@ interface UploadFileArgs {
1213
data: Record<string, unknown>
1314
filename: string
1415
mimeType: string
16+
tempFilePath?: string
1517
utApi: UTApi
1618
}
1719

@@ -22,6 +24,7 @@ export async function uploadFile({
2224
data,
2325
filename,
2426
mimeType,
27+
tempFilePath,
2528
utApi,
2629
}: UploadFileArgs): Promise<Record<string, unknown>> {
2730
try {
@@ -34,7 +37,9 @@ export async function uploadFile({
3437
await utApi.deleteFiles(clientUploadContext.key)
3538
}
3639

37-
const blob = new Blob([buffer], { type: mimeType })
40+
const blob = tempFilePath
41+
? await openAsBlob(tempFilePath, { type: mimeType })
42+
: new Blob([buffer], { type: mimeType })
3843
const res = await utApi.uploadFiles(new UTFile([blob], filename), { acl })
3944

4045
if (res.error) {

0 commit comments

Comments
 (0)