fix: utf-8 filename breaks

https://github.com/expressjs/multer/issues/1104
This commit is contained in:
Bobby Wibowo 2022-07-03 10:18:04 +07:00
parent bd797f2c27
commit 1404cf9328
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF

View File

@ -110,12 +110,16 @@ const executeMulter = multer({
files: maxFilesPerUpload files: maxFilesPerUpload
}, },
fileFilter (req, file, cb) { fileFilter (req, file, cb) {
// BUG: Since multer@1.4.5-lts.1, UTF-8 filenames are not handled properly, so we force it
file.originalname = file.originalname &&
Buffer.from(file.originalname, 'latin1').toString('utf8')
file.extname = utils.extname(file.originalname) file.extname = utils.extname(file.originalname)
if (self.isExtensionFiltered(file.extname)) { if (self.isExtensionFiltered(file.extname)) {
return cb(new ClientError(`${file.extname ? `${file.extname.substr(1).toUpperCase()} files` : 'Files with no extension'} are not permitted.`)) return cb(new ClientError(`${file.extname ? `${file.extname.substr(1).toUpperCase()} files` : 'Files with no extension'} are not permitted.`))
} }
// Re-map Dropzone keys so people can manually use the API without prepending 'dz' // Re-map Dropzone chunked uploads keys so people can manually use the API without prepending 'dz'
for (const key in req.body) { for (const key in req.body) {
if (!/^dz/.test(key)) continue if (!/^dz/.test(key)) continue
req.body[key.replace(/^dz/, '')] = req.body[key] req.body[key.replace(/^dz/, '')] = req.body[key]