From e09b56e0f382f698d73de4a965daccef635fb7cc Mon Sep 17 00:00:00 2001 From: que01 Date: Mon, 12 Oct 2026 01:00:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E5=96=84=E5=AA=92=E4=BD=93?= =?UTF-8?q?=E5=AD=98=E5=82=A8=E7=8A=B6=E6=80=81=E4=B8=8E=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/admin/src/components/ImageUrlField.tsx | 60 +++++++++++++++++-- .../modules/admin/admin-upload.controller.ts | 15 ++++- packages/api-client/src/index.ts | 1 + 3 files changed, 67 insertions(+), 9 deletions(-) diff --git a/apps/admin/src/components/ImageUrlField.tsx b/apps/admin/src/components/ImageUrlField.tsx index 5831024..c30130e 100644 --- a/apps/admin/src/components/ImageUrlField.tsx +++ b/apps/admin/src/components/ImageUrlField.tsx @@ -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" ? ( + + ) : null} { - void upload(file); + if (storageStatus === "ok") void upload(file); return false; }} > @@ -70,6 +102,22 @@ export function ImageUrlField({ id, value, onChange, placeholder, scope }: Image

{uploading ? "上传中..." : "点击或拖拽上传图片"}

JPG、PNG、WebP 或 AVIF,最大 10MB

+ {storageStatus === "unavailable" ? ( + + ) : null} {imageUrl ? ( { + 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") diff --git a/packages/api-client/src/index.ts b/packages/api-client/src/index.ts index 5442c33..7c1cbeb 100644 --- a/packages/api-client/src/index.ts +++ b/packages/api-client/src/index.ts @@ -57,6 +57,7 @@ export interface AdminBrandMember { export interface UploadConfig { configured: boolean; + status: "ok" | "unavailable" | "unconfigured"; } export interface PresignedUpload {