mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-20 17:32:15 +03:00
e19e91044d
refs #8221, closes #7688, refs #7558 🙇 Improve meta data publisher logo behaviour This is a follow-up PR for #8285. Reasons: The code changes of #8285 caused error messages when falling back to the default `favicon.ico`, as the `image-size` tool doesn't support `ico` files. This PR takes the logic to decide which logo needs to be listed in our schema into a new fn `blog_logo.js`. There we have now three decisions: 1. If we have a publication **logo**, we'll take that one 2. If we have no publication logo, but an **icon** we'll use this one. 3. If we have none of the above things, we fall back to our default `favicon.ico` Additional, we're hard coding image dimensions for whenever the logo is an `.ico` file and built and extra decision to not call `image-size` when the dimension are already given. I will create another follow-up PR, which checks the extension type for the file and offers it as a util. 🛠 Blog icon util refs #7688 Serve functionality around the blog icon in its own util: - getIconDimensions -> async function that takes the filepath of on ico file and returns its dimensions - isIcoImageType -> returns true if file has `.ico` extension - getIconType -> returns icon-type (`x-icon` or `png`) - getIconUrl -> returns the absolut or relativ URL for the favicon: `[subdirectory or not]favicon.[ico or png]` 📖 Get .ico sizes for meta data & logo improvement refs #7558 refs #8221 Use the new `blogIconUtil` in meta data to fetch the dimensions of `.ico` files. Improvements for `publisher.logo`: We're now returning a hard-coded 'faked' image dimensions value to render an `imageObject` and prevent error our schema (Google structured data). As soon as an image (`.ico` or non-`.ico`) is too large, but - in case of non-`.ico` - a square format, be set the image-dimensions to 60px width and height. This reduces the chances of getting constantly error messages from Googles' webmaster tools. - add getIconPath util
76 lines
3.0 KiB
JavaScript
76 lines
3.0 KiB
JavaScript
var errors = require('../../errors'),
|
|
config = require('../../config'),
|
|
Promise = require('bluebird'),
|
|
sizeOf = require('image-size'),
|
|
i18n = require('../../i18n'),
|
|
blogIconUtils = require('../../utils/blog-icon'),
|
|
validIconSize,
|
|
getIconDimensions;
|
|
|
|
validIconSize = function validIconSize(size) {
|
|
return size / 1024 <= 100 ? true : false;
|
|
};
|
|
|
|
getIconDimensions = function getIconDimensions(icon) {
|
|
return new Promise(function getImageSize(resolve, reject) {
|
|
if (blogIconUtils.isIcoImageType(icon.name)) {
|
|
blogIconUtils.getIconDimensions(icon.path).then(function (response) {
|
|
return resolve({
|
|
width: response.width,
|
|
height: response.height
|
|
});
|
|
}).catch(function (err) {
|
|
return reject(err);
|
|
});
|
|
} else {
|
|
sizeOf(icon.path, function (err, response) {
|
|
if (err) {
|
|
return reject(new errors.ValidationError({message: i18n.t('errors.api.icons.couldNotGetSize', {file: icon.name, error: err.message})}));
|
|
}
|
|
|
|
return resolve({
|
|
width: response.width,
|
|
height: response.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.ValidationError({message: i18n.t('errors.api.icons.invalidFile', {extensions: iconExtensions})}));
|
|
}
|
|
|
|
return getIconDimensions(req.file).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 errors.ValidationError({message: i18n.t('errors.api.icons.invalidFile', {extensions: iconExtensions})}));
|
|
}
|
|
|
|
// CASE: icon needs to be bigger than 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 errors.ValidationError({message: i18n.t('errors.api.icons.invalidFile', {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.invalidFile', {extensions: iconExtensions})}));
|
|
}
|
|
|
|
next();
|
|
}).catch(function (err) {
|
|
next(err);
|
|
});
|
|
};
|
|
};
|