0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-27 22:49:56 -05:00

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
This commit is contained in:
Kevin Ansfield 2020-06-23 12:47:20 +01:00 committed by Hannah Wolfe
parent 1694fa3675
commit 771908d04b
2 changed files with 30 additions and 9 deletions

View file

@ -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];

View file

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