mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
feat: middleware.handleMultipart, applies on API routes — invokes multipart middleware based on content-type header
This commit is contained in:
@@ -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,
|
||||
];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user