Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to multer 1.3.1 and automatically clean uploaded files #4704

Merged
merged 5 commits into from
Jul 23, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions admin/server/app/createDynamicRouter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var bodyParser = require('body-parser');
var express = require('express');
var multer = require('multer');

var uploads = require('../../../lib/uploads');

module.exports = function createDynamicRouter (keystone) {
// ensure keystone nav has been initialised
Expand All @@ -17,7 +18,7 @@ module.exports = function createDynamicRouter (keystone) {
// Use bodyParser and multer to parse request bodies and file uploads
router.use(bodyParser.json({}));
router.use(bodyParser.urlencoded({ extended: true }));
router.use(multer({ includeEmptyFields: true }));
uploads.configure(router);

// Bind the request to the keystone instance
router.use(function (req, res, next) {
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var Keystone = function () {
'module root': moduleRoot,
'frame guard': 'sameorigin',
'cache admin bundles': true,
'handle uploads': true,
};
this._redirects = {};

Expand Down
41 changes: 41 additions & 0 deletions lib/uploads.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
var fs = require('fs');
var multer = require('multer');
var os = require('os');

function handleUploadedFiles (req, res, next) {
if (!req.files || !Array.isArray(req.files)) return next();
var originalFiles = req.files;
var files = {};
originalFiles.forEach(function (i) {
if (i.fieldname in files) {
var tmp = files[i.fieldname];
files[i.fieldname] = [tmp];
}
if (Array.isArray(files[i.fieldname])) {
files[i.fieldname].push(i);
} else {
files[i.fieldname] = i;
}
});
req.files = files;
var cleanup = function () {
originalFiles.forEach(function (i) {
if (i.path) {
fs.unlink(i.path, function () {});
}
});
};
res.on('close', cleanup);
res.on('finish', cleanup);
next();
};

exports.handleUploadedFiles = handleUploadedFiles;

exports.configure = function (app, options) {
var upload = multer(options || {
dest: os.tmpdir(),
});
app.use(upload.any());
app.use(handleUploadedFiles);
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"moment": "2.19.3",
"mongoose": "4.13.14",
"morgan": "1.9.0",
"multer": "0.1.8",
"multer": "1.3.1",
"numeral": "2.0.6",
"object-assign": "4.1.1",
"qs": "6.5.2",
Expand Down
11 changes: 6 additions & 5 deletions server/bindBodyParser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var multer = require('multer');
var bodyParser = require('body-parser');

module.exports = function bindIPRestrictions (keystone, app) {
var uploads = require('../lib/uploads');

module.exports = function bindBodyParser (keystone, app) {
// Set up body options and cookie parser
var bodyParserParams = {};
if (keystone.get('file limit')) {
Expand All @@ -10,7 +11,7 @@ module.exports = function bindIPRestrictions (keystone, app) {
app.use(bodyParser.json(bodyParserParams));
bodyParserParams.extended = true;
app.use(bodyParser.urlencoded(bodyParserParams));
app.use(multer({
includeEmptyFields: true,
}));
if (keystone.get('handle uploads')) {
uploads.configure(app, keystone.get('multer options'));
}
};
Loading