refactor(chat): tighten media cache boundaries

This commit is contained in:
2026-06-30 16:12:02 +08:00
parent ccd2d6bd48
commit 9cff16e2d9
17 changed files with 388 additions and 146 deletions
+7 -17
View File
@@ -13,8 +13,12 @@ import { useRouter } from "next/navigation";
import { useState } from "react";
import { ROUTE_BUILDERS } from "@/router/routes";
import {
isBrowserLocalChatImageSource,
normalizeChatImageSource,
} from "@/lib/chat/chat_media_url";
import { useCachedChatMediaUrl } from "@/lib/chat/use_cached_chat_media_url";
import { useCachedChatMediaUrl } from "../hooks/use-cached-chat-media-url";
import styles from "./image-bubble.module.css";
export interface ImageBubbleProps {
@@ -36,9 +40,9 @@ export function ImageBubble({
kind: "image",
});
const src = decodeBase64Image(mediaUrl || imageUrl);
const src = normalizeChatImageSource(mediaUrl || imageUrl);
const canOpen = Boolean(messageId);
const shouldUseNativeImage = isBrowserLocalImageSrc(src);
const shouldUseNativeImage = isBrowserLocalChatImageSource(src);
const openImage = () => {
if (!messageId) return;
@@ -85,17 +89,3 @@ export function ImageBubble({
</div>
);
}
/** 解析 base64 data URI`data:image/png;base64,...`),否则原样返回 */
function decodeBase64Image(uri: string): string {
// 已是 data URI 或 http(s) URL:直接返回
if (uri.startsWith("data:") || uri.startsWith("http")) {
return uri;
}
// 裸 base64 字符串 → 包装成 data URIPNG 兜底)
return `data:image/png;base64,${uri}`;
}
function isBrowserLocalImageSrc(src: string): boolean {
return src.startsWith("blob:") || src.startsWith("data:");
}