refactor: extract content info logic to item_service

Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-25 20:46:30 -03:00
parent 52f707d1dd
commit 04554fe04d
3 changed files with 46 additions and 34 deletions

View File

@@ -245,22 +245,32 @@ async fn stream_item_content_response(
// Check if content is binary when allow_binary is false
if !allow_binary {
let is_content_binary = if let Some(binary_val) = metadata.get("binary") {
binary_val == "true"
} else {
// For the binary check, we need to read a sample of the content
match item_service.get_item_content(item_id).await {
Ok(item_with_content) => {
crate::common::is_binary::is_binary(&item_with_content.content)
}
Err(e) => {
warn!("Failed to get content for binary check for item {}: {}", item_id, e);
return Err(StatusCode::INTERNAL_SERVER_ERROR);
// Get the binary status using the new method
let is_binary = match item_service.get_item(item_id).await {
Ok(item_with_meta) => {
let metadata = item_with_meta.meta_as_map();
if let Some(binary_val) = metadata.get("binary") {
binary_val == "true"
} else {
// Fall back to checking the content directly
match item_service.get_item_content(item_id).await {
Ok(item_with_content) => {
crate::common::is_binary::is_binary(&item_with_content.content)
}
Err(e) => {
warn!("Failed to get content for binary check for item {}: {}", item_id, e);
return Err(StatusCode::INTERNAL_SERVER_ERROR);
}
}
}
}
Err(e) => {
warn!("Failed to get item {} for binary check: {}", item_id, e);
return Err(StatusCode::INTERNAL_SERVER_ERROR);
}
};
if is_content_binary {
if is_binary {
return Err(StatusCode::BAD_REQUEST);
}
}