diff --git a/src-tauri/src/networking/astrbot_client.rs b/src-tauri/src/networking/astrbot_client.rs index 77262ee..44ccca4 100644 --- a/src-tauri/src/networking/astrbot_client.rs +++ b/src-tauri/src/networking/astrbot_client.rs @@ -491,12 +491,14 @@ impl AstrBotClient { if !response.status().is_success() { return Err(AstrBotClientError::HttpStatus(response.status())); } - let mime_type = response + let status = response.status(); + let header_mime = response .headers() .get(CONTENT_TYPE) .and_then(|value| value.to_str().ok()) .and_then(|value| value.split(';').next()) - .filter(|value| !value.trim().is_empty()) + .map(str::trim) + .filter(|value| !value.is_empty()) .unwrap_or("application/octet-stream") .to_owned(); let bytes = response.bytes().await?.to_vec(); @@ -505,6 +507,28 @@ impl AstrBotClient { "attachment size is outside the supported range", )); } + + // A blob URL is decoded according to the blob's type, so handing the + // webview `application/octet-stream` makes `` refuse bytes that are + // a perfectly good PNG. Prefer a concrete image type from the server, + // fall back to what the bytes themselves say. + let sniffed = sniff_image_mime(&bytes); + let mime_type = if header_mime.starts_with("image/") { + header_mime.clone() + } else if let Some(sniffed) = sniffed { + sniffed.to_owned() + } else { + header_mime.clone() + }; + tracing::debug!( + target: FRAME_TRACE_TARGET, + status = status.as_u16(), + header_mime = header_mime.as_str(), + sniffed = sniffed.unwrap_or(""), + resolved_mime = mime_type.as_str(), + bytes = bytes.len(), + "astrbot attachment download" + ); Ok((mime_type, bytes)) } @@ -871,6 +895,36 @@ fn trace_frame_fields(value: &Value) { ); } +/// Identifies an image from its leading bytes. +/// +/// Only used when the server does not name a concrete image type. Every +/// signature here is a fixed format marker at a fixed offset, so this reads no +/// user content. +fn sniff_image_mime(bytes: &[u8]) -> Option<&'static str> { + const PNG: &[u8] = &[0x89, b'P', b'N', b'G', 0x0D, 0x0A, 0x1A, 0x0A]; + const JPEG: &[u8] = &[0xFF, 0xD8, 0xFF]; + const GIF87: &[u8] = b"GIF87a"; + const GIF89: &[u8] = b"GIF89a"; + const BMP: &[u8] = b"BM"; + + if bytes.starts_with(PNG) { + return Some("image/png"); + } + if bytes.starts_with(JPEG) { + return Some("image/jpeg"); + } + if bytes.starts_with(GIF87) || bytes.starts_with(GIF89) { + return Some("image/gif"); + } + if bytes.starts_with(BMP) { + return Some("image/bmp"); + } + if bytes.len() >= 12 && bytes.starts_with(b"RIFF") && &bytes[8..12] == b"WEBP" { + return Some("image/webp"); + } + None +} + fn protocol_slot(slot: SlotKind) -> desktop_protocol::ActiveSlotKind { match slot { SlotKind::Companion => desktop_protocol::ActiveSlotKind::Companion, @@ -1044,6 +1098,27 @@ mod tests { } } + #[test] + fn image_bytes_are_identified_when_the_server_names_no_type() { + let png = [0x89, b'P', b'N', b'G', 0x0D, 0x0A, 0x1A, 0x0A, 0x00]; + assert_eq!(sniff_image_mime(&png), Some("image/png")); + assert_eq!( + sniff_image_mime(&[0xFF, 0xD8, 0xFF, 0xE0]), + Some("image/jpeg") + ); + assert_eq!(sniff_image_mime(b"GIF89a...."), Some("image/gif")); + assert_eq!( + sniff_image_mime(b"RIFF\0\0\0\0WEBPVP8 "), + Some("image/webp") + ); + assert_eq!(sniff_image_mime(b"BM\0\0"), Some("image/bmp")); + + // A JSON error body served with HTTP 200 must not be mistaken for one. + assert_eq!(sniff_image_mime(br#"{"status":"error"}"#), None); + assert_eq!(sniff_image_mime(b""), None); + assert_eq!(sniff_image_mime(b""), None); + } + #[test] fn frame_trace_stays_silent_when_the_filter_excludes_it() { let sink = CapturedLog::default(); diff --git a/src/features/chat/MessageContent.tsx b/src/features/chat/MessageContent.tsx index 559b07e..c45c38f 100644 --- a/src/features/chat/MessageContent.tsx +++ b/src/features/chat/MessageContent.tsx @@ -1,6 +1,7 @@ import { Fragment, useEffect, useState, type ReactNode } from "react"; import { invoke } from "@tauri-apps/api/core"; +import { errorMessage } from "../../lib/errorMessage"; import { safeLinkHref } from "../../lib/safeLinkHref"; import type { ChatSlot } from "./types"; @@ -25,16 +26,24 @@ const emotionLabels: Record = { }; function normalizeContent(content: string): string { - return content.replace(/&&([a-zA-Z_-]+)&&/g, (_, name: string) => - emotionLabels[name.toLowerCase()] ?? "", + return content.replace( + /&&([a-zA-Z_-]+)&&/g, + (_, name: string) => emotionLabels[name.toLowerCase()] ?? "", ); } +interface AttachmentView { + urls: Record; + errors: Record; + onDecodeFailure: (attachmentId: string) => void; +} + function renderInline( text: string, keyPrefix: string, - imageUrls: Record, + attachments: AttachmentView, ): ReactNode[] { + const { urls: imageUrls, errors: attachmentErrors } = attachments; const tokens = text.split( /(!\[[^\]]*\]\(astrbot-attachment:\/\/[A-Za-z0-9._-]+\)|`[^`]+`|\*\*[^*]+\*\*|\[[^\]]+\]\([^)]+\))/g, ); @@ -44,17 +53,29 @@ function renderInline( /^!\[([^\]]*)\]\(astrbot-attachment:\/\/([A-Za-z0-9._-]+)\)$/, ); if (image) { - const source = imageUrls[image[2]]; + const attachmentId = image[2]; + const failure = attachmentErrors[attachmentId]; + if (failure) { + // Previously this path silently produced a broken image icon with no + // way to tell a failed download from an undecodable payload. + return ( + + 图片无法显示:{failure} + + ); + } + const source = imageUrls[attachmentId]; return source ? ( {image[1] attachments.onDecodeFailure(attachmentId)} /> ) : ( - image loading + 图片加载中… ); } @@ -82,6 +103,9 @@ function renderInline( export function MessageContent({ content, slot }: MessageContentProps) { const [imageUrls, setImageUrls] = useState>({}); + const [attachmentErrors, setAttachmentErrors] = useState< + Record + >({}); const attachmentIds = Array.from( content.matchAll( /!\[[^\]]*\]\(astrbot-attachment:\/\/([A-Za-z0-9._-]+)\)/g, @@ -95,6 +119,7 @@ export function MessageContent({ content, slot }: MessageContentProps) { let cancelled = false; const createdUrls: string[] = []; setImageUrls({}); + setAttachmentErrors({}); const ids = attachmentKey ? attachmentKey.split("|") : []; if (!ids.length) return undefined; @@ -110,7 +135,11 @@ export function MessageContent({ content, slot }: MessageContentProps) { payload.bytes instanceof Uint8Array ? payload.bytes : Array.isArray(payload.bytes) - ? new Uint8Array(payload.bytes.filter((item): item is number => typeof item === "number")) + ? new Uint8Array( + payload.bytes.filter( + (item): item is number => typeof item === "number", + ), + ) : null; if (!bytes || cancelled) return; const blob = new Blob([bytes.buffer as ArrayBuffer], { @@ -124,8 +153,15 @@ export function MessageContent({ content, slot }: MessageContentProps) { if (!cancelled) { setImageUrls((current) => ({ ...current, [attachmentId]: url })); } - } catch { - // Keep an inline placeholder when the attachment is no longer available. + } catch (reason) { + // Used to be swallowed entirely, which left a broken image icon with + // no way to tell a failed download from an undecodable payload. + if (!cancelled) { + setAttachmentErrors((current) => ({ + ...current, + [attachmentId]: errorMessage(reason), + })); + } } }), ); @@ -136,9 +172,17 @@ export function MessageContent({ content, slot }: MessageContentProps) { }; }, [attachmentKey, slot]); - const lines = normalizeContent(content) - .replaceAll("\r\n", "\n") - .split("\n"); + const attachments: AttachmentView = { + urls: imageUrls, + errors: attachmentErrors, + onDecodeFailure: (attachmentId) => + setAttachmentErrors((current) => ({ + ...current, + [attachmentId]: "数据已下载,但不是浏览器能解码的图片格式。", + })), + }; + + const lines = normalizeContent(content).replaceAll("\r\n", "\n").split("\n"); const blocks: ReactNode[] = []; let codeLines: string[] | null = null; let codeLanguage = ""; @@ -149,7 +193,11 @@ export function MessageContent({ content, slot }: MessageContentProps) { if (!paragraph.length) return; blocks.push(

