mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
refs #7688 Adds an `uploads/icon/` endpoint to the api route to get a seperate entry point for blog icon validations. The blog icon validation will specifically check for images which have icon extensions (`.ico` & `.png`) and throw errors if: - the icon file size is too big (>100kb) - the icon is not a squaer - the icon size is smaller than 32px - the icon size is larger than 1000px - the icon is not `.ico` or `.png` extension TODOs for this PR: - [X] get image dimensions - [X] validate for image - [X] size - [X] form (must be square) - [X] type - [X] dimenstion (min 32px and max 1,000px) - [X] return appropriate error messages - [X] write tests -------------------- TODOs for #7688: - [X] Figure out, which favicon should be used (uploaded or default) -> #7713 - [ ] Serve and redirect the favicon for any browser requests, incl. redirects -> #7700 [WIP] - [X] Upload favicon via `general/settings` and implement basic admin validations -> TryGhost/Ghost-Admin#397 - [X] Build server side validations -> this PR
90 lines
3.7 KiB
JavaScript
90 lines
3.7 KiB
JavaScript
var errors = require('../../errors'),
|
|
config = require('../../config'),
|
|
ICO = require('icojs'),
|
|
fs = require('fs'),
|
|
Promise = require('bluebird'),
|
|
sizeOf = require('image-size'),
|
|
i18n = require('../../i18n'),
|
|
_ = require('lodash'),
|
|
validIconSize,
|
|
getIconDimensions;
|
|
|
|
validIconSize = function validIconSize(size) {
|
|
return size / 1024 <= 100 ? true : false;
|
|
};
|
|
|
|
getIconDimensions = function getIconDimensions(icon) {
|
|
return new Promise(function getImageSize(resolve, reject) {
|
|
var arrayBuffer;
|
|
|
|
// image-size doesn't support .ico files
|
|
if (icon.name.match(/.ico$/i)) {
|
|
arrayBuffer = new Uint8Array(fs.readFileSync(icon.path)).buffer;
|
|
ICO.parse(arrayBuffer).then(function (result, error) {
|
|
if (error) {
|
|
return reject(new errors.ValidationError({message: i18n.t('errors.api.icons.couldNotGetSize', {file: icon.name, error: error.message})}));
|
|
}
|
|
|
|
// CASE: ico file contains only one size
|
|
if (result.length === 1) {
|
|
return resolve({
|
|
width: result[0].width,
|
|
height: result[0].height
|
|
});
|
|
} else {
|
|
// CASE: ico file contains multiple sizes, return only the max size
|
|
return resolve({
|
|
width: _.maxBy(result, function (w) {return w.width;}).width,
|
|
height: _.maxBy(result, function (h) {return h.height;}).height
|
|
});
|
|
}
|
|
});
|
|
} else {
|
|
sizeOf(icon.path, function (err, dimensions) {
|
|
if (err) {
|
|
return reject(new errors.ValidationError({message: i18n.t('errors.api.icons.couldNotGetSize', {file: icon.name, error: err.message})}));
|
|
}
|
|
|
|
return resolve({
|
|
width: dimensions.width,
|
|
height: dimensions.height
|
|
});
|
|
});
|
|
}
|
|
});
|
|
};
|
|
|
|
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 (!validIconSize(req.file.size)) {
|
|
return next(new errors.RequestEntityTooLargeError({message: i18n.t('errors.api.icons.fileSizeTooLarge', {extensions: iconExtensions})}));
|
|
}
|
|
|
|
return getIconDimensions(req.file).then(function (dimensions) {
|
|
// save the image dimensions in new property for file
|
|
req.file.dimensions = dimensions;
|
|
|
|
// CASE: file needs to be a square
|
|
if (req.file.dimensions.width !== req.file.dimensions.height) {
|
|
return next(new errors.ValidationError({message: i18n.t('errors.api.icons.iconNotSquare', {extensions: iconExtensions})}));
|
|
}
|
|
|
|
// CASE: icon needs to be bigger than 32px
|
|
// .ico files can contain multiple sizes, we need at least a minimum of 32px (16px is ok, as long as 32px are present as well)
|
|
if (req.file.dimensions.width < 32) {
|
|
return next(new errors.ValidationError({message: i18n.t('errors.api.icons.fileTooSmall', {extensions: iconExtensions})}));
|
|
}
|
|
|
|
// CASE: icon needs to be smaller than 1000px
|
|
if (req.file.dimensions.width > 1000) {
|
|
return next(new errors.ValidationError({message: i18n.t('errors.api.icons.fileTooLarge', {extensions: iconExtensions})}));
|
|
}
|
|
|
|
next();
|
|
});
|
|
};
|
|
};
|