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); }); }); });