Repository navigation
fix: avoid buffering large client uploads into memory - #17856
Merged
paulpopus merged 5 commits intoAug 26, 2026
Merged
Conversation
Contributor
📦 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
marked this pull request as ready for review
August 20, 2026 12:51
nathanlentz
previously requested changes
Aug 20, 2026
nathanlentz
approved these changes
Aug 26, 2026
paulpopus
deleted the
fix/large-file-uploads-being-buffered-into-memory
branch
August 26, 2026 14:51
This was referenced Aug 28, 2026
AlessioGr
pushed a commit
that referenced
this pull request
Sep 2, 2026
## Summary PR #17856 fixed client uploads so the server reads only the bytes each save operation needs, instead of always downloading the whole file into memory. This PR adds tests that fail if that behavior regresses: if a handler is called when no file content is needed, if a full-file read goes through a whole-body buffering API instead of a stream, or if a header read goes past its byte boundary. ## Why The original fix was correct, but nothing stopped a future change from reintroducing the same problem. Size-based assertions alone could pass even after a regression, because small test fixtures hide the difference between a bounded read and an unbounded one. These tests catch the regression at its source instead. ## How - Each unit test tripwire throws the moment a forbidden operation happens, instead of only checking file size afterward. The no-content handler throws if called at all. The full-content response throws if `arrayBuffer()` is read. The header-probe stream throws if pulled past the 1 MiB boundary. - The Azure integration suite now spies on the real `BlockBlobClient.getProperties` and `download` methods and checks exact call counts and arguments, for the `none`, `header`, and `full` content-requirement paths. - Spies install only after the browser-equivalent upload finishes, so the client's own PUT to Azure does not get counted as a server-side read. - Two Azure test collections gained a `beforeValidate` hook that checks `req.file`'s shape (buffered data vs. temp file on disk) for two specific test fixtures, so the document lifecycle itself proves the file was not over-fetched. - The header-only Azure test now uses `test/uploads/2mb.jpg` instead of a small PNG, so the 1 MiB range request is a true partial read rather than one that happens to cover the whole file. Written with AI
paulpopus
added a commit
that referenced
this pull request
Sep 10, 2026
Backport of #17856 ## Summary Client uploads send a file straight from the browser to storage, bypassing the Payload server. The server still reads the file afterward, to save it locally or to read metadata such as width and height. It previously always downloaded the whole file into one in-memory buffer for that step, with no size limit. This is a real risk for the multi-gigabyte files that client uploads exist to support. The server now reads only the bytes each save operation actually needs, and streams any full-file read straight to disk instead of memory. ## Why Azure's client-upload support removes the old five-gigabyte upload ceiling, so a single upload can now be far larger than the server's available memory. Re-downloading that whole file into a buffer, just to check its size or resave it unchanged, does not scale with that change. ## How - A new content-requirement check decides how much of a file the server needs before it fetches anything: nothing, when the client-reported metadata is enough; a small byte range, when only the image dimensions are needed; or the full file, when local storage needs the real bytes, the file will be resized or reformatted, or a configured mime-type allow list needs to inspect the content. - A full-file read now streams straight to a temporary file on disk, replacing the single in-memory buffer. - An unmodified temporary file that only needs saving to disk-disabled storage is left untouched, instead of being read into memory and written back unchanged. - Fixed a check for the `disableLocalStorage` option. Collections that leave it unset, the most common case, could take the reduced-fetch path meant only for disabled local storage. That risked saving a truncated or empty file. - Fixed the byte-range probe's request clone. It broke native request properties that real storage handlers read, such as an abort signal. Every adapter in this repository that reads that property, including S3 and Azure, crashed against a client upload needing only its image dimensions. - Corrected the list of image formats treated as animated. It skipped multi-page TIFF files and wrongly flagged every AVIF file as animated, even single-frame ones. ## Testing Added tests for the new content-requirement decision and the streaming behavior. Added integration tests that complete a real client upload against the S3 and Azure storage adapters for a file needing only its dimensions, the exact path the request-clone fix above corrects. No integration test exercised that path before, which is how the bug shipped unnoticed. ## Alternatives considered Tried splitting a full-file read into repeated smaller range requests, on the idea that shorter-lived reads would free memory sooner. Measured memory during the change and found no improvement, so it was not kept. ## Related work Related to #17318 and #17319, which enabled Azure client uploads larger than 5 GB.
paulpopus
added a commit
that referenced
this pull request
Sep 21, 2026
…#17996) ## Summary This PR applies review follow-ups from #17856. It removes duplicate logic, simplifies a temp file save decision, and adds error logging for temp file cleanup. ## How - Add a shared `hasCropOrResizeEdit` helper. `generateFileData` and `getFileFromUploadInstructions` now use it, instead of each keeping its own copy of the same crop-or-resize check. - Replace two boolean flags with one `resolveTempFileHandling` function. It returns one outcome for an unprocessed temp file: copy it, read it into a buffer, or skip the save. - Rename `isClientUploadTempFile` to `isClientUploadReference` in `unlinkTempFiles`. The new name states that the file has an upload reference, not that it is a temp file. ## Related work - Follow-up to #17856
This was referenced Sep 26, 2026
Closed
nathanlentz
added a commit
that referenced
this pull request
Nov 9, 2026
…#19122) 2/3 for stack [19374](https://github.com/payloadcms/payload/stack/19374). Upload transformers receive only `file` today. They need to also get `source`, `originalSource`, `doc`, and `originalDoc`. This PR adds those arguments. `file` stays for now and existing transformers work without changes. - Transformers can now read and change transform state - Transformers now see the stored document. - Transformers get _lazy_ file sources. `source` (the current file) and `originalSource` (the retained original file) are `FileSource` objects. They read byte ranges or streams only when called. - `FileSource` is a new type I am introducing which will be needed by the Metadata API ### API Before: ```ts transformFile: async ({ file }) => { return { status: 'continue' } } ``` After: ```ts transformFile: async ({ doc, source }) => { const header = await source.readFileBytes({ numberOfBytes: 1024, startByte: 0 }) doc._transforms = { ...doc._transforms, myTransformer: { score: score(header) } } return { status: 'continue' } } ``` ### Note For direct uploads to cloud storage, Payload no longer decides before the upload document exists whether a transformer needs the whole file. It first reads only what the upload needs without transformers, then calls `canTransform` with the real `doc`. It will download the whole file only when a transformer accepts the upload in order to preserve work done in #17856
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Client uploads send a file straight from the browser to storage, bypassing the Payload server. The server still reads the file afterward, to save it locally or to read metadata such as width and height. It previously always downloaded the whole file into one in-memory buffer for that step, with no size limit. This is a real risk for the multi-gigabyte files that client uploads exist to support. The server now reads only the bytes each save operation actually needs, and streams any full-file read straight to disk instead of memory.
Why
Azure's client-upload support removes the old five-gigabyte upload ceiling, so a single upload can now be far larger than the server's available memory. Re-downloading that whole file into a buffer, just to check its size or resave it unchanged, does not scale with that change.
How
disableLocalStorageoption. Collections that leave it unset, the most common case, could take the reduced-fetch path meant only for disabled local storage. That risked saving a truncated or empty file.Testing
Added tests for the new content-requirement decision and the streaming behavior. Added integration tests that complete a real client upload against the S3 and Azure storage adapters for a file needing only its dimensions, the exact path the request-clone fix above corrects. No integration test exercised that path before, which is how the bug shipped unnoticed.
Alternatives considered
Tried splitting a full-file read into repeated smaller range requests, on the idea that shorter-lived reads would free memory sooner. Measured memory during the change and found no improvement, so it was not kept.
Related work
Related to #17318 and #17319, which enabled Azure client uploads larger than 5 GB.