Skip to content
This repository was archived by the owner on May 28, 2023. It is now read-only.

Bugfix: Image file cache throws unhandled error if image not exists #580

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/api/img.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
17 changes: 13 additions & 4 deletions src/image/action/local/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down