Ghost/core/server/middleware/validation/upload.js
Katharina Irrgang 663b410fd4 feature: upload validation middleware (#7208)
no issue

- Source out validation logic into a upload validation middleware for all upload types (csv, image, subscribers). This unit can be later used for Ghost 1.0 as a pre validation core unit. 
- More usage of route tests than controller tests. These are use case tests, a use case only changes if the product changes
2016-08-18 20:25:51 +01:00

31 lines
1.1 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 (req, res, next) {
var extensions = (config.uploads[type] && config.uploads[type].extensions) || [],
contentTypes = (config.uploads[type] && config.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();
};
};