| 1 | import { Buffer } from 'node:buffer'; |
| 2 | |
| 3 | /** |
| 4 | * Decodes a file name from Latin1 to UTF-8. |
| 5 | * @param {string} str Input string |
| 6 | * @returns {string} Decoded file name |
| 7 | */ |
| 8 | function decodeFileName(str) { |
| 9 | return Buffer.from(str, 'latin1').toString('utf-8'); |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Middleware to decode file names from Latin1 to UTF-8. |
| 14 | * See: https://github.com/expressjs/multer/issues/1104 |
| 15 | * @param {import('express').Request} req Request |
| 16 | * @param {import('express').Response} _res Response |
| 17 | * @param {import('express').NextFunction} next Next middleware |
| 18 | */ |
| 19 | export default function multerMonkeyPatch(req, _res, next) { |
| 20 | try { |
| 21 | if (req.file) { |
| 22 | req.file.originalname = decodeFileName(req.file.originalname); |
| 23 | } |
| 24 | |
| 25 | next(); |
| 26 | } catch (error) { |
| 27 | console.error('Error in multerMonkeyPatch:', error); |
| 28 | next(); |
| 29 | } |
| 30 | } |