Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js and Multer - Handle the destination of the uploaded file in callback function (req,res)

I'm new to Node.js and I ran into a simple problem lately.

I'm using multer module to upload images. In my web app all the users have a unique id, and I want the uploaded images to be stored in a directory to be named based on their id.

Example:

.public/uploads/3454367856437534

Here is a part of my routes.js file:

// load multer to handle image uploads
var multer  = require('multer');
var saveDir = multer({
  dest: './public/uploads/' + req.user._id, //error, we can not access this id from here
  onFileUploadStart: function (file) { 
  return utils.validateImage(file); //validates the image file type
  }
});

module.exports = function(app, passport) {

app.post('/', saveDir, function(req, res) {
                id     : req.user._id,  //here i can access the user id
    });
});

}

How can I access req.user._id attribute outside the callback function(req, res), so I can use it with multer, to generate the proper directory based on the id?

EDIT Here is what I have tried and didn't work:

module.exports = function(app, passport) {

app.post('/', function(req, res) {
    app.use(multer({
        dest: './public/uploads/' + req.user._id
    }));
});

}
like image 204
Light Flow Avatar asked Nov 30 '14 12:11

Light Flow


1 Answers

Update

Quite a few things have changed since I posted the original answer.

With multer 1.2.1.

  1. You need to use DiskStorage to specify where & how of the stored file.
  2. By default, multer will use the operating system's default directory. In our case, since we are particular about the location. We need to ensure that the folder exists before we could save the file over there.

Note: You are responsible for creating the directory when providing destination as a function.

More here

'use strict';

let multer = require('multer');
let fs = require('fs-extra');

let upload = multer({
  storage: multer.diskStorage({
    destination: (req, file, callback) => {
      let userId = req.user._id;
      let path = `./public/uploads//${userId}`;
      fs.mkdirsSync(path);
      callback(null, path);
    },
    filename: (req, file, callback) => {
      //originalname is the uploaded file's name with extn
      callback(null, file.originalname);
    }
  })
});

app.post('/', upload.single('file'), (req, res) => {
  res.status(200).send();
});

fs-extra for creating directory, just in case it doesn't exists

Original answer

You can use changeDest

Function to rename the directory in which to place uploaded files.

It is available from v0.1.8

app.post('/', multer({
dest: './public/uploads/',
changeDest: function(dest, req, res) {
    var newDestination = dest + req.user._id;
    var stat = null;
    try {
        stat = fs.statSync(newDestination);
    } catch (err) {
        fs.mkdirSync(newDestination);
    }
    if (stat && !stat.isDirectory()) {
        throw new Error('Directory cannot be created because an inode of a different type exists at "' + dest + '"');
    }
    return newDestination
}
}), function(req, res) {
     //set your response
});
like image 151
Sridhar Avatar answered Oct 28 '22 22:10

Sridhar