feat: optimize item content streaming to reduce redundant database calls
Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
This commit is contained in:
@@ -232,7 +232,7 @@ async fn stream_item_content_response(
|
||||
offset: u64,
|
||||
length: u64,
|
||||
) -> Result<Response, StatusCode> {
|
||||
// First get the item metadata to check if it's binary and get MIME type
|
||||
// Get the item with metadata once
|
||||
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
|
||||
@@ -246,21 +246,26 @@ async fn stream_item_content_response(
|
||||
|
||||
// Check if content is binary when allow_binary is false
|
||||
if !allow_binary {
|
||||
match item_service.get_item_content_info(item_id).await {
|
||||
Ok((_, _, is_binary)) => {
|
||||
if is_binary {
|
||||
return Err(StatusCode::BAD_REQUEST);
|
||||
let is_binary = if let Some(binary_val) = metadata.get("binary") {
|
||||
binary_val == "true"
|
||||
} else {
|
||||
// If binary metadata isn't set, we need to check the content
|
||||
match item_service.get_item_content_info(item_id).await {
|
||||
Ok((_, _, is_binary)) => is_binary,
|
||||
Err(e) => {
|
||||
warn!("Failed to get content info for binary check for item {}: {}", item_id, e);
|
||||
return Err(StatusCode::INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
Err(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);
|
||||
}
|
||||
}
|
||||
|
||||
// Now stream the content with the specified offset and length
|
||||
match item_service.stream_item_content_by_id(item_id, true, offset, length).await {
|
||||
// Now stream the content with the specified offset and length using pre-fetched metadata
|
||||
match item_service.stream_item_content_by_id_with_metadata(item_id, &metadata, true, offset, length).await {
|
||||
Ok((stream, _)) => {
|
||||
let body = axum::body::Body::from_stream(stream);
|
||||
let response = Response::builder()
|
||||
|
||||
Reference in New Issue
Block a user