fix(astrbot): resolve an attachment's type from its bytes when needed
一些检测仍在等待运行
ci / Portable quality checks (push) Waiting to run
ci / Windows Tauri build (push) Waiting to run

An attachment downloaded and then rendered as a broken image. A blob URL
is decoded according to the blob's type, so handing the webview
application/octet-stream makes img refuse bytes that are a perfectly
good PNG — and octet-stream is exactly what the client substitutes when
the server names no type.

Prefer a concrete image type from the server, otherwise identify the
format from its leading bytes. Every signature checked is a fixed marker
at a fixed offset, so this reads no user content, and a JSON error body
served with HTTP 200 is deliberately not mistaken for an image.

Whether that was the cause is not yet established, so the alternative is
made self-diagnosing rather than guessed at. The download outcome —
status, the type the server gave, the type the bytes indicate, and the
length — is now traced, and the frontend reports why an attachment did
not render instead of leaving a bare broken-image icon: a failed
download shows its error, and a payload the browser cannot decode says
so. Both paths were previously swallowed.

Refs: N-6.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
这个提交存在于:
YokiiroBW 2026-07-27 02:29:33 +08:00
父节点 529357523c
当前提交 0b4a7de62a
共有 3 个文件被更改,包括 151 次插入 和 16 次删除

查看文件

