Skip to content

Commit ac46214

Browse files
authored
fix: follow redirects when fetching uploaded files for MIME type detection (#16708)
## Summary - When using S3 (or any storage adapter) with `signedDownloads` enabled, the `staticHandler` returns a `302` redirect to a pre-signed URL. Payload's `addDataAndFileToRequest` utility calls this handler internally to fetch back the just-uploaded file for MIME type detection, but Node.js `fetch` does not automatically follow redirects on a `Response` object returned from an internal handler call — resulting in an empty `ArrayBuffer` and a `DataView.getUint16()` bounds error. - The existing guard in the S3 `staticHandler` (`if (signedDownloads && !clientUploadContext)`) was meant to skip the redirect for internal upload processing, but fires anyway when `clientUploadContext` is `undefined` (i.e. when no upload context is configured for the collection). - Added redirect-following between receiving the handler response and consuming its body: if the response is a 3xx redirect, `addDataAndFileToRequest` now follows the `Location` header to retrieve the actual file bytes before processing.
1 parent f1861c1 commit ac46214

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

packages/payload/src/utilities/addDataAndFileToRequest.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ export const addDataAndFileToRequest: AddDataAndFileToRequest = async (req) => {
102102
throw new APIError('Expected response from the upload handler.')
103103
}
104104

105+
if (response.status >= 300 && response.status < 400) {
106+
const redirectUrl = response.headers.get('Location')
107+
if (redirectUrl) {
108+
response = await fetch(redirectUrl)
109+
}
110+
}
111+
105112
req.file = {
106113
name: filename,
107114
clientUploadContext,

0 commit comments

Comments
 (0)