feat: middleware.handleMultipart, applies on API routes — invokes multipart middleware based on content-type header

isekai-main
Julian Lam 2 years ago
parent eca283020f
commit bcc4b82cc6

@ -6,6 +6,7 @@ const validator = require('validator');
const nconf = require('nconf');
const toobusy = require('toobusy-js');
const util = require('util');
const multipart = require('connect-multiparty');
const { csrfSynchronisedProtection } = require('./csrf');
const plugins = require('../plugins');
@ -27,6 +28,7 @@ const delayCache = cacheCreate({
ttl: 1000 * 60,
max: 200,
});
const multipartMiddleware = multipart();
const middleware = module.exports;
@ -284,3 +286,14 @@ middleware.checkRequired = function (fields, req, res, next) {
controllers.helpers.formatApiResponse(400, res, new Error(`[[error:required-parameters-missing, ${missing.join(' ')}]]`));
};
middleware.handleMultipart = (req, res, next) => {
// Applies multipart handler on applicable content-type
const { 'content-type': contentType } = req.headers;
if (contentType && !contentType.startsWith('multipart/form-data')) {
return next();
}
multipartMiddleware(req, res, next);
};

@ -60,6 +60,7 @@ helpers.setupApiRoute = function (...args) {
middleware.registrationComplete,
middleware.pluginHooks,
middleware.logApiUsage,
middleware.handleMultipart,
...middlewares,
];

Loading…
Cancel
Save