refactor: add get_item_content_info to AsyncItemService and simplify binary checks

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:48:09 -03:00
parent 04554fe04d
commit 5830601150
2 changed files with 20 additions and 24 deletions

View File

@@ -168,14 +168,15 @@ pub async fn handle_get_item_latest_content(
let item_service = AsyncItemService::new(state.data_dir.clone(), state.db.clone(), state.item_service.clone());
// First get the item metadata to check if it's binary and get MIME type
// First find the item to get its ID
let item_with_meta = item_service
.find_item(vec![], tags, HashMap::new())
.await;
match item_with_meta {
Ok(item) => {
stream_item_content_response(&item_service, item.item.id.unwrap(), params.allow_binary, params.offset, params.length).await
let item_id = item.item.id.unwrap();
stream_item_content_response(&item_service, item_id, params.allow_binary, params.offset, params.length).await
}
Err(CoreError::ItemNotFoundGeneric) => Err(StatusCode::NOT_FOUND),
Err(e) => {
@@ -245,33 +246,16 @@ async fn stream_item_content_response(
// Check if content is binary when allow_binary is false
if !allow_binary {
// 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);
}
}
match item_service.get_item_content_info(item_id).await {
Ok((_, _, is_binary)) => {
if is_binary {
return Err(StatusCode::BAD_REQUEST);
}
}
Err(e) => {
warn!("Failed to get item {} for binary check: {}", item_id, e);
warn!("Failed to get content info for binary check for item {}: {}", item_id, e);
return Err(StatusCode::INTERNAL_SERVER_ERROR);
}
};
if is_binary {
return Err(StatusCode::BAD_REQUEST);
}
}