Skip to content

fix: use buffer byteLength for external file size (#18382) - #18390

Open
PINYOPATTANAWASANPORN wants to merge 1 commit into
payloadcms:mainfrom
PINYOPATTANAWASANPORN:fix/download-file-to-buffer-byte-length-18382
Open

PINYOPATTANAWASANPORN wants to merge 1 commit into
payloadcms:mainfrom
PINYOPATTANAWASANPORN:fix/download-file-to-buffer-byte-length-18382

Conversation

@PINYOPATTANAWASANPORN

Copy link
Copy Markdown
Contributor

Summary of Changes

  • Fix downloadFileToBuffer in packages/payload/src/uploads/downloadFileToBuffer.ts to assign size: data.byteLength directly from the downloaded buffer array buffer rather than Number(response.headers.get('content-length')) || 0.
  • Added unit test in packages/payload/src/uploads/downloadFileToBuffer.spec.ts ensuring accurate file size resolution even when the upstream server omits content-length (e.g. chunked transfer or compressed storage responses).

Root Cause / Technical Context

When downloading an external file to buffer (e.g., creating from URL via Paste URL, duplicating, or re-fetching from cloud storage adapters like @payloadcms/storage-vercel-blob), downloadFileToBuffer relied on Number(response.headers.get('content-length')) || 0.

In cases where:

  1. The remote endpoint uses chunked transfer encoding (Transfer-Encoding: chunked), Content-Length is omitted, causing filesize to be recorded as 0.
  2. The remote endpoint serves compressed responses (e.g., gzip/brotli SVGs), Content-Length represents compressed transport bytes rather than the decompressed body size.

Because response.arrayBuffer() is already completely buffered into memory as data, using data.byteLength provides the exact byte size of the decoded payload.

Verification & Testing

  • Unit test added in packages/payload/src/uploads/downloadFileToBuffer.spec.ts verifying file size calculation without content-length header.
  • Verified surgical minimal diff (1 line in downloadFileToBuffer.ts + unit test).

Fixes #18382

…s#18382)

When downloading an external file to buffer in downloadFileToBuffer,
size was previously read from the response Content-Length header with
fallback to 0. When Content-Length is omitted (chunked transfer) or
represents compressed transport size, filesize was either 0 or inaccurate.
Use the actual in-memory buffer byteLength (data.byteLength).

Fixes payloadcms#18382
@PINYOPATTANAWASANPORN PINYOPATTANAWASANPORN changed the title fix(uploads): use buffer byteLength for external file size (#18382) fix: use buffer byteLength for external file size (#18382) Sep 29, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

filesize of a file fetched from a URL comes from Content-Length: 0 when the header is absent

1 participant