Search code examples
javascriptnode.jsreactjsmultercloudinary

how to check file type in multer


I'm working on a project in React and ran into a problem that has me stumped.

I want to upload my file on cloudinary and also want to check my file type and mime type but facing this error this is my code

so here is my code and i am not able to filter out file type.

if any one have some solution it will be really great if you help me

const multer = require("multer");
const DatauriParser = require("datauri/parser");
const path = require("path");
const fs = require("fs");
const dontenv = require("dotenv");
dontenv.config({ path: "config.env" });

let storage;
let dataUri;
const parser = new DatauriParser();

if (process.env.STORAGE_TYPE == "cloud") {
    storage = multer.memoryStorage();
    dataUri = (req) =>
        parser.format(
            path.extname(req.file.originalname).toString(),
            req.file.buffer
        );
} else {
    dataUri = {};
    storage = multer.diskStorage({
        destination: function (req, file, cb) {
            var directory = process.env.STORAGE;
            if (!fs.existsSync(directory)) {
                fs.mkdirSync(directory);
            }
            cb(null, directory);
        },
        filename: function (req, file, cb) {
            cb(
                null,
                file.fieldname +
                    "-" +
                    Date.now() +
                    "." +
                    file.originalname.split(".")[1]
            );
        },
        fileFilter: function (req, file, cb) {
            checkFileType(file, cb);
        },
  });
  
    function checkFileType(file, cb) {
        // Allowed ext
        const filetypes = /jpeg|jpg|png|gif/;
        // Check ext
        const extname = filetypes.test(         
            path.extname(req.file.originalname).toString()
        );
        // Check mime
        const mimetype = filetypes.test(file.mimetype);

        if (mimetype && extname) {
            return cb(null, true);
        } else {
            cb("Error: Images Only!");
        }
    }

    // to Check File TYpe
}

const multerUploads = multer({ storage: storage }).single("imageUrl");

module.exports = { multerUploads, dataUri };


I have no idea why this is happening, if anyone has experienced this I would be grateful.


Solution

  • First of all fileFilter is not a key of diskStorage, it's a key of the multer itself. Also, you've declared function inside the else. So checking filetype using multer should be like this:

    const multer = require("multer");
    const DatauriParser = require("datauri/parser");
    const path = require("path");
    const fs = require("fs");
    const dontenv = require("dotenv");
    dontenv.config({ path: "config.env" });
    
    let storage;
    let dataUri;
    const parser = new DatauriParser();
    
    if (process.env.STORAGE_TYPE == "cloud") {
        storage = multer.memoryStorage();
        dataUri = (req) =>
            parser.format(
                path.extname(req.file.originalname).toString(),
                req.file.buffer
            );
    } else {
        dataUri = {};
        storage = multer.diskStorage({
            destination: function (req, file, cb) {
                var directory = process.env.STORAGE;
                if (!fs.existsSync(directory)) {
                    fs.mkdirSync(directory);
                }
                cb(null, directory);
            },
            filename: function (req, file, cb) {
                cb(
                    null,
                    file.fieldname +
                    "-" +
                    Date.now() +
                    "." +
                    file.originalname.split(".")[1]
                );
            }
        });
        // to Check File TYpe
    }
    
    function checkFileType(file, cb) {
        // Allowed ext
        const filetypes = /jpeg|jpg|png|gif/;
        // Check ext
        const extname = filetypes.test(
            path.extname(file.originalname).toString()
        );
        // Check mime
        const mimetype = filetypes.test(file.mimetype);
    
        if (mimetype && extname) {
            return cb(null, true);
        } else {
            cb("Error: Images Only!");
        }
    }
    
    const multerUploads = multer({
        storage: storage,
        fileFilter: function (req, file, cb) {
            checkFileType(file, cb);
        },
    }).single("imageUrl");
    
    module.exports = { multerUploads, dataUri };