With version 1.0.0., the destination directory is not automatically created any more if it doesn't exist.
If it doesn't, then the request is not being processed any more (I can't check for "err" and "req.file" in the callback). There is no response.
Am I missing something or is this a bug?
I just saw in the source code that the directory should be created.. I'll have a closer look.
So depending on how you define the destination (dest string or storage functions), the directory is created or not.
Shouldn't the dir also be created when using disk storage?
So this is how it currently works. If the destination is specified as a string, the directory will be created. If the directory is specified as function, you are responsible for creating the directory.
I guess that this should be clearer in the documentation, or maybe we should change it. I'm not sure if I want us to call out to mkdirp for each file uploaded though, which we'll have to do if we change it to work with the functions.
Maybe the best approach here is to never create the directory, and provide a really good and clear error message when the directory isn't there. (This can be done by checking the error for `err.code === 'ENOENT' and then returning a better error.)
@jpfluger, do you have any comments?
I don't get the 'err' though. Somehow the route is stopped and I don't get a response. The function with the error callback is not executed.
This is the code I tested:
var multer = require('multer');
var upload = multer({
storage: multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'uploads/');
},
filename: function (req, file, cb) {
cb(null, Date.now() + '_' + file.originalname);
}
})
});
var uploadFile = upload.single('file');
app.post('/upload', function (req, res, next) {
console.log('ok');
uploadFile(req, res, function (err) {
console.log('not ok');
if (err) return next(err);
res.status(204).end();
});
});
The console.log('not ok'); is not printed. I can only cancel the request in the client.
@dsine-de - most likely a bug that happens when the new dir is invalid and nodes tries to process.
@LinusU - I think leave it as-is for now but can this issue's error be detected anyway? If we stay as-is, would need to update the documentation to show how user can use mkdirp him or herself:
var multer = require('multer');
var mkdirp = require('mkdirp');
var upload = multer({
storage: multer.diskStorage({
destination: function (req, file, cb) {
var dest = 'uploads/';
mkdirp.sync(dest);
cb(null, dest);
}
})
});
Awesome! Our first real bug in 1.0 :)
This is now fixed in version 1.0.1 :sparkler:
This is now documented as well, I'm considering this to be closed now.
Great work, thanks!
Most helpful comment
@dsine-de - most likely a bug that happens when the new dir is invalid and nodes tries to process.
@LinusU - I think leave it as-is for now but can this issue's error be detected anyway? If we stay as-is, would need to update the documentation to show how user can use mkdirp him or herself: