Skip to content

Commit f2cbc85

Browse files
authored
fix(storage-vercel-blob): keep the receipt key for processed client uploads with addRandomSuffix (#18921)
Stacked on #18913. Review and merge #18388 and #18913 first. ## Summary With `addRandomSuffix: true`, a processed client upload was saved again under a new name with a suffix. The original file from the browser stayed in storage, and nothing used it. Now the processed file replaces the original file, and the document keeps the original filename. ## How - The server already saves the temp-file path of a client upload in `req.context` for cleanup. The adapter turns off `addRandomSuffix` when the file's `tempFilePath` is the same as that saved path. - The client cannot set this value, because the server sets both paths. - The check does not use `_objectKey`, so legacy overwrite receipts without `_objectKey` are covered too. - `payload` now exports `CLIENT_UPLOAD_TEMP_FILE_PATH_CONTEXT_KEY`, so the adapter does not copy the key string. ## Related work - Stack: #18388 → #18913 → this PR - Reported in a comment on #18333 and in the #18387 review. - #18920 fixes a separate `addRandomSuffix` bug with image sizes. Both PRs add `packages/storage-vercel-blob/src/adapter.spec.ts`, so the second one to merge needs a rebase.
1 parent 8282414 commit f2cbc85

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

‎packages/payload/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,6 +1797,7 @@ export {
17971797
export { getLocalI18n } from './translations/getLocalI18n.js'
17981798
export * from './types/index.js'
17991799
export { getFileByPath } from './uploads/getFileByPath.js'
1800+
export { CLIENT_UPLOAD_TEMP_FILE_PATH_CONTEXT_KEY } from './uploads/getFileFromClientUpload.js'
18001801
export { _internal_safeFetchGlobal } from './uploads/safeFetch.js'
18011802

18021803
export type * from './uploads/types.js'

‎packages/storage-vercel-blob/src/adapter.spec.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ vi.mock('@vercel/blob', async (importOriginal) => ({
66
put: vi.fn(async (pathname: string) => ({ pathname: pathname.replace('.png', '-suffix.png') })),
77
}))
88

9+
vi.mock('fs', async (importOriginal) => ({
10+
...(await importOriginal<typeof import('fs')>()),
11+
openAsBlob: vi.fn(async () => new Blob(['file'])),
12+
}))
13+
914
import { createVercelBlobAdapter } from './adapter.js'
1015

1116
const adapter = createVercelBlobAdapter({
@@ -36,6 +41,7 @@ describe('handleUpload with addRandomSuffix', () => {
3641
imageSizeName: filename === 'image.png' ? undefined : 'square',
3742
mimeType: 'image/png',
3843
},
44+
req: { context: {} },
3945
storageFilePath: filename,
4046
} as never)
4147

@@ -61,6 +67,7 @@ describe('handleUpload with addRandomSuffix', () => {
6167
imageSizeName,
6268
mimeType: 'image/png',
6369
},
70+
req: { context: {} },
6471
storageFilePath: 'image-400x400.png',
6572
} as never)
6673

@@ -71,4 +78,28 @@ describe('handleUpload with addRandomSuffix', () => {
7178
square: { filename: 'image-400x400-b.png' },
7279
})
7380
})
81+
82+
it.each([
83+
['a processed client upload', '/tmp/client-upload', false],
84+
['another temp file', '/tmp/other-upload', true],
85+
])('should set addRandomSuffix for %s', async (_, tempFilePath, expected) => {
86+
await adapter.handleUpload({
87+
data: { filename: 'image.png' },
88+
file: {
89+
buffer: Buffer.alloc(0),
90+
filename: 'image.png',
91+
filesize: 4,
92+
mimeType: 'image/png',
93+
tempFilePath,
94+
},
95+
req: { context: { payloadClientUploadTempFilePath: '/tmp/client-upload' } },
96+
storageFilePath: 'issued/image.png',
97+
} as never)
98+
99+
expect(vi.mocked(put)).toHaveBeenLastCalledWith(
100+
'issued/image.png',
101+
expect.anything(),
102+
expect.objectContaining({ addRandomSuffix: expected }),
103+
)
104+
})
74105
})

‎packages/storage-vercel-blob/src/adapter.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import type {
44
GeneratedAdapter,
55
} from '@payloadcms/plugin-cloud-storage/types'
66

7+
import { CLIENT_UPLOAD_TEMP_FILE_PATH_CONTEXT_KEY } from 'payload'
8+
79
import { deleteFile } from './deleteFile.js'
810
import { generateURL } from './generateURL.js'
911
import { getFile } from './getFile.js'
@@ -52,11 +54,16 @@ export function createVercelBlobAdapter({
5254
handleUpload: async ({
5355
data,
5456
file: { buffer, imageSizeName, mimeType, tempFilePath },
57+
req,
5558
storageFilePath,
5659
}) => {
60+
// A processed client upload keeps the storage key from its receipt.
61+
const isClientUpload =
62+
Boolean(tempFilePath) &&
63+
tempFilePath === req.context?.[CLIENT_UPLOAD_TEMP_FILE_PATH_CONTEXT_KEY]
5764
const result = await uploadFile({
5865
access,
59-
addRandomSuffix,
66+
addRandomSuffix: addRandomSuffix && !isClientUpload,
6067
buffer,
6168
cacheControlMaxAge,
6269
mimeType,

0 commit comments

Comments
 (0)