Skip to content

Commit

Permalink
Removed bluebird dependency
Browse files Browse the repository at this point in the history
Issue TryGhost#14882
- Bluebird promise was being used for getting cached image sizes using .props method. It has been removed to be replaced by native promise using Promise.all
  • Loading branch information
Navarjun authored and Navarjun committed May 28, 2022
1 parent 302c257 commit 12379e2
Showing 1 changed file with 47 additions and 39 deletions.
86 changes: 47 additions & 39 deletions core/frontend/meta/image-dimensions.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,69 @@
const Promise = require('bluebird');
const _ = require('lodash');
const imageSizeCache = require('../../server/lib/image').cachedImageSizeFromUrl;

/**
* Get Image dimensions
* @param {object} metaData
* @returns {object} metaData
* @returns {Promise<object>} metaData
* @description for image properties in meta data (coverImage, authorImage and site.logo), `getCachedImageSizeFromUrl` is
* called to receive image width and height
*/
function getImageDimensions(metaData) {
async function getImageDimensions(metaData) {
const fetch = {
coverImage: imageSizeCache.getCachedImageSizeFromUrl(metaData.coverImage.url),
authorImage: imageSizeCache.getCachedImageSizeFromUrl(metaData.authorImage.url),
ogImage: imageSizeCache.getCachedImageSizeFromUrl(metaData.ogImage.url),
logo: imageSizeCache.getCachedImageSizeFromUrl(metaData.site.logo.url)
};

return Promise
.props(fetch)
.then(function (imageObj) {
_.forEach(imageObj, function (key, value) {
if (_.has(key, 'width') && _.has(key, 'height')) {
// We have some restrictions for publisher.logo:
// The image needs to be <=600px wide and <=60px high (ideally exactly 600px x 60px).
// Unless we have proper image-handling (see https://github.com/TryGhost/Ghost/issues/4453),
// we will fake it in some cases or not produce an imageObject at all.
if (value === 'logo') {
if (key.height <= 60 && key.width <= 600) {
_.assign(metaData.site[value], {
dimensions: {
width: key.width,
height: key.height
}
});
} else if (key.width === key.height) {
// CASE: the logo is too large, but it is a square. We fake it...
_.assign(metaData.site[value], {
dimensions: {
width: 60,
height: 60
}
});
const [coverImage, authorImage, ogImage, logo] = await Promise.all([
fetch.coverImage,
fetch.authorImage,
fetch.ogImage,
fetch.logo
]);
const imageObj = {
coverImage,
authorImage,
ogImage,
logo
};

_.forEach(imageObj, function (key, value) {
if (_.has(key, 'width') && _.has(key, 'height')) {
// We have some restrictions for publisher.logo:
// The image needs to be <=600px wide and <=60px high (ideally exactly 600px x 60px).
// Unless we have proper image-handling (see https://github.com/TryGhost/Ghost/issues/4453),
// we will fake it in some cases or not produce an imageObject at all.
if (value === 'logo') {
if (key.height <= 60 && key.width <= 600) {
_.assign(metaData.site[value], {
dimensions: {
width: key.width,
height: key.height
}
} else {
_.assign(metaData[value], {
dimensions: {
width: key.width,
height: key.height
}
});
}
});
} else if (key.width === key.height) {
// CASE: the logo is too large, but it is a square. We fake it...
_.assign(metaData.site[value], {
dimensions: {
width: 60,
height: 60
}
});
}
});
} else {
_.assign(metaData[value], {
dimensions: {
width: key.width,
height: key.height
}
});
}
}
});

return metaData;
});
return metaData;
}

module.exports = getImageDimensions;

0 comments on commit 12379e2

Please sign in to comment.