Ghost/core/server/utils/cached-image-size-from-url.js
Katharina Irrgang 64d2a94073 🐛 fix image size timeout (#8283)
closes #8041

- destroy socket is required, see https://nodejs.org/api/http.html#http_server_settimeout_msecs_callback
- optimise error messages in general
- make timeouts configureable
2017-04-05 21:58:26 +01:00

38 lines
1.2 KiB
JavaScript

var Promise = require('bluebird'),
size = require('./image-size-from-url'),
logging = require('../logging'),
getImageSizeFromUrl = size.getImageSizeFromUrl,
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 getImageSizeFromUrl(url).then(function (res) {
imageSizeCache[url] = res;
return Promise.resolve(imageSizeCache[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;