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 52846c750d..86a64341ad 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 @@ -2,11 +2,25 @@ const debug = require('@tryghost/debug')('utils:image-size-cache'); const errors = require('@tryghost/errors'); const logging = require('@tryghost/logging'); +/** + * @example + * { + * height: 50, + * url: 'https://mysite.com/images/cat.jpg', + * width: 50 + * } + * @typedef ImageSizeCache + * @type {Object} + * @property {string} url image url + * @property {number} height image height + * @property {number} width image width + */ + class CachedImageSizeFromUrl { /** * * @param {Object} options - * @param {Function} options.getImageSizeFromUrl - method that resolves images based on URL + * @param {(url: string) => Promise} options.getImageSizeFromUrl - method that resolves images based on URL * @param {Object} options.cache - cache store instance */ constructor({getImageSizeFromUrl, cache}) { @@ -18,7 +32,7 @@ class CachedImageSizeFromUrl { * Get cached image size from URL * Always returns {object} imageSizeCache * @param {string} url - * @returns {Promise} imageSizeCache + * @returns {Promise} * @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. */ @@ -50,7 +64,9 @@ class CachedImageSizeFromUrl { } // in case of error we just attach the url - this.cache.set(url, url); + this.cache.set(url, { + url + }); return this.cache.get(url); } 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 790e2e3dba..eee54abe08 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 @@ -74,6 +74,7 @@ describe('lib/image: image size cache', function () { cacheStore.get(url).should.not.be.undefined; const image = cacheStore.get(url); + should.equal(image.url, 'http://mysite.com/content/image/mypostcoverimage.jpg'); should.not.exist(image.width); should.not.exist(image.height); }); @@ -93,6 +94,7 @@ describe('lib/image: image size cache', function () { cacheStore.get(url).should.not.be.undefined; const image = cacheStore.get(url); + should.equal(image.url, 'http://mysite.com/content/image/mypostcoverimage.jpg'); should.not.exist(image.width); should.not.exist(image.height); });