mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-17 13:31:39 +03:00
82597080be
refs #9178 - `checkFileExists` and `checkFileIsValid` where dirty required from web/middleware - these two functions are only used in the target middleware - let's move them
36 lines
1.3 KiB
JavaScript
36 lines
1.3 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../../../lib/common'),
|
|
config = require('../../../config'),
|
|
localUtils = require('../../utils');
|
|
|
|
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 (!localUtils.checkFileExists(req.file)) {
|
|
return next(new common.errors.NoPermissionError({
|
|
message: common.i18n.t('errors.api.' + type + '.missingFile')
|
|
}));
|
|
}
|
|
|
|
// Check if the file is valid
|
|
if (!localUtils.checkFileIsValid(req.file, contentTypes, extensions)) {
|
|
return next(new common.errors.UnsupportedMediaTypeError({
|
|
message: common.i18n.t('errors.api.' + type + '.invalidFile', {extensions: extensions})
|
|
}));
|
|
}
|
|
|
|
next();
|
|
};
|
|
};
|