Skip to content
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

When apply { attachFieldsToBody: 'keyValues' }, stringified buffer may not a valid file #367

Closed
2 tasks done
iketiunn opened this issue Jul 6, 2022 · 2 comments
Closed
2 tasks done

Comments

@iketiunn
Copy link

iketiunn commented Jul 6, 2022

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.1.0

Plugin version

7.1.0

Node.js version

v16.14.2

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

12.3

Description

In my case, when I apply { attachFieldsToBody: 'keyValues' }, the uploaded image was been stringified from buffer with buffer.toString() which is default to uft-8 encoding. And it might not able to be converted correctly back to the buffer.

body[key] = field._buf.toString()

Maybe buffer.toString('binary') or buffer.toString('base64') is more general and acceptable encoding.
If there is no other concern I could make a PR for it.

Steps to Reproduce

import fastifyMultipart from '@fastify/multipart'
import { writeFile} from 'fs/promises'

app.register(fastifyMultipart, { attachFieldsToBody: 'keyValues' })
app.post('upload', async (req, reply) => {
  const buffer = Buffer.from(req.image)
  await writeFile('path', buffer) // which is not a valid image
  reply.send()
})

Expected Behavior

The written image should be read correctly.

@climba03003
Copy link
Member

You can check the discussion in #348 (comment)

@iketiunn
Copy link
Author

iketiunn commented Jul 7, 2022

Okay, seems like onFile handler could do the trick. Thanks for point it out!

@iketiunn iketiunn closed this as completed Jul 7, 2022
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

No branches or pull requests

2 participants