feat: 完善媒体存储状态与上传失败提示
This commit is contained in:
1 parent
84956b8fe2
commit
e09b56e0f3
3 files changed
+67
-9
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import { InboxOutlined } from "@ant-design/icons";
|
||||
import { Image, Input, Space, Typography, Upload, message } from "antd";
|
||||
import { useState } from "react";
|
||||
import { CloudUploadOutlined, InboxOutlined } from "@ant-design/icons";
|
||||
import { Alert, Button, Image, Input, Space, Typography, Upload, message } from "antd";
|
||||
import { useEffect, useState } from "react";
|
||||
import { createApiClient } from "../lib/api.js";
|
||||
|
||||
interface ImageUrlFieldProps {
|
||||
@@ -14,8 +14,26 @@ interface ImageUrlFieldProps {
|
||||
export function ImageUrlField({ id, value, onChange, placeholder, scope }: ImageUrlFieldProps) {
|
||||
const imageUrl = typeof value === "string" ? value.trim() : "";
|
||||
const [uploading, setUploading] = useState(false);
|
||||
const [storageStatus, setStorageStatus] = useState<
|
||||
"loading" | "ok" | "unavailable" | "unconfigured"
|
||||
>("loading");
|
||||
const client = createApiClient();
|
||||
|
||||
useEffect(() => {
|
||||
let active = true;
|
||||
client
|
||||
.getUploadConfig()
|
||||
.then((config) => {
|
||||
if (active) setStorageStatus(config.configured ? config.status : "unconfigured");
|
||||
})
|
||||
.catch(() => {
|
||||
if (active) setStorageStatus("unavailable");
|
||||
});
|
||||
return () => {
|
||||
active = false;
|
||||
};
|
||||
}, [client]);
|
||||
|
||||
async function upload(file: File) {
|
||||
if (!file.type.startsWith("image/")) {
|
||||
message.error("仅支持图片文件");
|
||||
@@ -40,7 +58,8 @@ export function ImageUrlField({ id, value, onChange, placeholder, scope }: Image
|
||||
if (!response.ok) throw new Error(`upload failed: ${response.status}`);
|
||||
onChange?.(presign.publicUrl);
|
||||
} catch {
|
||||
message.error("图片上传失败,请重试");
|
||||
setStorageStatus("unavailable");
|
||||
message.error("图片上传失败,请检查对象存储后重试");
|
||||
} finally {
|
||||
setUploading(false);
|
||||
}
|
||||
@@ -54,13 +73,26 @@ export function ImageUrlField({ id, value, onChange, placeholder, scope }: Image
|
||||
onChange={(event) => onChange?.(event.target.value)}
|
||||
placeholder={placeholder ?? "粘贴图片地址"}
|
||||
/>
|
||||
{storageStatus !== "ok" ? (
|
||||
<Alert
|
||||
type={storageStatus === "loading" ? "info" : "warning"}
|
||||
showIcon
|
||||
message={
|
||||
storageStatus === "loading"
|
||||
? "正在检查媒体存储..."
|
||||
: storageStatus === "unconfigured"
|
||||
? "媒体存储未配置,请先设置对象存储"
|
||||
: "媒体存储不可用,请检查对象存储配置"
|
||||
}
|
||||
/>
|
||||
) : null}
|
||||
<Upload.Dragger
|
||||
accept="image/*"
|
||||
multiple={false}
|
||||
showUploadList={false}
|
||||
disabled={uploading}
|
||||
disabled={uploading || storageStatus !== "ok"}
|
||||
beforeUpload={(file) => {
|
||||
void upload(file);
|
||||
if (storageStatus === "ok") void upload(file);
|
||||
return false;
|
||||
}}
|
||||
>
|
||||
@@ -70,6 +102,22 @@ export function ImageUrlField({ id, value, onChange, placeholder, scope }: Image
|
||||
<p className="ant-upload-text">{uploading ? "上传中..." : "点击或拖拽上传图片"}</p>
|
||||
<p className="ant-upload-hint">JPG、PNG、WebP 或 AVIF,最大 10MB</p>
|
||||
</Upload.Dragger>
|
||||
{storageStatus === "unavailable" ? (
|
||||
<Button
|
||||
icon={<CloudUploadOutlined />}
|
||||
onClick={() => {
|
||||
setStorageStatus("loading");
|
||||
client
|
||||
.getUploadConfig()
|
||||
.then((config) =>
|
||||
setStorageStatus(config.configured ? config.status : "unconfigured"),
|
||||
)
|
||||
.catch(() => setStorageStatus("unavailable"));
|
||||
}}
|
||||
>
|
||||
重新检查存储
|
||||
</Button>
|
||||
) : null}
|
||||
{imageUrl ? (
|
||||
<Space align="start">
|
||||
<Image
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -57,6 +57,7 @@ export interface AdminBrandMember {
|
||||
|
||||
export interface UploadConfig {
|
||||
configured: boolean;
|
||||
status: "ok" | "unavailable" | "unconfigured";
|
||||
}
|
||||
|
||||
export interface PresignedUpload {
|
||||
|
||||
Reference in new issue
Block a user