2
votes

for some reason the fileFilter on multer is not getting called.

here is my Controller (i am using express routers)

const express = require('express');
const router = express.Router();
const UploadController = require('../controllers/UploadController');

router.route('/upload').post(UploadController.upload);

module.exports = router;

and this is the controller

const multer = require('multer');
const fs = require('fs');

module.exports = {
    upload: function (req, res) {
        let storage = multer.diskStorage({
            destination: function (req, file, cb) {
                console.log('here');
                const filesDir = './uploads/' + req.body.ref;
                if (!fs.existsSync(filesDir)) {
                    fs.mkdirSync(filesDir);
                }
                cb(null, filesDir);
            },
            filename: function (req, file, cb) {
                let extArray = file.mimetype.split("/");
                let extension = extArray[extArray.length - 1];
                cb(null, req.body.type + '-' + Date.now() + '.' + extension);
            },
            fileFilter : function (req, file, cb) {
                if (!file.originalname.match(/\.(jpg|jpeg|png|gif)$/)) {
                    return cb(new Error('Only image files are allowed!'), false);
                }
                cb(null, true);
            }
        })
        const upload = multer({ storage: storage }).single('file');

        upload(req, res, function (err) {
            if (err instanceof multer.MulterError) {
                res.send({
                    error: err
                });
            } else if (err) {
                res.send({
                    error: err
                });
            }else{
                res.send({
                    file: req.file,
                    body: req.body
                });
            }

        });
    }
}

I have following issues:

  • The fileFilter function is not even called so its not validating files
  • the req.body on the upload function (upload: function (req, res)) is empty it is only available in diskStorage and last upload function (upload(req, res, function (err)) so i cannot validate the body data also.
1

1 Answers

1
votes

I had the same problem. the fileFilter function has to be defined inside multer, not inside the diskStorage function.

To make it a bit more readable I defined the storage and filter in variables instead of making everything inside the multer call.

// storage settings
const multerStorage = multer.diskStorage({
    destination: function(req, file, next) {
        next(null, './public/files');
    },
    filename: function(req, file, next) {
        const sanitizedName = file.originalname
            .replace('/[^a-z0-9\./gi', '-')
            .replace('/-{2,}/g', '-')
            .toLowerCase();
         const name = Date.now() + '-' + sanitizedName;

         // sending the file name to be stored in the database
         req.body.filename = name;

         next(null, name);
     },
     limits: {
         fileSize: 25000000
     }
 });

 // filter function
 const multerFilter = function(req, file, cb) {
     const ext = path.extname(file.originalname).toLowerCase();

     if (ext !== '.pdf') {
         cb(new Error('File must be in PDF format.'));
     }

     cb(null, true);
 }

And then applying the storage settings and filter function to multer:

const upload = multer({
    storage: multerStorage,
    fileFilter: multerFilter
});