Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #272 from zarathustra323/prevent-image-errors
Browse files Browse the repository at this point in the history
Do not throw fatal error when images are missing/invalid
  • Loading branch information
zarathustra323 authored Apr 6, 2022
2 parents 91c3df8 + e7601b4 commit fe2d174
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
3 changes: 3 additions & 0 deletions packages/image/src/create-src-for.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ module.exports = (host, image, options, defaultOptions) => {
fileName,
cropDimensions,
} = image;
if (!fileName || !filePath) {
return buildImgixUrl(`https://${host}/asset-is-missing-file-name-or-path`, options, defaultOptions);
}
const path = cropDimensions && cropDimensions.aspectRatio ? `${filePath}/${cropDimensions.aspectRatio}` : filePath;
const src = `https://${host}/${path}/${fileName}`;
return buildImgixUrl(src, options, defaultOptions);
Expand Down
2 changes: 1 addition & 1 deletion packages/image/src/crop-rectangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class CropRectangle {
* the crop area.
*/
module.exports = ({ width, height, cropDimensions }) => {
if (!cropDimensions) {
if (!cropDimensions || !width || !height) {
return new CropRectangle({
x: 0,
y: 0,
Expand Down
20 changes: 13 additions & 7 deletions services/graphql-server/src/graphql/utils/get-image-dimensions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fetch = require('node-fetch');
const newrelic = require('../../newrelic');

/**
* Retrieves width/height information from Imgix and sets to
Expand All @@ -18,12 +19,17 @@ module.exports = async ({
} = image;
if (width && height) return { width, height };
const url = `https://${host}/${filePath}/${fileName}?fm=json`;
const res = await fetch(url);
if (!res.ok) {
throw new Error(`Image at ${filePath}/${fileName} ${res.statusText} ID: ${image._id}`);
try {
const res = await fetch(url);
if (!res.ok) {
throw new Error(`Image at ${filePath}/${fileName} ${res.statusText} ID: ${image._id}`);
}
const { PixelWidth, PixelHeight } = await res.json();
const $set = { width: PixelWidth, height: PixelHeight };
await basedb.updateOne('platform.Asset', { _id: image._id }, { $set });
return { width: PixelWidth, height: PixelHeight };
} catch (e) {
newrelic.noticeError(e);
return { width: 0, height: 0 };
}
const { PixelWidth, PixelHeight } = await res.json();
const $set = { width: PixelWidth, height: PixelHeight };
await basedb.updateOne('platform.Asset', { _id: image._id }, { $set });
return { width: PixelWidth, height: PixelHeight };
};

0 comments on commit fe2d174

Please sign in to comment.