Search code examples
node.jsmultersharp

Custom Multer storage - Illegal operation when using Sharp


I'm using Multer together with Sharp to store images uploaded as part of an HTML form. I want to resize and transform the images before storing them on the disk and found this thread about how to do just that.

I thought I had set-up everything correctly, but when I try and upload an image I get:

Error: EISDIR: illegal operation on a directory, open 'C:\...\uploads'

Below is my code:

Routes.js:

var multer = require('multer');
var customStorage = require(path.join(__dirname, 'customStorage.js'));

var upload = multer({
    storage: new customStorage({
        destination: function (req, file, cb) {
            cb(null, path.join(__dirname, 'uploads'));
        },
        filename: function (req, file, cb) {
            cb(null, Date.now());
        }
    }),
    limits: { fileSize: 5000000 }
});
...
app.use('/upload', upload.single('file'), (req, res) => { ... });

customStorage.js:

var fs = require('fs');
var sharp = require('sharp');

function getDestination (req, file, cb) {
    cb(null, '/dev/null'); // >Implying I use loonix
};

function customStorage (opts) {
    this.getDestination = (opts.destination || getDestination);
};

customStorage.prototype._handleFile = function _handleFile(req, file, cb) {
    this.getDestination(req, file, function (err, path) {
        if (err) return cb(err);

        var outStream = fs.createWriteStream(path);
        var transform = sharp().resize(200, 200).background('white').embed().jpeg();

        file.stream.pipe(transform).pipe(outStream);
        outStream.on('error', cb);
        outStream.on('finish', function () {
            cb(null, {
                path: path,
                size: outStream.bytesWritten
            });
        });
    });
};

customStorage.prototype._removeFile = function _removeFile(req, file, cb) {
    fs.unlink(file.path, cb);
};

module.exports = function (opts) {
    return new customStorage(opts);
};

Solution

  • The error Error: EISDIR: illegal operation on a directory in this context indicates that you are setting Multer's destination to a directory when it should be the name of the destination file.

    The destination is set in the line cb(null, path.join(__dirname, 'uploads')); in Routes.js. If you change this line to something like cb(null, path.join(__dirname, 'myDirectory\\mySubdirectory\\', myFilename + '.jpg')), it will work.