@ -491,12 +491,14 @@ impl AstrBotClient {
if !response.status().is_success() { if !response.status().is_success() {
return Err(AstrBotClientError::HttpStatus(response.status())); return Err(AstrBotClientError::HttpStatus(response.status()));
} }
let mime_type = response let status = response.status();
let header_mime = response
.headers() .headers()
.get(CONTENT_TYPE) .get(CONTENT_TYPE)
.and_then(|value| value.to_str().ok()) .and_then(|value| value.to_str().ok())
.and_then(|value| value.split(';').next()) .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") .unwrap_or("application/octet-stream")
.to_owned(); .to_owned();
let bytes = response.bytes().await?.to_vec(); let bytes = response.bytes().await?.to_vec();
@ -505,6 +507,28 @@ impl AstrBotClient {
"attachment size is outside the supported range", "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 `<img>` 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("<not an image>"),
resolved_mime = mime_type.as_str(),
bytes = bytes.len(),
"astrbot attachment download"
);
Ok((mime_type, bytes)) 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 { fn protocol_slot(slot: SlotKind) -> desktop_protocol::ActiveSlotKind {
match slot { match slot {
SlotKind::Companion => desktop_protocol::ActiveSlotKind::Companion, 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"<!DOCTYPE html>"), None);
assert_eq!(sniff_image_mime(b""), None);
}
#[test] #[test]
fn frame_trace_stays_silent_when_the_filter_excludes_it() { fn frame_trace_stays_silent_when_the_filter_excludes_it() {
let sink = CapturedLog::default(); let sink = CapturedLog::default();

查看文件

@ -1,6 +1,7 @@
import { Fragment, useEffect, useState, type ReactNode } from "react"; import { Fragment, useEffect, useState, type ReactNode } from "react";
import { invoke } from "@tauri-apps/api/core"; import { invoke } from "@tauri-apps/api/core";
import { errorMessage } from "../../lib/errorMessage";
import { safeLinkHref } from "../../lib/safeLinkHref"; import { safeLinkHref } from "../../lib/safeLinkHref";
import type { ChatSlot } from "./types"; import type { ChatSlot } from "./types";
@ -25,16 +26,24 @@ const emotionLabels: Record<string, string> = {
}; };
function normalizeContent(content: string): string { function normalizeContent(content: string): string {
return content.replace(/&&([a-zA-Z_-]+)&&/g, (_, name: string) => return content.replace(
emotionLabels[name.toLowerCase()] ?? "", /&&([a-zA-Z_-]+)&&/g,
(_, name: string) => emotionLabels[name.toLowerCase()] ?? "",
); );
} }
interface AttachmentView {
urls: Record<string, string>;
errors: Record<string, string>;
onDecodeFailure: (attachmentId: string) => void;
}
function renderInline( function renderInline(
text: string, text: string,
keyPrefix: string, keyPrefix: string,
imageUrls: Record<string, string>, attachments: AttachmentView,
): ReactNode[] { ): ReactNode[] {
const { urls: imageUrls, errors: attachmentErrors } = attachments;
const tokens = text.split( const tokens = text.split(
/(!\[[^\]]*\]\(astrbot-attachment:\/\/[A-Za-z0-9._-]+\)|`[^`]+`|\*\*[^*]+\*\*|\[[^\]]+\]\([^)]+\))/g, /(!\[[^\]]*\]\(astrbot-attachment:\/\/[A-Za-z0-9._-]+\)|`[^`]+`|\*\*[^*]+\*\*|\[[^\]]+\]\([^)]+\))/g,
); );
@ -44,17 +53,29 @@ function renderInline(
/^!\[([^\]]*)\]\(astrbot-attachment:\/\/([A-Za-z0-9._-]+)\)$/, /^!\[([^\]]*)\]\(astrbot-attachment:\/\/([A-Za-z0-9._-]+)\)$/,
); );
if (image) { 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 (
<span className="message-attachment-failed" key={key} role="status">
图片无法显示:{failure}
</span>
);
}
const source = imageUrls[attachmentId];
return source ? ( return source ? (
<img <img
className="message-attachment-image" className="message-attachment-image"
key={key} key={key}
src={source} src={source}
alt={image[1] || "image"} alt={image[1] || "image"}
onError={() => attachments.onDecodeFailure(attachmentId)}
/> />
) : ( ) : (
<span className="message-attachment-placeholder" key={key}> <span className="message-attachment-placeholder" key={key}>
image loading 图片加载中…
</span> </span>
); );
} }
@ -82,6 +103,9 @@ function renderInline(
export function MessageContent({ content, slot }: MessageContentProps) { export function MessageContent({ content, slot }: MessageContentProps) {
const [imageUrls, setImageUrls] = useState<Record<string, string>>({}); const [imageUrls, setImageUrls] = useState<Record<string, string>>({});
const [attachmentErrors, setAttachmentErrors] = useState<
Record<string, string>
>({});
const attachmentIds = Array.from( const attachmentIds = Array.from(
content.matchAll( content.matchAll(
/!\[[^\]]*\]\(astrbot-attachment:\/\/([A-Za-z0-9._-]+)\)/g, /!\[[^\]]*\]\(astrbot-attachment:\/\/([A-Za-z0-9._-]+)\)/g,
@ -95,6 +119,7 @@ export function MessageContent({ content, slot }: MessageContentProps) {
let cancelled = false; let cancelled = false;
const createdUrls: string[] = []; const createdUrls: string[] = [];
setImageUrls({}); setImageUrls({});
setAttachmentErrors({});
const ids = attachmentKey ? attachmentKey.split("|") : []; const ids = attachmentKey ? attachmentKey.split("|") : [];
if (!ids.length) return undefined; if (!ids.length) return undefined;
@ -110,7 +135,11 @@ export function MessageContent({ content, slot }: MessageContentProps) {
payload.bytes instanceof Uint8Array payload.bytes instanceof Uint8Array
? payload.bytes ? payload.bytes
: Array.isArray(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; : null;
if (!bytes || cancelled) return; if (!bytes || cancelled) return;
const blob = new Blob([bytes.buffer as ArrayBuffer], { const blob = new Blob([bytes.buffer as ArrayBuffer], {
@ -124,8 +153,15 @@ export function MessageContent({ content, slot }: MessageContentProps) {
if (!cancelled) { if (!cancelled) {
setImageUrls((current) => ({ ...current, [attachmentId]: url })); setImageUrls((current) => ({ ...current, [attachmentId]: url }));
} }
} catch { } catch (reason) {
// Keep an inline placeholder when the attachment is no longer available. // 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]); }, [attachmentKey, slot]);
const lines = normalizeContent(content) const attachments: AttachmentView = {
.replaceAll("\r\n", "\n") urls: imageUrls,
.split("\n"); errors: attachmentErrors,
onDecodeFailure: (attachmentId) =>
setAttachmentErrors((current) => ({
...current,
[attachmentId]: "数据已下载,但不是浏览器能解码的图片格式。",
})),
};
const lines = normalizeContent(content).replaceAll("\r\n", "\n").split("\n");
const blocks: ReactNode[] = []; const blocks: ReactNode[] = [];
let codeLines: string[] | null = null; let codeLines: string[] | null = null;
let codeLanguage = ""; let codeLanguage = "";
@ -149,7 +193,11 @@ export function MessageContent({ content, slot }: MessageContentProps) {
if (!paragraph.length) return; if (!paragraph.length) return;
blocks.push( blocks.push(
<p key={`paragraph-${blocks.length}`}> <p key={`paragraph-${blocks.length}`}>
{renderInline(paragraph.join(" "), `paragraph-${blocks.length}`, imageUrls)} {renderInline(
paragraph.join(" "),
`paragraph-${blocks.length}`,
attachments,
)}
</p>, </p>,
); );
paragraph = []; paragraph = [];
@ -160,7 +208,7 @@ export function MessageContent({ content, slot }: MessageContentProps) {
<ul key={`list-${blocks.length}`}> <ul key={`list-${blocks.length}`}>
{list.map((item, index) => ( {list.map((item, index) => (
<li key={`list-${blocks.length}-${index}`}> <li key={`list-${blocks.length}-${index}`}>
{renderInline(item, `list-${blocks.length}-${index}`, imageUrls)} {renderInline(item, `list-${blocks.length}-${index}`, attachments)}
</li> </li>
))} ))}
</ul>, </ul>,
@ -203,7 +251,7 @@ export function MessageContent({ content, slot }: MessageContentProps) {
const Heading = `h${level}` as "h1" | "h2" | "h3"; const Heading = `h${level}` as "h1" | "h2" | "h3";
blocks.push( blocks.push(
<Heading key={`heading-${index}`}> <Heading key={`heading-${index}`}>
{renderInline(heading[2], `heading-${index}`, imageUrls)} {renderInline(heading[2], `heading-${index}`, attachments)}
</Heading>, </Heading>,
); );
return; return;

查看文件

@ -3399,6 +3399,18 @@ button:disabled {
background: var(--ui-surface-soft); 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 { .message-attachment-placeholder {
display: inline-flex; display: inline-flex;
align-items: center; align-items: center;