richardgirges / express-fileupload

Simple express file upload middleware that wraps around busboy
MIT License
1.52k stars 259 forks source link

filename tmp-1-1616611601149 instead of filename on the host pc #339

Closed mukundag closed 10 months ago

mukundag commented 1 year ago

code: const fileUpload = require("express-fileupload"); app.use(fileUpload({ limits: { fileSize: 1024 1024 1024 // 1 GB }, abortOnLimit: true, safeFileNames: true, preserveExtension: 8, //debug: true, useTempFiles : true, tempFileDir : '/tmp/', uriDecodeFileNames : true, defParamCharset: 'utf-8' }));

app.post("/updateFile", (req, res) => { if (!req.files) { return res.status(400).send("No files were uploaded."); }

const file = req.files.userAppFile;
console.log('upload : ' + JSON.stringify(file))
const path = '/var/volatile/tmp/' + file.name;

file.mv(path, (err) => {
  if (err) {
    return res.status(500).send(err);
  }
  return res.send({ status: "success", path: path });
});

});

Log:

upload : {"name":"tmp-1-1616611601149","data":{"type":"Buffer","data":[]},"size":341712,"tempFilePath":"/tmp/tmp-2-1616611601150","truncated":false,"md5":"cdf49126018bb6e5f43f0e9e87ba7319"}

mukundag commented 1 year ago

I tried using the example provided with package using postman with a file "option.ko". The size is correct and file contents are correct.

Node Version: v12.21.0

root@fxr90evk:/platform/www/web-app/node_modules/express-fileupload/example# node server.js Express server listening on port 8000 req.files >>> [Object: null prototype] { sampleFile: { name: 'tmp-1-1616613072957', data: <Buffer 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 01 00 b7 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 90 2c 05 00 00 00 00 00 00 00 ... 341662 more bytes>, size: 341712, encoding: undefined, tempFilePath: '', truncated: false, mimetype: undefined, md5: 'cdf49126018bb6e5f43f0e9e87ba7319', mv: [Function: mv] } }

RomanBurunkov commented 10 months ago

Hi,

That can happens if client didn't send a file name or server was not able to get it some how. When the name empty or it is not a string, mw set it to random file name, see code below:

if (!fileName || typeof fileName !== 'string') return getTempFilename();