diff --git a/src-tauri/src/networking/astrbot_client.rs b/src-tauri/src/networking/astrbot_client.rs index 1b97e44..6520fe4 100644 --- a/src-tauri/src/networking/astrbot_client.rs +++ b/src-tauri/src/networking/astrbot_client.rs @@ -480,26 +480,32 @@ impl AstrBotClient { { return Err(AstrBotClientError::InvalidConfig("invalid attachment id")); } - let response = self - .http - // AstrBot's dashboard fetches a chat image by name via - // `/api/v1/files/content?filename=`. The by-id form - // `/api/v1/files/{id}/content` also exists, but the id carried by - // `attachment_saved` does not address the file — asking for it - // returned the dashboard's own HTML shell with HTTP 200. - .get(self.url(&format!( - "/api/v1/files/content?filename={}", - 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())); - } + // AstrBot's dashboard fetches a chat image by name via + // `/api/v1/files/content?filename=`. The by-id form + // `/api/v1/files/{id}/content` also exists, but the id carried by + // `attachment_saved` does not address the file. + let request_url = self.url(&format!( + "/api/v1/files/content?filename={}", + urlencoding_username(attachment_id) + )); + let response = self.http.get(&request_url).send().await?; 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 .headers() .get(CONTENT_TYPE) @@ -528,6 +534,13 @@ impl AstrBotClient { } else { 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!( target: FRAME_TRACE_TARGET, status = status.as_u16(), @@ -535,6 +548,7 @@ impl AstrBotClient { sniffed = sniffed.unwrap_or(""), resolved_mime = mime_type.as_str(), bytes = bytes.len(), + server_note = server_note.as_str(), "astrbot attachment download" );