mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-18 16:01:40 +03:00
1fe87a6110
no issue - test cases were trying to fetch image sizes for `localhost:port/favicon.ico` but no server is running so they time out - stub the `getImageSizeFromUrl` method so it resolves instantly
41 lines
1.3 KiB
JavaScript
41 lines
1.3 KiB
JavaScript
var Promise = require('bluebird'),
|
|
imageSize = require('./image-size'),
|
|
logging = require('../logging'),
|
|
errors = require('../errors'),
|
|
imageSizeCache = {};
|
|
|
|
/**
|
|
* Get cached image size from URL
|
|
* Always returns {object} imageSizeCache
|
|
* @param {string} url
|
|
* @returns {Promise<Object>} imageSizeCache
|
|
* @description Takes a url and returns image width and height from cache if available.
|
|
* If not in cache, `getImageSizeFromUrl` is called and returns the dimensions in a Promise.
|
|
*/
|
|
function getCachedImageSizeFromUrl(url) {
|
|
if (!url || url === undefined || url === null) {
|
|
return;
|
|
}
|
|
|
|
// image size is not in cache
|
|
if (!imageSizeCache[url]) {
|
|
return imageSize.getImageSizeFromUrl(url).then(function (res) {
|
|
imageSizeCache[url] = res;
|
|
|
|
return Promise.resolve(imageSizeCache[url]);
|
|
}).catch(errors.NotFoundError, function () {
|
|
// in case of error we just attach the url
|
|
return Promise.resolve(imageSizeCache[url] = url);
|
|
}).catch(function (err) {
|
|
logging.error(err);
|
|
|
|
// in case of error we just attach the url
|
|
return Promise.resolve(imageSizeCache[url] = url);
|
|
});
|
|
}
|
|
// returns image size from cache
|
|
return Promise.resolve(imageSizeCache[url]);
|
|
}
|
|
|
|
module.exports = getCachedImageSizeFromUrl;
|