feat: 完善媒体存储状态与上传失败提示

This commit is contained in:
que01 committed 2026-10-12 01:00:02 +08:00
1 parent 84956b8fe2
commit e09b56e0f3
3 files changed
+67 -9

No files matched your search

@@ -1,5 +1,5 @@
import { BadRequestException, Body, Controller, Get, Post, Req } from "@nestjs/common";
import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
import { HeadBucketCommand, PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import { createHash, randomUUID } from "node:crypto";
import type { FastifyRequest } from "fastify";
@@ -52,6 +52,15 @@ class ObjectStorage {
{ expiresIn: 600 },
);
}
async checkBucket(): Promise<"ok" | "unavailable"> {
try {
await this.client.send(new HeadBucketCommand({ Bucket: this.bucket }));
return "ok";
} catch {
return "unavailable";
}
}
}
const allowedExtensions = new Map([
@@ -68,13 +77,13 @@ export class AdminUploadController {
constructor(private readonly access: AdminAccessService) {}
@Get("config")
config() {
async config() {
const configured = Boolean(
process.env.OBJECT_STORAGE_BUCKET &&
process.env.OBJECT_STORAGE_ACCESS_KEY &&
process.env.OBJECT_STORAGE_SECRET_KEY,
);
return { configured };
return { configured, status: configured ? await this.storage.checkBucket() : "unconfigured" };
}
@Post("presign")