/**
 * Multer encodes originalname as latin1 (ISO-8859-1).
 * Browsers send filenames as UTF-8 bytes, so non-ASCII chars get mangled.
 * This middleware re-decodes the bytes back to proper UTF-8.
 */
function fixUtf8Name(req, _res, next) {
  if (req.file && req.file.originalname) {
    try {
      req.file.originalname = Buffer.from(req.file.originalname, 'latin1').toString('utf8');
    } catch { /* keep as-is if decoding fails */ }
  }
  next();
}

module.exports = { fixUtf8Name };
