From 58306011509a0fa1f436d39b2fcc4eef2d4cf940 Mon Sep 17 00:00:00 2001 From: Andrew Phillips Date: Mon, 25 Aug 2025 20:48:09 -0300 Subject: [PATCH] refactor: add get_item_content_info to AsyncItemService and simplify binary checks Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) --- src/modes/server/api/item.rs | 32 ++++++++---------------------- src/services/async_item_service.rs | 12 +++++++++++ 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/modes/server/api/item.rs b/src/modes/server/api/item.rs index 70e1f45..75863f4 100644 --- a/src/modes/server/api/item.rs +++ b/src/modes/server/api/item.rs @@ -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); } } diff --git a/src/services/async_item_service.rs b/src/services/async_item_service.rs index e09820c..21522de 100644 --- a/src/services/async_item_service.rs +++ b/src/services/async_item_service.rs @@ -47,6 +47,18 @@ impl AsyncItemService { .unwrap() } + pub async fn get_item_content_info(&self, id: i64) -> Result<(Vec, String, bool), CoreError> { + let db = self.db.clone(); + let item_service = self.item_service.clone(); + + tokio::task::spawn_blocking(move || { + let conn = db.blocking_lock(); + item_service.get_item_content_info(&conn, id) + }) + .await + .unwrap() + } + pub async fn stream_item_content_by_id( &self,