-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Payload v3 accepts only single file uploads, export fetchAPIFileUpload as a workaround #10125
Comments
Temporary hackfix:
run this in "postinstall" |
And this brings back the old style of body parsing, automatically finding data field and safely parsing as JSON as a bonus
|
Additional issue: if filename contains a To summarize all issues with addDataAndFileToRequest:
|
Hey guys any chance to get |
What is the status of this issue? This blocks an upgrade for one of our customers |
Got mine to work without the need of a post install script, but with a direct import { APIError, PayloadRequest } from "payload"
import { fetchAPIFileUpload } from "node_modules/payload/dist/uploads/fetchAPI-multipart"
export const addDataAndFilesToRequest = async (req: PayloadRequest): Promise<void> => {
const { body, headers, method, payload } = req
if (method && ['PATCH', 'POST', 'PUT'].includes(method.toUpperCase()) && body) {
const [contentType] = (headers.get('Content-Type') || '').split(';')
const bodyByteSize = parseInt(req.headers.get('Content-Length') || '0', 10)
if (contentType === 'application/json') {
let data = {}
try {
data = await req?.json?.()
} catch (error) {
req.payload.logger.error(error)
} finally {
req.data = data
req.json = () => Promise.resolve(data)
}
} else if (bodyByteSize && contentType.includes('multipart/')) {
const { error, fields, files } = await fetchAPIFileUpload({
options: payload.config.upload,
request: req as Request,
})
if (error) {
throw new APIError(error.message)
}
const data = {
...fields || {},
...files || {},
}
req.data = Object.entries(data).reduce((acc, [key, value]) => {
const parts = key.split('.')
parts.reduce((acc: Record<string, any>, part, index) => {
if (index === parts.length - 1) {
acc[part] = value
return
}
if (!acc[part]) {
acc[part] = {}
}
return acc[part]
}, acc)
return acc
}, {})
}
}
} |
Nice one, I only take a single JSON field, no reason not to parse all. though stripe tends to shoot 0 length POST at my endpoints from time to time, keep that in mind (will crash) |
Describe the Bug
Payload v2 used to be able to handle any shape of multipart data and parse files accordingly. v3 is only able to find a single file named
file
.Ideally as a long term solution it would be great to have payload automatically figure out data types passed in a multipart body and create matching data structure. So just
req.files = files
and done I guess...As a quick and dirty fix it would be also great to just expose
fetchAPIFileUpload
so that end users can easily create custom body parsers while still using global payload upload configuration.Link to the code that reproduces this issue
https://github.com/payloadcms/payload/blob/main/packages/next/src/fetchAPI-multipart/index.ts
Reproduction Steps
upgrade to v3
make sad noises attempting to migrate file uploads
Which area(s) are affected? (Select all that apply)
area: core
Environment Info
The text was updated successfully, but these errors were encountered: