0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00
ghost/core/server/middleware/validation/upload.js
Hannah Wolfe b0af496c98 💄 Ensure all middleware use named functions (#7434)
no issue

- anonymous functions are hard to debug in memory traces etc
- having anonymous middleware functions makes it hard to inspect or debug the middleware stack (something I like to do)
- these 2 are the only ones atm, including all 3rd party middleware
2016-09-26 11:10:44 +02:00

30 lines
1.2 KiB
JavaScript

var apiUtils = require('../../api/utils'),
errors = require('../../errors'),
config = require('../../config'),
i18n = require('../../i18n');
module.exports = function upload(options) {
var type = options.type;
// if we finish the data/importer logic, we forward the request to the specified importer
return function uploadValidation(req, res, next) {
var extensions = (config.get('uploads')[type] && config.get('uploads')[type].extensions) || [],
contentTypes = (config.get('uploads')[type] && config.get('uploads')[type].contentTypes) || [];
req.file = req.file || {};
req.file.name = req.file.originalname;
req.file.type = req.file.mimetype;
// Check if a file was provided
if (!apiUtils.checkFileExists(req.file)) {
return next(new errors.NoPermissionError(i18n.t('errors.api.' + type + '.missingFile')));
}
// Check if the file is valid
if (!apiUtils.checkFileIsValid(req.file, contentTypes, extensions)) {
return next(new errors.UnsupportedMediaTypeError(i18n.t('errors.api.' + type + '.invalidFile', {extensions: extensions})));
}
next();
};
};