Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fileFilter not working #246

Open
tibortru opened this issue Oct 13, 2015 · 10 comments
Open

fileFilter not working #246

tibortru opened this issue Oct 13, 2015 · 10 comments

Comments

@tibortru
Copy link

I wrote about this here #192

I have multer 1.0.6 , node 4.1.2, express 4.13.3

And this in file filter:

fileFilter: function (req, file, cb) {

        // The function should call `cb` with a boolean
        // to indicate if the file should be accepted

        // To reject this file pass `false`, like so:
        if (file.mimetype !== 'image/png'
            && file.mimetype !== 'image/jpg'
            && file.mimetype !== 'image/jpeg'
            && file.mimetype !== 'image/gif') {
            console.log('Got file of type', file.mimetype);
            return cb(new Error('Only image files are allowed!'));
        }

        // To accept the file pass `true`, like so:
        cb(null, true);

    }

When I send image file, everything works properly. But when I send something else like blabla.txt I expect error to return, but the request goes pending... in Chrome it says it is pending. After a couple of minutes I get response with net::ERR_EMPTY_RESPONSE.

I tried calling only

cb(new Error('Only image files are allowed!'));

without return statement, same issue.

@LinusU
Copy link
Member

LinusU commented Oct 13, 2015

Hmm, are all the tests passing for you? Could you please try and clone this repo and run npm test?

@tibortru
Copy link
Author

44 passing (552ms)
8 failing

1) Disk Storage should process parser/form-data POST request:

      Uncaught AssertionError: 1803 == 1778
      + expected - actual

      -1803
      +1778
 2) Disk Storage should process multiple files:

      Uncaught AssertionError: 128 == 122
      + expected - actual

      -128
      +122
3) Functionality should upload the file to the `dest` dir:

      Uncaught AssertionError: 1803 == 1778
      + expected - actual

      -1803
      +1778
 4) Functionality should rename the destination directory to a different directory:

      Uncaught AssertionError: false == true
      + expected - actual

      -false
      +true
5) Memory Storage should process multipart/form-data POST request:

      Uncaught AssertionError: 1803 == 1778
      + expected - actual

      -1803
      +1778
6) Memory Storage should process multiple files:

      Uncaught AssertionError: 128 == 122
      + expected - actual

      -128
      +122
 7) Reuse Middleware should accept multiple requests:

      Uncaught AssertionError: 1803 == 1778
      + expected - actual

      -1803
      +1778
8) Unicode should handle unicode filenames:

      Uncaught AssertionError: 1803 == 1778
      + expected - actual

      -1803
      +1778

@tibortru
Copy link
Author

A strange thing happened as I changed all files in the repo to have LF line separators... at first they had CRLF as I cloned repo to my machine... anyway, now I have only 1 test failing

1) Functionality should rename the destination directory to a different directory:

      Uncaught AssertionError: false == true
      + expected - actual

      -false
      +true

      at C:\Users\Win7\Documents\Projects\multer\test\functionality.js:132:14

Any clues... so far ? :/ I am really bothered by this :(

@cwolpert
Copy link

Hi tibortru, I had the same problem. Finally my solution was to give up multer's fileFilter and do the mimetype check on the temporary file inside app.post() . Files that do not pass the check are deleted.
My code looks like this:

app.post('/upload/*', upload.single('image'), function(req, res, cb) {
if(path.extname(req.file.originalname).toLowerCase() !== '.jpeg') {
fs.unlinkSync(req.file.path) //delete temporary file
}
...
}

@srkkhan
Copy link

srkkhan commented Aug 16, 2016

This worked for me:

fileFilter: function (req, file, cb) {
    if (!file.originalname.match(/\.(jpg|jpeg|png|gif)$/)) {
        return cb(new Error('Only image files are allowed!'));
    }
    cb(null, true);
  }

@benbonnet
Copy link

benbonnet commented Dec 12, 2016

@srkkhan you've added the same wrong hint there (#114), but it is not safe to test a file by its filename extension, it has to be done by its mime type. That said, @tim-montague last answer on #114 has no real effects on my side :/. Some has changed and things needs to be done another way ?

@pguerrerox
Copy link

Hello,
This was happening to me, this is how I fix it,

  • From this response - Link to #186 I grabed this code, He used it to display a "FILE TO BIG" error.
app.use(function (err, req, res, next) {
  if (err.code === 'LIMIT_FILE_SIZE') {
    res.send({ result: 'fail', error: { code: 1001, message: 'File is too big' } })
    return 
  }
  // Handle any other errors
})
  • Them I added the following code:
if(err){
  console.log(err);
  res.send({ error: 'YOUR MESSAGE TO BE DISPLAY' });
}

This help me process the error created if the fileFilter is false.

hope it helps.

@retroceso
Copy link

retroceso commented Apr 2, 2019

const upload = multer({
fileFilter:(req,file,cb) => {
let extname = file.originalname.toLowerCase().match(/.(jpeg|jpg|png|gif)$/);
let mimetype = file.mimetype.match(/(jpeg|jpg|png|gif)$/);
if(mimetype && extname) cb(null,true);
else cb('Error: Images Only');
},
storage:multer.diskStorage({
destination:'./public/img/',
filename:(req,file,cb) => cb(null,PKred_${Date.now()+''+(Math.floor(Math.random()*100000))}),
limits:{ fileSize:1000000 }
})
}).single('file');

@mycharoka
Copy link

mycharoka commented Sep 19, 2020

still wondering what this line does if (err.code === 'LIMIT_FILE_SIZE') {
@pguerrerox

@pguerrerox
Copy link

still wondering what this line does if (err.code === 'LIMIT_FILE_SIZE') {
@pguerrerox

if there an error, the "err" object have a "code" key. That line just check if the code throw in the error is a size related.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants