0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00
ghost/core/server/web/middleware/validation/blog-icon.js
kirrg001 fc5b4dd934 Moved image utils to lib/image
refs #9178

- i am not super happy about `const imageLib = require('../lib/image')`
- i don't really like the name `imageLib`
- but i had no better idea 😃
- if we use the same name in the whole project, it's very easy to rename the folder or the variable
2017-12-14 20:46:53 +01:00

53 lines
2.2 KiB
JavaScript

var config = require('../../../config'),
common = require('../../../lib/common'),
imageLib = require('../../../lib/image'),
validIconFileSize;
validIconFileSize = function validIconFileSize(size) {
return size / 1024 <= 100 ? true : false;
};
module.exports = function blogIcon() {
// we checked for a valid image file, now we need to do validations for blog icons
return function blogIconValidation(req, res, next) {
var iconExtensions = (config.get('uploads').icons && config.get('uploads').icons.extensions) || [];
// CASE: file should not be larger than 100kb
if (!validIconFileSize(req.file.size)) {
return next(new common.errors.ValidationError({
message: common.i18n.t('errors.api.icons.invalidFile', {extensions: iconExtensions})
}));
}
return imageLib.blogIcon.getIconDimensions(req.file.path).then(function (response) {
// save the image dimensions in new property for file
req.file.dimensions = response;
// CASE: file needs to be a square
if (req.file.dimensions.width !== req.file.dimensions.height) {
return next(new common.errors.ValidationError({
message: common.i18n.t('errors.api.icons.invalidFile', {extensions: iconExtensions})
}));
}
// CASE: icon needs to be bigger than or equal to 60px
// .ico files can contain multiple sizes, we need at least a minimum of 60px (16px is ok, as long as 60px are present as well)
if (req.file.dimensions.width < 60) {
return next(new common.errors.ValidationError({
message: common.i18n.t('errors.api.icons.invalidFile', {extensions: iconExtensions})
}));
}
// CASE: icon needs to be smaller than or equal to 1000px
if (req.file.dimensions.width > 1000) {
return next(new common.errors.ValidationError({
message: common.i18n.t('errors.api.icons.invalidFile', {extensions: iconExtensions})
}));
}
next();
}).catch(function (err) {
next(err);
});
};
};