0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-24 23:48:13 -05:00
ghost/core/server/web/middleware/validation/upload.js
Nazar Gargol 0faf89b5ab Added ability to resize and compress images on upload (#9837)
refs #4453

* On by default

* Added config to disable resizing

* Added basic image optimization processing

* Added dep: sharp (optional dep)

* Added resize middleware

* Take care of rotation based on EXIF information

* Removed all meta data from optimised image

* Added handling if sharp could not get installed

* Do not read ext twice - optimisation

* Do not call sharp if config is disabled

* Do not remove the original image which was uploaded (store 2 images)

* Support of `req.files` for internal logic

* Disabled cache to enable file removal on Windows
2018-08-30 17:30:36 +01:00

36 lines
1.4 KiB
JavaScript

const path = require('path');
const common = require('../../../lib/common');
const config = require('../../../config');
const 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')
}));
}
req.file.ext = path.extname(req.file.name).toLowerCase();
// 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();
};
};