question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fileFilter refused file still uploaded in the diskStorage

See original GitHub issue

given a code similar to this:

    const multerDiskStorage = multer.diskStorage({
      destination: function(req, file, cb) {
        cb(null, "tmp/uploads/");
      },
      filename: function(req, file, cb) {
        cb(null, hashFile(file));
      }
    });
    const multerInstance = multer({
      storage: multerDiskStorage,
      fileFilter: (req, file, cb) => {
        let error;
        if (!UploadImagesController.didReceiveFile(file))
          error = new Error("no file");
        if (!UploadImagesController.validImageByteSize(req))
          error = new Error("file bigger then permitted");
        if (!UploadImagesController.validImageType(file))
          error = new Error("file type not permitted");
        if (error) {
          cb(error, false);
        }
        cb(null, true);
      },
      limits: {
        fieldSize: 20000000,
        fileSize: 20000000
      }
    });

The fileFilter works as expected except for the fact that when a fileFilter cb with error and false is invoked the file still gets uploaded in the tmp/uploads folder (with a name that is not the result of invoking the filename callback propriety defined in the storage)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
YuriScarbacicommented, Nov 6, 2019

Based on @HarshithaKP insight

I solved the problem the following way:

  1. invoked cb with only one parameter
  2. inside fileFilter returned the invokation of the cb instead of simply invoking it
  3. instead of passing an Error instance to cb, actually passed a string (passing the Error instance resulted in losing the message!!!)

this seems to have solved the problem.

Based on this I think that the readme needs to be changed to correctly state the required syntax and gotchas to make this work!

https://github.com/expressjs/multer#filefilter

1reaction
YuriScarbacicommented, Nov 2, 2019

@HarshithaKP Ty a lot for it, I will try it our ASAP,

From what i was able to infer the problem is invoking the cb with two arguments instead of invoking it only with the first argument?

Read more comments on GitHub >

github_iconTop Results From Across the Web

fileFilter on multer still allowing all file types - Stack Overflow
Added a fileFilter to my storage function allowing only image file types to be uploaded, but all file types are still being uploaded....
Read more >
Express multer middleware
Multer is a node.js middleware for handling multipart/form-data , which is primarily used for uploading files. It is written on top of busboy...
Read more >
Uploading Files Using Multer in a Node.js Application
In this article, we will see how to use Multer to handle multipart/form-data using Node.js, Express and MongoDB.
Read more >
"ENOENT: no such file or directory, open 'E:\\astrology\\utils ...
diskStorage ({ destination: function (req, file, cb) { cb(null, ... const fileFilter = (req, file, cb) => { // reject a file if ......
Read more >
Help with Multer package and image path : r/node - Reddit
image uploading const multer = require('multer') const storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, '.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found