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:
@@ -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());
|
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
|
let item_with_meta = item_service
|
||||||
.find_item(vec![], tags, HashMap::new())
|
.find_item(vec![], tags, HashMap::new())
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
match item_with_meta {
|
match item_with_meta {
|
||||||
Ok(item) => {
|
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(CoreError::ItemNotFoundGeneric) => Err(StatusCode::NOT_FOUND),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
@@ -245,35 +246,18 @@ async fn stream_item_content_response(
|
|||||||
|
|
||||||
// Check if content is binary when allow_binary is false
|
// Check if content is binary when allow_binary is false
|
||||||
if !allow_binary {
|
if !allow_binary {
|
||||||
// Get the binary status using the new method
|
match item_service.get_item_content_info(item_id).await {
|
||||||
let is_binary = match item_service.get_item(item_id).await {
|
Ok((_, _, is_binary)) => {
|
||||||
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_binary {
|
if is_binary {
|
||||||
return Err(StatusCode::BAD_REQUEST);
|
return Err(StatusCode::BAD_REQUEST);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Err(e) => {
|
||||||
|
warn!("Failed to get content info for binary check for item {}: {}", item_id, e);
|
||||||
|
return Err(StatusCode::INTERNAL_SERVER_ERROR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Now stream the content with the specified offset and length
|
// Now stream the content with the specified offset and length
|
||||||
match item_service.stream_item_content_by_id(item_id, true, offset, length).await {
|
match item_service.stream_item_content_by_id(item_id, true, offset, length).await {
|
||||||
|
|||||||
@@ -47,6 +47,18 @@ impl AsyncItemService {
|
|||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_item_content_info(&self, id: i64) -> Result<(Vec<u8>, 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(
|
pub async fn stream_item_content_by_id(
|
||||||
&self,
|
&self,
|
||||||
|
|||||||
Reference in New Issue
Block a user