- {renderInline(paragraph.join(" "), `paragraph-${blocks.length}`, imageUrls)} + {renderInline( + paragraph.join(" "), + `paragraph-${blocks.length}`, + attachments, + )}

, ); paragraph = []; @@ -160,7 +208,7 @@ export function MessageContent({ content, slot }: MessageContentProps) {
    {list.map((item, index) => (
  • - {renderInline(item, `list-${blocks.length}-${index}`, imageUrls)} + {renderInline(item, `list-${blocks.length}-${index}`, attachments)}
  • ))}
, @@ -203,7 +251,7 @@ export function MessageContent({ content, slot }: MessageContentProps) { const Heading = `h${level}` as "h1" | "h2" | "h3"; blocks.push( - {renderInline(heading[2], `heading-${index}`, imageUrls)} + {renderInline(heading[2], `heading-${index}`, attachments)} , ); return; diff --git a/src/styles.css b/src/styles.css index 22a0c3c..807855c 100644 --- a/src/styles.css +++ b/src/styles.css @@ -3399,6 +3399,18 @@ button:disabled { background: var(--ui-surface-soft); } +/* Why an attachment did not render, rather than a bare broken-image icon. */ +.message-attachment-failed { + display: inline-flex; + align-items: center; + min-height: 2rem; + padding: 0.35rem 0.55rem; + border: 1px dashed #d88d8d; + border-radius: var(--ui-radius-sm, 0.5rem); + color: #e2a5a5; + font-size: 0.85rem; +} + .message-attachment-placeholder { display: inline-flex; align-items: center;