feat(astrbot): record the attachment request and the server's reply
一些检测仍在等待运行
ci / Portable quality checks (push) Waiting to run
ci / Windows Tauri build (push) Waiting to run

Probing the server settled that the endpoints exist: unauthenticated,
/api/v1/files/content?filename= and /api/v1/files/{id}/content both
answer 401 JSON, while an unknown /api/v1 path answers 404 JSON. AstrBot
does not serve an HTML shell for API paths at all.

Our authenticated request nevertheless logs 200 with text/html and 201
bytes, and 201 bytes is exactly the length of AstrBot's plain-text 404
body. That combination cannot be explained from the code: the base URL
has its trailing slashes stripped and is written back, redirects are not
followed, and a non-2xx status returns before the log line.

So record the URL actually requested, the status before the status
checks, and — when the body is not an image — the first 160 bytes of it,
which is the server explaining itself rather than anything the user
wrote.

Refs: N-6.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
这个提交存在于:
YokiiroBW 2026-07-27 03:10:14 +08:00
父节点 d1e4cdf471
当前提交 d5dfcd465e

查看文件

@ -480,26 +480,32 @@ impl AstrBotClient {
{ {
return Err(AstrBotClientError::InvalidConfig("invalid attachment id")); return Err(AstrBotClientError::InvalidConfig("invalid attachment id"));
} }
let response = self // AstrBot's dashboard fetches a chat image by name via
.http // `/api/v1/files/content?filename=`. The by-id form
// AstrBot's dashboard fetches a chat image by name via // `/api/v1/files/{id}/content` also exists, but the id carried by
// `/api/v1/files/content?filename=`. The by-id form // `attachment_saved` does not address the file.
// `/api/v1/files/{id}/content` also exists, but the id carried by let request_url = self.url(&format!(
// `attachment_saved` does not address the file — asking for it "/api/v1/files/content?filename={}",
// returned the dashboard's own HTML shell with HTTP 200. urlencoding_username(attachment_id)
.get(self.url(&format!( ));
"/api/v1/files/content?filename={}", let response = self.http.get(&request_url).send().await?;
urlencoding_username(attachment_id)
)))
.send()
.await?;
if response.status().as_u16() == 401 || response.status().as_u16() == 403 {
return Err(AstrBotClientError::HttpStatus(response.status()));
}
if !response.status().is_success() {
return Err(AstrBotClientError::HttpStatus(response.status()));
}
let status = response.status(); let status = response.status();
// Logged before the status checks: a rejected request is exactly the
// case where the path being asked for is the thing in question. The URL
// holds a file name, not a credential — the bearer token lives in a
// header.
tracing::debug!(
target: FRAME_TRACE_TARGET,
url = request_url.as_str(),
status = status.as_u16(),
"astrbot attachment request"
);
if status.as_u16() == 401 || status.as_u16() == 403 {
return Err(AstrBotClientError::HttpStatus(status));
}
if !status.is_success() {
return Err(AstrBotClientError::HttpStatus(status));
}
let header_mime = response let header_mime = response
.headers() .headers()
.get(CONTENT_TYPE) .get(CONTENT_TYPE)
@ -528,6 +534,13 @@ impl AstrBotClient {
} else { } else {
header_mime.clone() header_mime.clone()
}; };
// When the body is not an image it is the server explaining why, which
// is diagnostic text rather than anything the user wrote.
let server_note = if sniffed.is_none() {
String::from_utf8_lossy(&bytes[..bytes.len().min(160)]).into_owned()
} else {
String::new()
};
tracing::debug!( tracing::debug!(
target: FRAME_TRACE_TARGET, target: FRAME_TRACE_TARGET,
status = status.as_u16(), status = status.as_u16(),
@ -535,6 +548,7 @@ impl AstrBotClient {
sniffed = sniffed.unwrap_or("<not an image>"), sniffed = sniffed.unwrap_or("<not an image>"),
resolved_mime = mime_type.as_str(), resolved_mime = mime_type.as_str(),
bytes = bytes.len(), bytes = bytes.len(),
server_note = server_note.as_str(),
"astrbot attachment download" "astrbot attachment download"
); );