Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Decodes a file name from Latin1 to UTF-8. | ||
* @param {string} str Input string | ||
* @returns {string} Decoded file name | ||
*/ | ||
function decodeFileName(str) { | ||
return Buffer.from(str, 'latin1').toString('utf-8'); | ||
} | ||
|
||
/** | ||
* Middleware to decode file names from Latin1 to UTF-8. | ||
* See: https://github.com/expressjs/multer/issues/1104 | ||
* @param {import('express').Request} req Request | ||
* @param {import('express').Response} _res Response | ||
* @param {import('express').NextFunction} next Next middleware | ||
*/ | ||
function multerMonkeyPatch(req, _res, next) { | ||
try{ | ||
if (req.file) { | ||
req.file.originalname = decodeFileName(req.file.originalname); | ||
} | ||
|
||
next(); | ||
} catch (error) { | ||
console.error('Error in multerMonkeyPatch:', error); | ||
next(); | ||
} | ||
} | ||
|
||
module.exports = multerMonkeyPatch; |