Fix: add not found response
All checks were successful
Build and push image / release-image (push) Successful in 1m39s

This commit is contained in:
2025-07-10 16:26:16 +08:00
parent 3fd0398a78
commit 8b66e589d7
2 changed files with 15 additions and 2 deletions

View File

@@ -45,6 +45,9 @@ export class AppController {
Key: pkg, Key: pkg,
Range: range, Range: range,
} as GetObjectCommandInput); } as GetObjectCommandInput);
if (!stream)
throw new HttpException(
`No such file '${pkg}'`, HttpStatus.NOT_FOUND);
return new StreamableFile(stream); return new StreamableFile(stream);
} }

View File

@@ -6,6 +6,8 @@ import {
S3ClientConfig, S3ClientConfig,
GetObjectCommand, GetObjectCommand,
GetObjectCommandInput, GetObjectCommandInput,
GetObjectCommandOutput,
NoSuchKey,
} from '@aws-sdk/client-s3'; } from '@aws-sdk/client-s3';
@Injectable() @Injectable()
@@ -28,14 +30,22 @@ export class AppService {
return 'OK'; return 'OK';
} }
async getObject(getObjectConfig: GetObjectCommandInput): Promise<Readable> { async getObject(getObjectConfig: GetObjectCommandInput): Promise<Readable | null> {
const command = new GetObjectCommand(getObjectConfig); const command = new GetObjectCommand(getObjectConfig);
const res = await this.s3Client.send(command); let res: GetObjectCommandOutput;
try {
res = await this.s3Client.send(command);
} catch (err: unknown) {
if (err instanceof NoSuchKey)
return null;
throw err;
}
if (!res.Body) if (!res.Body)
throw new HttpException( throw new HttpException(
's3 get object failed', 's3 get object failed',
HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.INTERNAL_SERVER_ERROR,
); );
return res.Body as Readable; return res.Body as Readable;
} }
} }