Ghost/core/server/utils/cached-image-size-from-url.js
Aileen Nowak a45a91c906 🐛 Fix invalid image URLs not being cached and causing timeouts (#8986)
refs #8868

* 📐  Use request util in image-size
- swapped the usage of `got` for requests with the request util

* 💄  Use catch predicates
- Uses catch predicates instead of conditionals in `getImageSizeFromUrl`
- Return `NotFoundError` if applicable in `getImageSizeFromFilePath` as the caller function `cachedImageSizeFromUrl` is differentiating those between this error and others.

* 🐛  Fixed ImageObject URL & simplify no protocol URL logic

- Using `ImageObject` as a global var resulted in having the `url` property being the same for all requests coming in.
- The logic that checked for an existing protocol (e. g. gravatar URLs) was overly complicated. Refactored it to be more simple.
- Passing the correct value to `fetchDimensionsFromBuffer` as the population of `imageObject.url` happens there. These are used in our structured data and need to be full URLs (in case of locally stored files) or the original URL (in case of URLs missing the protocol)
- Added two more debug logs in `getCachedImageSizeFromUrl` so it's logged when an image is added to the cache even tho it was returned as error.

* 👀  Differentiate error codes between request and storage

* 🔥  Remove not needed `Promise.resolve()`

We're always resolving the result in `getCachedImageSizeFromUrl`, so there's no need to return the values with a `Promise.resolve()`. The caller fn uses waits for the Promises to be fulfilled.

* ☂️  Wrap already rejected predicate errors in catch all

* Use errorDetails instead of context

* ☂️  Support /assets/ image paths

- adds a guard that checks the image URL for `/assets/` in the beginning and passes a completed URL to the request util to try and fetch the image size
- adds tests
2017-09-12 12:53:18 +01:00

50 lines
1.6 KiB
JavaScript

var debug = require('ghost-ignition').debug('utils:image-size-cache'),
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;
debug('Cached image:', url);
return imageSizeCache[url];
}).catch(errors.NotFoundError, function () {
debug('Cached image (not found):', url);
// in case of error we just attach the url
imageSizeCache[url] = url;
return imageSizeCache[url];
}).catch(function (err) {
debug('Cached image (error):', url);
logging.error(err);
// in case of error we just attach the url
imageSizeCache[url] = url;
return imageSizeCache[url];
});
}
debug('Read image from cache:', url);
// returns image size from cache
return imageSizeCache[url];
}
module.exports = getCachedImageSizeFromUrl;