mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-18 05:52:40 +03:00
663b410fd4
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
31 lines
1.1 KiB
JavaScript
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();
|
|
};
|
|
};
|