multer icon indicating copy to clipboard operation
multer copied to clipboard

Added support for busboy configuration parameters; 'defCharset', 'defParamCharset', 'highWaterMark' and 'fileHwm'

Open RopoMen opened this issue 2 years ago • 17 comments

Hi,

I added support for these busboy configuration paramters. Reason for this is that I need ' defParamCharset' override from latin1 to utf-8. We have busboy 1.6.0 in our project and busboy has changed filename parsing from utf-8 to latin1 and it broke our application. Our Multer version is 1.4.5-lts.1 so these changes should be release on the lts side as well.

I would not like to add iconv-lite package to our application, I would prefer Multer to allow to set these properties.

I tried to upgrade busboy to 1.6.0 in Multer, but it broke too many unit tests (30+). I changed test/unicode.js to use filename åäöèøßð.dat and with busboy 1.6.0 result will be aÌaÌoÌeÌøÃð.dat and it can be fixed by adding busboy configuration defParamCharset = 'utf-8'

Fixes: #320 #846

RopoMen avatar Jun 02 '22 04:06 RopoMen

@zacharytyhacz can you give any estimate how quickly this could be merged, cherry-picked into lts and making new LTS release? If ETA is 1-3 days I can wait, otherwise I need to add iconv-lite and fix this temporarily.

RopoMen avatar Jun 06 '22 05:06 RopoMen

Hey @RopoMen - I am not a maintainer or anything, I was just looking at this PR

zacharytyhacz avatar Jun 06 '22 14:06 zacharytyhacz

@LinusU ping.

RopoMen avatar Jun 07 '22 06:06 RopoMen

Also issue #1082 is related to this.

RopoMen avatar Jun 09 '22 08:06 RopoMen

I also encountered the problem of "Chinese garbled code" today. Thank you. I got the solution - v1.4.5-lts 1.thanks.

RSS1102 avatar Jun 10 '22 21:06 RSS1102

I now use this temporary fix ( https://github.com/expressjs/multer#multeropts )

fileFilter: (req, file, cb) => {
  file.originalname = Buffer.from(file.originalname, 'latin1').toString(
    'utf8'
  )
  cb(null, true)
},

RopoMen avatar Jun 13 '22 04:06 RopoMen

I ❤️ u @RopoMen - been struggling with this issue the whole day, ur comment fixed it. Ty so much.

ghost avatar Jul 07 '22 19:07 ghost

@RopoMen After an entire day of eternal pain, you are truly a hero! Thanks so much for posting your solution! ❤️

marckornberger avatar Jul 07 '22 19:07 marckornberger

It is a good idea to make this configurable. Maybe someone can also add a notice to the recent release or something as the update from 1.44 to 1.45 is breaking stuff.

adrianmxb avatar Jul 13 '22 10:07 adrianmxb

original issue

a better solution

        if (!/[^\u0000-\u00ff]/.test(file.originalname)) {
            file.originalname = Buffer.from(file.originalname, 'latin1').toString('utf8')
        }

luoxzhg avatar Aug 15 '22 08:08 luoxzhg

What is blocking a merge of this PR?

Currently, multer in default config us unusable for file uploads with non-ASCII characters in the filename!

bf avatar Aug 31 '22 09:08 bf

This problem still exists until October 22 @RopoMen but thanks I solved this problem with the method you gave me。my version is 1.4.7

luojiong avatar Oct 22 '22 14:10 luojiong

Hello!

Thx @RopoMen for your solution. But one problem i have: If I convert the originalname with your code and after i wanna download a file with this name like

...
res.set("Content-Disposition", "attachment; filename="+convertedName);
res.set("Access-Control-Expose-Headers", "Content-Disposition");
...

(I convert the name inside fileFilter and later i download this file with a NodeJS Backend using ExpressJS)

I got the follow Error:

TypeError [ERR_INVALID_CHAR]: Invalid character in header content ["Content-Disposition"]
    at ServerResponse.setHeader (node:_http_outgoing:606:3)
........
........
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  code: 'ERR_INVALID_CHAR'

If i look at the byte code (Buffer.from(file.originalname, 'latin1')) And convert the same name by hand like Buffer.from("FileNameToConvertÄ.pdf", 'utf8') it looks different, too.

Any solution for this problem? I use Content-Disposition to get the current filename and its important for my app (a DMS Drive App).

---------- UPDATE ---------- For all with the same problem, here is a solution:

Express (were fileNames[0].name is your filename from a database as example)

 res.setHeader(
        "Content-Disposition",
        `attachment; filename*=UTF-8''${encodeURI(fileNames[0].name)}`
      );

If the route is a GET a browser will convert the filename back. If you handle it with axios or httpClient (Angular) you need to use decodeURI() to convert the name back. And all works as well! ---------- UPDATE ----------

Thx and Greetings, Florian

Flo0806 avatar Oct 25 '22 10:10 Flo0806

@Flo0806 check this npm package https://www.npmjs.com/package/content-disposition

RopoMen avatar Oct 26 '22 03:10 RopoMen

I published a multer-utf8 package on npm that read files as utf8 charset by default.

https://www.npmjs.com/package/multer-utf8

It may help you until this pr is approved.

jhpung avatar Jan 13 '23 06:01 jhpung

How someone can think that latin1 is still preferrable to utf-8 is beyond me. Please merge a fix for this so we can use non-ASCII chars in filenames without workarounds.

mkesper avatar Mar 25 '24 10:03 mkesper

This PR https://github.com/expressjs/multer/pull/1210 is backward compatible to the current multer behavior

Doc999tor avatar Mar 26 '24 01:03 Doc999tor