From 8068a4ff8bc1b4e1cc44a1498bbe5085be70e7f0 Mon Sep 17 00:00:00 2001 From: Anders Ekelund Date: Tue, 21 Sep 2021 13:56:36 +0200 Subject: [PATCH 1/2] Add and sett error flag if image source throws exception. Add 404 edge case. --- src/image/action/local/index.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/image/action/local/index.ts b/src/image/action/local/index.ts index 82114eaf..ea40385e 100644 --- a/src/image/action/local/index.ts +++ b/src/image/action/local/index.ts @@ -6,6 +6,7 @@ export default class LocalImageAction extends ImageAction { public imageOptions public SUPPORTED_MIMETYPES = ['image/gif', 'image/png', 'image/jpeg', 'image/webp', 'image/svg+xml'] public imageBuffer: Buffer + public error = false public get whitelistDomain (): string[] { return this.options.imageable.whitelist @@ -90,10 +91,18 @@ export default class LocalImageAction extends ImageAction { try { this.imageBuffer = await downloadImage(imgUrl) } catch (err) { - return this.res.status(400).send({ - code: 400, - result: `Unable to download the requested image ${imgUrl}` - }) + this.error = true + if (err.statusCode === 404) { + return this.res.status(404).send({ + code: 404, + result: 'Not found' + }) + } else { + return this.res.status(500).send({ + code: 500, + result: `Unable to download the requested image ${imgUrl}` + }) + } } const { action, width, height } = this.imageOptions switch (action) { From a838d57c9dec8ca51eeff84c1fe435d865afee70 Mon Sep 17 00:00:00 2001 From: Anders Ekelund Date: Tue, 21 Sep 2021 13:57:06 +0200 Subject: [PATCH 2/2] Only cache image if no errors raised --- src/api/img.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/img.ts b/src/api/img.ts index 42bb7156..400ee1df 100755 --- a/src/api/img.ts +++ b/src/api/img.ts @@ -35,7 +35,7 @@ export default ({ config, db }) => } else { await imageAction.prossesImage() - if (config.imageable.caching.active) { + if (config.imageable.caching.active && !imageAction.error) { cache.image = imageAction.imageBuffer await cache.save() }