forked from mtusi2026/mtusi2026lab5
feat: accept uploads into staging with jobs, outbox and job processor
This commit is contained in:
@@ -76,6 +76,69 @@ export class S3Service implements OnModuleDestroy {
|
||||
);
|
||||
}
|
||||
|
||||
async putBuffer(
|
||||
key: string,
|
||||
body: Buffer,
|
||||
contentType: string,
|
||||
sha256: string,
|
||||
): Promise<void> {
|
||||
await this.guard(`put ${key}`, () =>
|
||||
this.client.send(
|
||||
new PutObjectCommand({
|
||||
Bucket: this.bucket,
|
||||
Key: key,
|
||||
Body: body,
|
||||
ContentLength: body.length,
|
||||
ContentType: contentType,
|
||||
ChecksumSHA256: Buffer.from(sha256, 'hex').toString('base64'),
|
||||
}),
|
||||
{ abortSignal: AbortSignal.timeout(OPERATION_TIMEOUT_MS) },
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
async getBuffer(key: string, maxBytes: number): Promise<Buffer> {
|
||||
let body: Readable;
|
||||
try {
|
||||
const response = await this.client.send(
|
||||
new GetObjectCommand({ Bucket: this.bucket, Key: key }),
|
||||
{ abortSignal: AbortSignal.timeout(OPERATION_TIMEOUT_MS) },
|
||||
);
|
||||
if ((response.ContentLength ?? 0) > maxBytes) {
|
||||
throw new ObjectTooLargeError(key);
|
||||
}
|
||||
body = response.Body as Readable;
|
||||
} catch (error) {
|
||||
if (error instanceof ObjectTooLargeError) {
|
||||
throw error;
|
||||
}
|
||||
const name = error instanceof Error ? error.name : 'unknown error';
|
||||
this.logger.error(`get ${key} failed: ${name}`);
|
||||
if (name === 'NoSuchKey') {
|
||||
throw new ObjectMissingError(key);
|
||||
}
|
||||
throw new ApiError('unavailable', 'object storage unavailable');
|
||||
}
|
||||
const chunks: Buffer[] = [];
|
||||
let size = 0;
|
||||
try {
|
||||
for await (const chunk of body) {
|
||||
size += chunk.length;
|
||||
if (size > maxBytes) {
|
||||
body.destroy();
|
||||
throw new ObjectTooLargeError(key);
|
||||
}
|
||||
chunks.push(chunk);
|
||||
}
|
||||
} catch (error) {
|
||||
if (error instanceof ObjectTooLargeError) {
|
||||
throw error;
|
||||
}
|
||||
throw new ApiError('unavailable', 'object storage unavailable');
|
||||
}
|
||||
return Buffer.concat(chunks);
|
||||
}
|
||||
|
||||
async getStream(key: string): Promise<Readable> {
|
||||
const deadline = new AbortController();
|
||||
const timer = setTimeout(() => deadline.abort(), IDLE_TIMEOUT_MS);
|
||||
@@ -158,6 +221,20 @@ export class S3Service implements OnModuleDestroy {
|
||||
}
|
||||
}
|
||||
|
||||
export class ObjectMissingError extends Error {
|
||||
constructor(key: string) {
|
||||
super(`object ${key} is missing from storage`);
|
||||
this.name = 'ObjectMissingError';
|
||||
}
|
||||
}
|
||||
|
||||
export class ObjectTooLargeError extends Error {
|
||||
constructor(key: string) {
|
||||
super(`object ${key} exceeds size limit`);
|
||||
this.name = 'ObjectTooLargeError';
|
||||
}
|
||||
}
|
||||
|
||||
function readSecret(name: string): string {
|
||||
const file = process.env[`${name}_FILE`];
|
||||
const value = file ? readFileSync(file, 'utf8').trim() : process.env[name];
|
||||
|
||||
Reference in New Issue
Block a user