feat: add stream parameter support for item content responses
Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
This commit is contained in:
@@ -243,7 +243,7 @@ pub async fn handle_get_item_content(
|
|||||||
state.cmd.clone(),
|
state.cmd.clone(),
|
||||||
state.settings.clone()
|
state.settings.clone()
|
||||||
);
|
);
|
||||||
stream_item_content_response(&item_service, item_id, params.allow_binary, params.offset, params.length).await
|
stream_item_content_response(&item_service, item_id, params.allow_binary, params.offset, params.length, params.stream).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn stream_item_content_response(
|
async fn stream_item_content_response(
|
||||||
@@ -252,6 +252,7 @@ async fn stream_item_content_response(
|
|||||||
allow_binary: bool,
|
allow_binary: bool,
|
||||||
offset: u64,
|
offset: u64,
|
||||||
length: u64,
|
length: u64,
|
||||||
|
stream: bool,
|
||||||
) -> Result<Response, StatusCode> {
|
) -> Result<Response, StatusCode> {
|
||||||
// Get the item with metadata once
|
// Get the item with metadata once
|
||||||
let item_with_meta = item_service.get_item(item_id).await.map_err(|e| {
|
let item_with_meta = item_service.get_item(item_id).await.map_err(|e| {
|
||||||
@@ -260,7 +261,7 @@ async fn stream_item_content_response(
|
|||||||
})?;
|
})?;
|
||||||
|
|
||||||
let metadata = item_with_meta.meta_as_map();
|
let metadata = item_with_meta.meta_as_map();
|
||||||
stream_item_content_response_with_metadata(item_service, item_id, &metadata, allow_binary, offset, length).await
|
stream_item_content_response_with_metadata(item_service, item_id, &metadata, allow_binary, offset, length, stream).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn stream_item_content_response_with_metadata(
|
async fn stream_item_content_response_with_metadata(
|
||||||
@@ -270,6 +271,7 @@ async fn stream_item_content_response_with_metadata(
|
|||||||
allow_binary: bool,
|
allow_binary: bool,
|
||||||
offset: u64,
|
offset: u64,
|
||||||
length: u64,
|
length: u64,
|
||||||
|
stream: bool,
|
||||||
) -> Result<Response, StatusCode> {
|
) -> Result<Response, StatusCode> {
|
||||||
let mime_type = metadata
|
let mime_type = metadata
|
||||||
.get("mime_type")
|
.get("mime_type")
|
||||||
@@ -296,7 +298,8 @@ async fn stream_item_content_response_with_metadata(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now stream the content with the specified offset and length using pre-fetched metadata
|
if stream {
|
||||||
|
// Stream the content
|
||||||
match item_service.stream_item_content_by_id_with_metadata(item_id, metadata, true, offset, length).await {
|
match item_service.stream_item_content_by_id_with_metadata(item_id, metadata, true, offset, length).await {
|
||||||
Ok((stream, _)) => {
|
Ok((stream, _)) => {
|
||||||
let body = axum::body::Body::from_stream(stream);
|
let body = axum::body::Body::from_stream(stream);
|
||||||
@@ -311,6 +314,37 @@ async fn stream_item_content_response_with_metadata(
|
|||||||
Err(StatusCode::INTERNAL_SERVER_ERROR)
|
Err(StatusCode::INTERNAL_SERVER_ERROR)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// Build the full response in memory
|
||||||
|
match item_service.get_item_content_info(item_id).await {
|
||||||
|
Ok((content, _, _)) => {
|
||||||
|
// Apply offset and length
|
||||||
|
let content_len = content.len() as u64;
|
||||||
|
let start = std::cmp::min(offset, content_len);
|
||||||
|
let end = if length > 0 {
|
||||||
|
std::cmp::min(start + length, content_len)
|
||||||
|
} else {
|
||||||
|
content_len
|
||||||
|
};
|
||||||
|
|
||||||
|
let response_content = if start < content_len {
|
||||||
|
&content[start as usize..end as usize]
|
||||||
|
} else {
|
||||||
|
&[]
|
||||||
|
};
|
||||||
|
|
||||||
|
let response = Response::builder()
|
||||||
|
.header(header::CONTENT_TYPE, mime_type)
|
||||||
|
.body(axum::body::Body::from(response_content.to_vec()))
|
||||||
|
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||||
|
Ok(response)
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
warn!("Failed to get content for item {}: {}", item_id, e);
|
||||||
|
Err(StatusCode::INTERNAL_SERVER_ERROR)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -128,6 +128,8 @@ pub struct ItemQuery {
|
|||||||
pub offset: u64,
|
pub offset: u64,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub length: u64,
|
pub length: u64,
|
||||||
|
#[serde(default = "default_stream")]
|
||||||
|
pub stream: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, utoipa::ToSchema)]
|
#[derive(Debug, Deserialize, utoipa::ToSchema)]
|
||||||
@@ -139,6 +141,8 @@ pub struct ItemContentQuery {
|
|||||||
pub offset: u64,
|
pub offset: u64,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub length: u64,
|
pub length: u64,
|
||||||
|
#[serde(default = "default_stream")]
|
||||||
|
pub stream: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_bearer_auth(auth_str: &str, expected_password: &str, expected_hash: &Option<String>) -> bool {
|
fn check_bearer_auth(auth_str: &str, expected_password: &str, expected_hash: &Option<String>) -> bool {
|
||||||
|
|||||||
Reference in New Issue
Block a user