feat: improve binary detection and streaming for item content

Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-25 18:16:56 -03:00
parent 90dd6d7718
commit 97c4e26dbf

View File

@@ -95,7 +95,7 @@ pub async fn handle_list_items(
Ok(Json(response)) Ok(Json(response))
} }
async fn stream_item_content(service: &AsyncItemService, item_id: i64, allow_binary: bool, offset: u64, length: u64) -> anyhow::Result<(ReaderStream<tokio::fs::File>, String)> { async fn stream_item_content(service: &AsyncItemService, item_id: i64, allow_binary: bool, offset: u64, length: u64) -> anyhow::Result<(ReaderStream<tokio::io::Take<tokio::io::BufReader<tokio::fs::File>>>, String)> {
let item_with_meta = service.get_item(item_id).await?; let item_with_meta = service.get_item(item_id).await?;
let metadata = item_with_meta.meta_as_map(); let metadata = item_with_meta.meta_as_map();
@@ -103,8 +103,9 @@ async fn stream_item_content(service: &AsyncItemService, item_id: i64, allow_bin
let is_content_binary = if let Some(binary_meta) = metadata.get("binary") { let is_content_binary = if let Some(binary_meta) = metadata.get("binary") {
binary_meta == "true" binary_meta == "true"
} else { } else {
// If binary metadata not available, we'll need to check the file // If binary metadata not available, we might need to check the file
false // Default to false for now, since we don't want to read the file here // For now, we'll assume non-binary if metadata not present
false
}; };
// If binary content is not allowed and content is binary, return an error // If binary content is not allowed and content is binary, return an error
@@ -119,21 +120,25 @@ async fn stream_item_content(service: &AsyncItemService, item_id: i64, allow_bin
// Open the file for streaming // Open the file for streaming
let file_path = service.data_dir.join(format!("{}.dat", item_id)); let file_path = service.data_dir.join(format!("{}.dat", item_id));
let mut file = tokio::fs::File::open(&file_path).await?; let file = tokio::fs::File::open(&file_path).await?;
let buffered_file = tokio::io::BufReader::new(file);
// Seek to the requested offset // Seek to the requested offset if needed
if offset > 0 { let buffered_file = if offset > 0 {
file.seek(std::io::SeekFrom::Start(offset)).await?; let mut seekable_file = buffered_file;
} seekable_file.seek(std::io::SeekFrom::Start(offset)).await?;
seekable_file
} else {
buffered_file
};
// Create a reader stream with optional length limit // Create a reader stream with optional length limit
let stream = if length > 0 { let stream = if length > 0 {
// Limit the stream to the specified length // Limit the stream to the specified length
let limited_reader = tokio::io::BufReader::new(file).take(length); ReaderStream::new(buffered_file.take(length))
ReaderStream::new(limited_reader)
} else { } else {
// Stream the entire file from the offset // Stream the entire file from the offset
ReaderStream::new(file) ReaderStream::new(buffered_file)
}; };
Ok((stream, mime_type)) Ok((stream, mime_type))