From db66f1cbbd8d8bc3927d178e415b76f17e56ef3b Mon Sep 17 00:00:00 2001 From: Kevin Ansfield <kevin@lookingsideways.co.uk> Date: Tue, 23 Jun 2020 12:47:20 +0100 Subject: [PATCH] Fixed `populateImageSizes` handling of images in subdir setups no issue - when using subdirectories, images can be stored in the database both with and without the subdirectory prefix. We weren't taking that into account and so images without the subdirectory were not having the `/content/images/` prefix removed when passed to the storage adapter resulting in the storage adapter not finding the image --- core/server/lib/mobiledoc.js | 5 +++-- test/unit/lib/mobiledoc_spec.js | 34 ++++++++++++++++++++++++++------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/core/server/lib/mobiledoc.js b/core/server/lib/mobiledoc.js index e232ad4ae8..cc0574db22 100644 --- a/core/server/lib/mobiledoc.js +++ b/core/server/lib/mobiledoc.js @@ -108,8 +108,9 @@ module.exports = { } // local storage adapter's .exists() expects image paths without any prefixes - const imageUrlPrefix = urlUtils.urlJoin(urlUtils.getSubdir(), urlUtils.STATIC_IMAGE_URL_PREFIX); - const storagePath = url.replace(imageUrlPrefix, ''); + const subdirRegex = new RegExp(`^${urlUtils.getSubdir()}`); + const contentRegex = new RegExp(`^/${urlUtils.STATIC_IMAGE_URL_PREFIX}`); + const storagePath = url.replace(subdirRegex, '').replace(contentRegex, ''); const {dir, name, ext} = path.parse(storagePath); const [imageNameMatched, imageName, imageNumber] = name.match(/^(.+?)(-\d+)?$/) || [null]; diff --git a/test/unit/lib/mobiledoc_spec.js b/test/unit/lib/mobiledoc_spec.js index eab154bed9..a111f420ed 100644 --- a/test/unit/lib/mobiledoc_spec.js +++ b/test/unit/lib/mobiledoc_spec.js @@ -8,6 +8,11 @@ const storage = require('../../../core/server/adapters/storage'); describe('lib/mobiledoc', function () { beforeEach(function () { configUtils.set('url', 'https://example.com'); + + // UrlUtils gets cached with old config data so we need to make sure it's + // reloaded when it gets required in modules under test so that our config + // changes actually have an effect + delete require.cache[require.resolve('../../../core/shared/url-utils')]; }); afterEach(function () { @@ -150,19 +155,34 @@ describe('lib/mobiledoc', function () { unsplashMock.isDone().should.be.true(); transformed.cards.length.should.equal(4); + }); + + it('works with subdir', async function () { + // images can be stored with and without subdir when a subdir is configured + // but storage adapter always needs paths relative to content dir + configUtils.set('url', 'http://localhost:2368/subdir/'); + + let mobiledoc = { + cards: [ + ['image', {src: '/content/images/ghost-logo.png'}], + ['image', {src: '/subdir/content/images/ghost-logo.png'}] + ] + }; + + const transformedMobiledoc = await mobiledocLib.populateImageSizes(JSON.stringify(mobiledoc)); + const transformed = JSON.parse(transformedMobiledoc); + + transformed.cards.length.should.equal(2); should.exist(transformed.cards[0][1].width); transformed.cards[0][1].width.should.equal(800); should.exist(transformed.cards[0][1].height); transformed.cards[0][1].height.should.equal(257); - should.not.exist(transformed.cards[1][1].width); - should.not.exist(transformed.cards[1][1].height); - - should.exist(transformed.cards[2][1].width); - transformed.cards[2][1].width.should.equal(100); - should.exist(transformed.cards[2][1].height); - transformed.cards[2][1].height.should.equal(80); + should.exist(transformed.cards[1][1].width); + transformed.cards[1][1].width.should.equal(800); + should.exist(transformed.cards[1][1].height); + transformed.cards[1][1].height.should.equal(257); }); }); });