refactor: extract content info logic to item_service
Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
This commit is contained in:
@@ -58,37 +58,20 @@ impl AsyncItemService {
|
||||
let db = self.db.clone();
|
||||
let item_service = self.item_service.clone();
|
||||
|
||||
// Get item metadata first to check binary status and get MIME type
|
||||
let item_with_content = tokio::task::spawn_blocking(move || {
|
||||
// Get item content, mime type, and binary status
|
||||
let (content, mime_type, is_binary) = tokio::task::spawn_blocking(move || {
|
||||
let conn = db.blocking_lock();
|
||||
item_service.get_item_content(&conn, item_id)
|
||||
item_service.get_item_content_info(&conn, item_id)
|
||||
})
|
||||
.await
|
||||
.unwrap()?;
|
||||
|
||||
let metadata = item_with_content.item_with_meta.meta_as_map();
|
||||
let mime_type = metadata
|
||||
.get("mime_type")
|
||||
.map(|s| s.to_string())
|
||||
.unwrap_or_else(|| "application/octet-stream".to_string());
|
||||
|
||||
// Check if content is binary when allow_binary is false
|
||||
if !allow_binary {
|
||||
let is_content_binary = if let Some(binary_val) = metadata.get("binary") {
|
||||
binary_val == "true"
|
||||
} else {
|
||||
// For the binary check, we need to read a sample of the content
|
||||
// We'll read the first 8192 bytes to determine if it's binary
|
||||
crate::common::is_binary::is_binary(&item_with_content.content)
|
||||
};
|
||||
|
||||
if is_content_binary {
|
||||
return Err(CoreError::InvalidInput("Binary content not allowed".to_string()));
|
||||
}
|
||||
if !allow_binary && is_binary {
|
||||
return Err(CoreError::InvalidInput("Binary content not allowed".to_string()));
|
||||
}
|
||||
|
||||
// Create a stream that reads only the requested portion
|
||||
let content = item_with_content.content;
|
||||
let content_len = content.len() as u64;
|
||||
|
||||
// Apply offset and length constraints
|
||||
|
||||
Reference in New Issue
Block a user