diff --git a/ghost/core/core/server/lib/image/cached-image-size-from-url.js b/ghost/core/core/server/lib/image/cached-image-size-from-url.js index 61be09ae59..bec2d57d2f 100644 --- a/ghost/core/core/server/lib/image/cached-image-size-from-url.js +++ b/ghost/core/core/server/lib/image/cached-image-size-from-url.js @@ -1,10 +1,17 @@ const debug = require('@tryghost/debug')('utils:image-size-cache'); const errors = require('@tryghost/errors'); const logging = require('@tryghost/logging'); + class CachedImageSizeFromUrl { - constructor({imageSize}) { + /** + * + * @param {Object} options + * @param {Object} options.imageSize - instance with "getImageSizeFromUrl" method + * @param {Object} options.cache - cache store instance + */ + constructor({imageSize, cache}) { this.imageSize = imageSize; - this.cache = new Map(); + this.cache = cache; } /** @@ -19,31 +26,32 @@ class CachedImageSizeFromUrl { if (!url || url === undefined || url === null) { return; } - + // image size is not in cache if (!this.cache.has(url)) { return this.imageSize.getImageSizeFromUrl(url).then((res) => { this.cache.set(url, res); - + debug('Cached image:', url); - + return this.cache.get(url); }).catch(errors.NotFoundError, () => { debug('Cached image (not found):', url); // in case of error we just attach the url this.cache.set(url, url); - + return this.cache.get(url); }).catch((err) => { debug('Cached image (error):', url); logging.error(err); - + // in case of error we just attach the url this.cache.set(url, url); - + return this.cache.get(url); }); } + debug('Read image from cache:', url); // returns image size from cache return this.cache.get(url); diff --git a/ghost/core/core/server/lib/image/image-utils.js b/ghost/core/core/server/lib/image/image-utils.js index 23b725d3cc..0ae8e3f5a2 100644 --- a/ghost/core/core/server/lib/image/image-utils.js +++ b/ghost/core/core/server/lib/image/image-utils.js @@ -7,7 +7,10 @@ class ImageUtils { constructor({config, urlUtils, settingsCache, storageUtils, storage, validator, request}) { this.blogIcon = new BlogIcon({config, urlUtils, settingsCache, storageUtils}); this.imageSize = new ImageSize({config, storage, storageUtils, validator, urlUtils, request}); - this.cachedImageSizeFromUrl = new CachedImageSizeFromUrl({imageSize: this.imageSize}); + this.cachedImageSizeFromUrl = new CachedImageSizeFromUrl({ + imageSize: this.imageSize, + cache: new Map() + }); this.gravatar = new Gravatar({config, request}); } } diff --git a/ghost/core/test/unit/server/lib/image/cached-image-size-from-url.test.js b/ghost/core/test/unit/server/lib/image/cached-image-size-from-url.test.js index 3495c6bf67..550c9b2b37 100644 --- a/ghost/core/test/unit/server/lib/image/cached-image-size-from-url.test.js +++ b/ghost/core/test/unit/server/lib/image/cached-image-size-from-url.test.js @@ -26,11 +26,12 @@ describe('lib/image: image size cache', function () { type: 'jpg' })); - const cachedImageSizeFromUrl = new CachedImageSizeFromUrl({logging: { - error: () => {} - }, imageSize: { - getImageSizeFromUrl: sizeOfStub - }}); + const cachedImageSizeFromUrl = new CachedImageSizeFromUrl({ + imageSize: { + getImageSizeFromUrl: sizeOfStub + }, + cache: new Map() + }); imageSizeSpy = sizeOfStub; @@ -70,11 +71,12 @@ describe('lib/image: image size cache', function () { sizeOfStub.returns(new Promise.reject('error')); - const cachedImageSizeFromUrl = new CachedImageSizeFromUrl({logging: { - error: () => {} - }, imageSize: { - getImageSizeFromUrl: sizeOfStub - }}); + const cachedImageSizeFromUrl = new CachedImageSizeFromUrl({ + imageSize: { + getImageSizeFromUrl: sizeOfStub + }, + cache: new Map() + }); cachedImageSizeResult = Promise.resolve(cachedImageSizeFromUrl.getCachedImageSizeFromUrl(url)); cachedImageSizeResult.then(function () { @@ -89,7 +91,10 @@ describe('lib/image: image size cache', function () { }); it('should return null if url is undefined', function (done) { - const cachedImageSizeFromUrl = new CachedImageSizeFromUrl({logging: {}, imageSize: {}}); + const cachedImageSizeFromUrl = new CachedImageSizeFromUrl({ + imageSize: {}, + cache: new Map() + }); const url = null; let result;