fix: return error based on official approach.

This commit is contained in:
Shibo Lyu 2025-01-04 01:04:00 +08:00 committed by Shibo Lyu
parent 06890b99cc
commit 91a3a1423c
3 changed files with 1190 additions and 4 deletions

View file

@ -12,6 +12,8 @@
"license": "ISC",
"dependencies": {
"@aws-sdk/client-s3": "^3.540.0",
"@tryghost/errors": "^1.3.6",
"@tryghost/tpl": "^0.1.33",
"ghost-storage-base": "^1.0.0"
},
"devDependencies": {

1178
pnpm-lock.yaml generated

File diff suppressed because it is too large Load diff

View file

@ -5,6 +5,8 @@ import {
S3,
} from '@aws-sdk/client-s3'
import StorageBase, { type ReadOptions, type Image } from 'ghost-storage-base'
import errors from '@tryghost/errors'
import tpl from '@tryghost/tpl'
import { join } from 'path'
import { createReadStream } from 'fs'
import type { Readable } from 'stream'
@ -197,16 +199,20 @@ class S3Storage extends StorageBase {
stream.pipe(res)
} catch (err) {
if (err.name === 'NoSuchKey') {
res.status(404).send('Image not found');
return next(
new errors.NotFoundError({
message: tpl('File not found'),
code: 'STATIC_FILE_NOT_FOUND',
property: err.path,
})
)
} else {
res.status(500);
next(err);
next(new errors.InternalServerError({ err: err }))
}
}
}
}
async read(options: ReadOptions = { path: '' }) {
let path = (options.path || '').replace(/\/$|\\$/, '')