Repository navigation
fix: preserve processed client upload contents - #18387
nathanlentz wants to merge 15 commits into
Conversation
📦 esbuild Bundle Analysis for payloadThis analysis was generated by esbuild-bundle-analyzer. 🤖
Largest pathsThese visualization shows top 20 largest paths in the bundle.Meta file: packages/next/meta_index.json, Out file: esbuild/index.js
Meta file: packages/payload/meta_index.json, Out file: esbuild/index.js
Meta file: packages/payload/meta_shared.json, Out file: esbuild/exports/shared.js
Meta file: packages/richtext-lexical/meta_client.json, Out file: esbuild/exports/client_optimized/index.js
Meta file: packages/ui/meta_client.json, Out file: esbuild/exports/client_optimized/index.js
Meta file: packages/ui/meta_shared.json, Out file: esbuild/exports/shared_optimized/index.js
DetailsNext to the size is how much the size has increased or decreased compared with the base branch of this PR.
|
paulpopus
left a comment
There was a problem hiding this comment.
A better approach might be to keep client-upload information on the file. Do not infer it from _objectKey or request context.
Define the upload state
type ClientUploadState = {
isProcessed: boolean
originalStorageFilePath: string
}
type UploadFile = {
data: Buffer
mimetype: string
name: string
size: number
tempFilePath?: string
clientUpload?: ClientUploadState
}Add the state after verifying the upload receipt
const file: UploadFile = {
data: Buffer.alloc(0),
mimetype: response.headers.get('Content-Type') || instructions.mimeType,
name: instructions.filename,
size: instructions.size,
tempFilePath,
clientUpload: {
isProcessed: false,
originalStorageFilePath: receipt.storageFilePath,
},
}This records that the file came from a verified client upload. It also records the exact object path assigned by the server.
Update the complete file after processing
When Payload processes the file, update the file object and put it back on the request:
const processedFile: UploadFile = {
...file,
data: Buffer.alloc(0),
mimetype: processedMimeType,
name: processedFilename,
size: processedSize,
tempFilePath,
clientUpload: file.clientUpload
? {
...file.clientUpload,
isProcessed: true,
}
: undefined,
}
req.file = processedFileThis prevents req.file from retaining an old uploadReference, MIME type, filename, or file size.
Decide whether the file needs uploading
The storage hook can make this decision from the file state:
const shouldUploadFile = !file.clientUpload || file.clientUpload.isProcessed
if (shouldUploadFile) {
await adapter.handleUpload({
file,
storageFilePath,
})
}The rules are:
- Server uploads must be uploaded.
- Unprocessed client uploads are already stored and can be skipped.
- Processed client uploads must be uploaded again.
Keep the server-issued key
Adapters no longer need to inspect _objectKey or request context:
const shouldAddRandomSuffix = file.clientUpload ? false : addRandomSuffixThis also works for legacy documents without _objectKey.
Remove the original object when its path changes
A format conversion can change image.png to image.webp. Remove the original client-uploaded object after the processed upload and metadata update succeed:
const originalStorageFilePath = file.clientUpload?.originalStorageFilePath
if (
originalStorageFilePath &&
originalStorageFilePath !== storageFilePath
) {
await adapter.handleDelete({
storageFilePath: originalStorageFilePath,
})
}This prevents unused objects from remaining in storage.
Isolate bulk-operation request state
Each document in a parallel bulk operation should receive separate mutable request state:
const documentReq = isolateObjectProperty(req, [
'context',
'file',
'payloadUploadSizes',
'query',
])
documentReq.context = { ...req.context }
documentReq.query = { ...req.query }This prevents one document from changing uploadEdits or skipCloudStorage while another document is still processing.
Result
This keeps the important facts together:
- where the client uploaded the original file;
- whether Payload processed the file;
- whether the file needs uploading again;
- whether the original object needs deletion.
It also removes the need to infer file state from _objectKey, temporary-file paths, or shared request context.
Regardless here are other findings on the PR, happy to discuss directly if you want to change the direction of the solution here:
| delete fileForResize.uploadReference | ||
| if (file.tempFilePath) { | ||
| await fs.writeFile(file.tempFilePath, croppedImage) | ||
| await fs.writeFile(file.tempFilePath, fileForResize.data) |
There was a problem hiding this comment.
When detected and submitted MIME types differ, generateFileData clones file and removes uploadReference only from that clone. req.file keeps the reference, so the storage hook skips the processed main file. This can serve original bytes or make the saved URL point to no object. Synchronise the processed state back to req.file, and add a client-upload test with a MIME mismatch and processing.
| storageFilePath: buildUploadStoragePathData({ | ||
| collectionPrefix, | ||
| docPrefix: dataForUpload.prefix, | ||
| filename: file.filename, |
There was a problem hiding this comment.
formatOptions can change image.png to image.webp, so this upload uses a different path from the browser upload. Create operations have no previousDoc, so the original object remains in storage. Delete the original client-upload path after the processed upload succeeds, and add a format-conversion test that checks the final object count.
| req.payloadUploadSizes = undefined | ||
| const uploadEdits = req.query?.uploadEdits | ||
| if (req.query) { | ||
| delete req.query.uploadEdits |
There was a problem hiding this comment.
Bulk document requests share req.query and req.context, but this code changes both across an awaited update. One operation can restore uploadEdits or remove skipCloudStorage while another nested update is still running. Isolate these properties for each document and add a parallel bulk crop or reprocessing test.
| const clientUploadTempFilePath = req.context?._payloadClientUploadTempFile | ||
| const isProcessedClientUpload = Boolean( | ||
| clientUploads && | ||
| data._objectKey && |
There was a problem hiding this comment.
Authorised overwrite receipts for legacy documents can have no _objectKey, so this condition does not identify their processed client uploads. With addRandomSuffix: true, the processed overwrite moves to a second key and changes the document URL. Use the verified temporary-file marker without requiring _objectKey, and add a legacy overwrite test.
…ploads 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.
Description
Fixes #18333. Processed direct-client uploads could leave a zero-byte Vercel Blob object while the document reported the processed file size. The upload now reads the processed temporary file. GCS and R2 use the temporary file too when one exists.
The final crop and resize bytes are written to that file. Processed client uploads keep the object key assigned by the upload receipt. Server-side uploads retain their configured random-suffix behavior.
Shared-hook behavior
File.sizeNameto update the matching size, even when sizes share a filename.req.file,req.payloadUploadSizes, andreq.context._payloadCloudStorageso later operations do not upload the same file again.Validation
Backport to
3.x: #18388.