From 7eefb64d1572c39c77848a19e7c598f67cb4859f Mon Sep 17 00:00:00 2001 From: Andrew Phillips Date: Mon, 25 Aug 2025 20:23:18 -0300 Subject: [PATCH] refactor: extract shared content streaming logic into helper function Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) --- src/modes/server/api/item.rs | 93 ++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 46 deletions(-) diff --git a/src/modes/server/api/item.rs b/src/modes/server/api/item.rs index 5f2df8f..0380f36 100644 --- a/src/modes/server/api/item.rs +++ b/src/modes/server/api/item.rs @@ -175,50 +175,7 @@ pub async fn handle_get_item_latest_content( match item_with_meta { Ok(item) => { - let item_id = item.item.id.unwrap(); - let metadata = item.meta_as_map(); - let mime_type = metadata - .get("mime_type") - .map(|s| s.to_string()) - .unwrap_or_else(|| "application/octet-stream".to_string()); - - // Check if content is binary when allow_binary is false - if !params.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); - } - } - }; - - if is_content_binary { - return Err(StatusCode::BAD_REQUEST); - } - } - - // Now stream the content with the specified offset and length - match item_service.stream_item_content_by_id(item_id, true, params.offset, params.length).await { - Ok((stream, _)) => { - let body = axum::body::Body::from_stream(stream); - let response = Response::builder() - .header(header::CONTENT_TYPE, mime_type) - .body(body) - .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; - Ok(response) - } - Err(e) => { - warn!("Failed to stream content for item {}: {}", item_id, e); - Err(StatusCode::INTERNAL_SERVER_ERROR) - } - } + stream_item_content_response(&item_service, item.item.id.unwrap(), params.allow_binary, params.offset, params.length).await } Err(CoreError::ItemNotFoundGeneric) => Err(StatusCode::NOT_FOUND), Err(e) => { @@ -264,9 +221,53 @@ pub async fn handle_get_item_content( } let item_service = AsyncItemService::new(state.data_dir.clone(), state.db.clone()); + stream_item_content_response(&item_service, item_id, params.allow_binary, params.offset, params.length).await +} - match item_service.stream_item_content_by_id(item_id, params.allow_binary, params.offset, params.length).await { - Ok((stream, mime_type)) => { +async fn stream_item_content_response( + item_service: &AsyncItemService, + item_id: i64, + allow_binary: bool, + offset: u64, + length: u64, +) -> Result { + // First get the item metadata to check if it's binary and get MIME type + let item_with_meta = item_service.get_item(item_id).await.map_err(|e| { + warn!("Failed to get item {} for content: {}", item_id, e); + StatusCode::INTERNAL_SERVER_ERROR + })?; + + let metadata = item_with_meta.meta_as_map(); + let mime_type = metadata + .get("mime_type") + .map(|s| s.to_string()) + .unwrap_or_else(|| "application/octet-stream".to_string()); + + // 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); + } + } + }; + + if is_content_binary { + return Err(StatusCode::BAD_REQUEST); + } + } + + // Now stream the content with the specified offset and length + match item_service.stream_item_content_by_id(item_id, true, offset, length).await { + Ok((stream, _)) => { let body = axum::body::Body::from_stream(stream); let response = Response::builder() .header(header::CONTENT_TYPE, mime_type)