feat: use item content for binary detection

Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-25 18:39:59 -03:00
parent 06c6971d07
commit c3dd017b25

View File

@@ -208,28 +208,16 @@ async fn stream_item_content(
offset: u64, offset: u64,
length: u64, length: u64,
) -> anyhow::Result<(impl tokio_stream::Stream<Item = Result<bytes::Bytes, std::io::Error>>, String)> { ) -> anyhow::Result<(impl tokio_stream::Stream<Item = Result<bytes::Bytes, std::io::Error>>, String)> {
let item_with_meta = service.get_item(item_id).await?; let item_with_content = service.get_item_content(item_id).await?;
let metadata = item_with_meta.meta_as_map(); let metadata = item_with_content.item_with_meta.meta_as_map();
// 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 {
let is_content_binary = if let Some(binary_meta) = metadata.get("binary") { let is_content_binary = if let Some(binary_val) = metadata.get("binary") {
binary_meta == "true" binary_val == "true"
} else { } else {
// If binary metadata not available, we need to check the file content // If binary metadata not available, check the actual content
// Read the first 8192 bytes to determine if it's binary is_binary(&item_with_content.content)
let file_path = service.data_dir.join(format!("{}.dat", item_id));
if let Ok(mut file) = tokio::fs::File::open(&file_path).await {
let mut buffer = vec![0; 8192];
if let Ok(bytes_read) = file.read(&mut buffer).await {
buffer.truncate(bytes_read);
is_binary(&buffer)
} else {
false // Default to non-binary if we can't read
}
} else {
false // Default to non-binary if we can't open the file
}
}; };
if is_content_binary { if is_content_binary {