diff --git a/src/middleware/index.js b/src/middleware/index.js index f3c42d11d6..80a5f568c6 100644 --- a/src/middleware/index.js +++ b/src/middleware/index.js @@ -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); +}; diff --git a/src/routes/helpers.js b/src/routes/helpers.js index 0f257410fe..aff46467ec 100644 --- a/src/routes/helpers.js +++ b/src/routes/helpers.js @@ -60,6 +60,7 @@ helpers.setupApiRoute = function (...args) { middleware.registrationComplete, middleware.pluginHooks, middleware.logApiUsage, + middleware.handleMultipart, ...middlewares, ];