Ghost/core/server/utils/blog-icon.js
Aileen Nowak b086b432ad Dumped icojs and use image-size for .ico (#8888)
refs #8868

The `image-size` library supports now `.ico` files, which means there is no longer need to use the `icojs` library.
- removes unnecessary `icojs` dependency
- refactors `getIconDimensions` fn in blog icon util to fetch image sizes synchronus
- removes unnecessary `getIconDimensions` fn in blog icon validation, as there is no longer need to use different image size fn for different file extensions, and uses `getIconDimensions` from blog util fn instead.
- updates and adds more tests
2017-08-17 17:44:05 +01:00

120 lines
4.2 KiB
JavaScript

var sizeOf = require('image-size'),
errors = require('../errors'),
url = require('./url'),
Promise = require('bluebird'),
i18n = require('../i18n'),
settingsCache = require('../settings/cache'),
_ = require('lodash'),
path = require('path'),
config = require('../config'),
utils = require('../utils'),
getIconDimensions,
isIcoImageType,
getIconType,
getIconUrl,
getIconPath;
/**
* Get dimensions for ico file from its real file storage path
* Always returns {object} getIconDimensions
* @param {string} path
* @returns {Promise<Object>} getIconDimensions
* @description Takes a file path and returns ico width and height.
*/
getIconDimensions = function getIconDimensions(path) {
return new Promise(function getIconSize(resolve, reject) {
var dimensions;
try {
dimensions = sizeOf(path);
if (dimensions.images) {
dimensions.width = _.maxBy(dimensions.images, function (w) {return w.width;}).width;
dimensions.height = _.maxBy(dimensions.images, function (h) {return h.height;}).height;
}
return resolve({
width: dimensions.width,
height: dimensions.height
});
} catch (err) {
return reject(new errors.ValidationError({message: i18n.t('errors.utils.blogIcon.error', {file: path, error: err.message})}));
}
});
};
/**
* Check if file is `.ico` extension
* Always returns {object} isIcoImageType
* @param {string} icon
* @returns {Boolean} true if submitted path is .ico file
* @description Takes a path and returns boolean value.
*/
isIcoImageType = function isIcoImageType(icon) {
var blogIcon = icon || settingsCache.get('icon');
return blogIcon.match(/.ico$/i) ? true : false;
};
/**
* Check if file is `.ico` extension
* Always returns {object} isIcoImageType
* @param {string} icon
* @returns {Boolean} true if submitted path is .ico file
* @description Takes a path and returns boolean value.
*/
getIconType = function getIconType(icon) {
var blogIcon = icon || settingsCache.get('icon');
return isIcoImageType(blogIcon) ? 'x-icon' : 'png';
};
/**
* Return URL for Blog icon: [subdirectory or not]favicon.[ico or png]
* Always returns {string} getIconUrl
* @returns {string} [subdirectory or not]favicon.[ico or png]
* @description Checks if we have a custom uploaded icon and the extension of it. If no custom uploaded icon
* exists, we're returning the default `favicon.ico`
*/
getIconUrl = function getIconUrl(absolut) {
var blogIcon = settingsCache.get('icon');
if (absolut) {
if (blogIcon) {
return isIcoImageType(blogIcon) ? url.urlFor({relativeUrl: '/favicon.ico'}, true) : url.urlFor({relativeUrl: '/favicon.png'}, true);
} else {
return url.urlFor({relativeUrl: '/favicon.ico'}, true);
}
} else {
if (blogIcon) {
return isIcoImageType(blogIcon) ? url.urlFor({relativeUrl: '/favicon.ico'}) : url.urlFor({relativeUrl: '/favicon.png'});
} else {
return url.urlFor({relativeUrl: '/favicon.ico'});
}
}
};
/**
* Return path for Blog icon without [subdirectory]/content/image prefix
* Always returns {string} getIconPath
* @returns {string} physical storage path of icon
* @description Checks if we have a custom uploaded icon. If no custom uploaded icon
* exists, we're returning the default `favicon.ico`
*/
getIconPath = function getIconPath() {
var blogIcon = settingsCache.get('icon');
if (blogIcon) {
// The '/' in urlJoin is necessary to add the '/' to `content/images`, if no subdirectory is setup
return blogIcon.replace(new RegExp('^' + utils.url.urlJoin(utils.url.getSubdir(), '/', utils.url.STATIC_IMAGE_URL_PREFIX)), '');
} else {
return path.join(config.get('paths:publicFilePath'), 'favicon.ico');
}
};
module.exports.getIconDimensions = getIconDimensions;
module.exports.isIcoImageType = isIcoImageType;
module.exports.getIconUrl = getIconUrl;
module.exports.getIconPath = getIconPath;
module.exports.getIconType = getIconType;