diff --git a/web/src/components/cached-resource-image.tsx b/web/src/components/cached-resource-image.tsx index efda66929..fe0213322 100644 --- a/web/src/components/cached-resource-image.tsx +++ b/web/src/components/cached-resource-image.tsx @@ -2,6 +2,7 @@ import { useEffect, useRef, useState, type ImgHTMLAttributes, type ReactNode } f import { resourceIdFromStorageKey } from "@/services/api/resources"; import { cacheResourceObjectUrl } from "@/services/resource-blob-cache"; +import { resolveImageUrl } from "@/services/image-storage"; type CachedResourceImageProps = Omit, "src"> & { storageKey?: string; @@ -12,10 +13,11 @@ type CachedResourceImageProps = Omit, "src"> /** * 资源图片优先读取按用户隔离的本地 Blob 缓存,避免刷新后再次从对象存储下载。 - * 普通外链、data URL 和本地 Blob URL 不经过资源缓存,保持原有行为。 + * 本地 image: 类型的 storageKey 也会自动从 LocalForage 恢复有效的 Object URL。 */ export function CachedResourceImage({ storageKey, src = "", fallback = null, eager = false, onError, ...props }: CachedResourceImageProps) { const remoteResource = Boolean(resourceIdFromStorageKey(storageKey)); + const localImageResource = Boolean(storageKey && storageKey.startsWith("image:")); const targetRef = useRef(null); const [nearViewport, setNearViewport] = useState(eager || !remoteResource); const [cachedSrc, setCachedSrc] = useState(remoteResource ? "" : src); @@ -44,32 +46,64 @@ export function CachedResourceImage({ storageKey, src = "", fallback = null, eag useEffect(() => { let cancelled = false; setCacheFailed(false); - if (!remoteResource || !storageKey) { - setCachedSrc(src); - return () => { cancelled = true; }; - } - if (!nearViewport) { - setCachedSrc(""); - return () => { cancelled = true; }; - } - setCachedSrc(""); - const resolve = cacheResourceObjectUrl(storageKey); - void resolve.then((url) => { - if (!cancelled) setCachedSrc(url || src); - }).catch(() => { - if (!cancelled) { - setCacheFailed(true); - setCachedSrc(src); + if (remoteResource && storageKey) { + if (!nearViewport) { + setCachedSrc(""); + return () => { cancelled = true; }; } - }); - return () => { cancelled = true; }; - }, [nearViewport, remoteResource, src, storageKey]); + setCachedSrc(""); + const resolve = cacheResourceObjectUrl(storageKey); + void resolve.then((url) => { + if (!cancelled) setCachedSrc(url || src); + }).catch(() => { + if (!cancelled) { + setCacheFailed(true); + setCachedSrc(src); + } + }); + return () => { cancelled = true; }; + } - if (!remoteResource) return ; + if (localImageResource && storageKey) { + void resolveImageUrl(storageKey, src).then((url) => { + if (!cancelled) setCachedSrc(url || src); + }).catch(() => { + if (!cancelled) setCachedSrc(src); + }); + return () => { cancelled = true; }; + } + + setCachedSrc(src); + return () => { cancelled = true; }; + }, [localImageResource, nearViewport, remoteResource, src, storageKey]); + + const handleImgError = (e: React.SyntheticEvent) => { + if (localImageResource && storageKey && cachedSrc.startsWith("blob:")) { + void resolveImageUrl(storageKey).then((url) => { + if (url && url !== cachedSrc) { + setCachedSrc(url); + return; + } + setCacheFailed(true); + onError?.(e); + }).catch(() => { + setCacheFailed(true); + onError?.(e); + }); + return; + } + setCacheFailed(true); + onError?.(e); + }; + + if (!remoteResource) { + if (cacheFailed && fallback) return <>{fallback}; + return ; + } return ( - {cachedSrc && !(cacheFailed && !src) ? : fallback} + {cachedSrc && !(cacheFailed && !src) ? : fallback} ); } diff --git a/web/src/components/canvas/canvas-project-card.tsx b/web/src/components/canvas/canvas-project-card.tsx index 87a70d28a..df63add13 100644 --- a/web/src/components/canvas/canvas-project-card.tsx +++ b/web/src/components/canvas/canvas-project-card.tsx @@ -1,4 +1,4 @@ -import { Check, Clapperboard, Download, FileText, Frame, Image as ImageIcon, MoreHorizontal, Music2, Pencil, Plus, Settings2, Sparkles, Trash2, Video, X } from "lucide-react"; +import { Check, Clapperboard, CloudUpload, Download, FileText, Frame, Image as ImageIcon, MoreHorizontal, Music2, Pencil, Plus, Settings2, Sparkles, Trash2, Video, X } from "lucide-react"; import type { ReactNode } from "react"; import { useNavigate, useSearchParams } from "react-router"; import { Dropdown, Input } from "antd"; @@ -11,6 +11,7 @@ import { resourceFileUrl, resourceIdFromStorageKey } from "@/services/api/resour import { resolveBackendApiUrl } from "@/stores/use-config-store"; import { CachedResourceImage } from "@/components/cached-resource-image"; import { cn } from "@/lib/utils"; +import { useSyncProgressStore } from "@/stores/use-sync-progress-store"; export function CanvasCreateCard({ disabled, onClick }: { disabled?: boolean; onClick: () => void }) { return