fix: update warnings to use log::warn
Co-authored-by: aider (openai/andrew/openrouter/mistralai/mistral-medium-3.1) <aider@aider.chat>
This commit is contained in:
@@ -8,6 +8,7 @@ use std::io::Read;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Mutex;
|
||||
use log::warn;
|
||||
|
||||
/// An asynchronous wrapper around the `ItemService` for use in async contexts like the web server.
|
||||
/// It uses `tokio::task::spawn_blocking` to run synchronous database and filesystem operations
|
||||
@@ -61,7 +62,6 @@ impl AsyncItemService {
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
|
||||
pub async fn stream_item_content_by_id(
|
||||
&self,
|
||||
item_id: i64,
|
||||
@@ -80,10 +80,10 @@ impl AsyncItemService {
|
||||
})
|
||||
.await
|
||||
.unwrap()?;
|
||||
|
||||
|
||||
// Clone content for use in the binary check closure
|
||||
let content_clone = content.clone();
|
||||
|
||||
|
||||
// Get metadata to determine MIME type and binary status
|
||||
let (mime_type, is_binary) = {
|
||||
let db = self.db.clone();
|
||||
@@ -92,32 +92,32 @@ impl AsyncItemService {
|
||||
let conn = db.blocking_lock();
|
||||
let item_with_meta = item_service.get_item(&conn, item_id)?;
|
||||
let metadata = 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());
|
||||
|
||||
|
||||
let is_binary = if let Some(binary_val) = metadata.get("binary") {
|
||||
binary_val == "true"
|
||||
} else {
|
||||
crate::common::is_binary::is_binary(&content_clone)
|
||||
};
|
||||
|
||||
|
||||
Ok::<_, CoreError>((mime_type, is_binary))
|
||||
})
|
||||
.await
|
||||
.unwrap()?
|
||||
};
|
||||
|
||||
|
||||
// Check if content is binary when allow_binary is false
|
||||
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_len = content.len() as u64;
|
||||
|
||||
|
||||
// Apply offset and length constraints
|
||||
let start = std::cmp::min(offset, content_len);
|
||||
let end = if length > 0 {
|
||||
@@ -125,14 +125,14 @@ impl AsyncItemService {
|
||||
} else {
|
||||
content_len
|
||||
};
|
||||
|
||||
|
||||
let stream = if start < content_len {
|
||||
let chunk = tokio_util::bytes::Bytes::from(content[start as usize..end as usize].to_vec());
|
||||
Box::pin(tokio_stream::iter(vec![Ok(chunk)]))
|
||||
} else {
|
||||
Box::pin(tokio_stream::iter(vec![]))
|
||||
};
|
||||
|
||||
|
||||
Ok((stream, mime_type))
|
||||
}
|
||||
|
||||
@@ -152,7 +152,7 @@ impl AsyncItemService {
|
||||
.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_binary = if let Some(binary_val) = metadata.get("binary") {
|
||||
@@ -162,12 +162,12 @@ impl AsyncItemService {
|
||||
let (_, _, is_binary) = self.get_item_content_info_streaming(item_id).await?;
|
||||
is_binary
|
||||
};
|
||||
|
||||
|
||||
if is_binary {
|
||||
return Err(CoreError::InvalidInput("Binary content not allowed".to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Get a streaming reader for the content
|
||||
let reader = {
|
||||
let db = self.db.clone();
|
||||
@@ -176,28 +176,28 @@ impl AsyncItemService {
|
||||
let conn = db.blocking_lock();
|
||||
let item_with_meta = item_service.get_item(&conn, item_id)?;
|
||||
let item_id_val = item_with_meta.item.id.ok_or_else(|| CoreError::InvalidInput("Item missing ID".to_string()))?;
|
||||
|
||||
|
||||
let mut item_path = item_service.get_data_path().clone();
|
||||
item_path.push(item_id_val.to_string());
|
||||
|
||||
|
||||
let reader = item_service.get_compression_service().stream_item_content(
|
||||
item_path,
|
||||
item_path,
|
||||
&item_with_meta.item.compression
|
||||
)?;
|
||||
|
||||
|
||||
Ok::<_, CoreError>(reader)
|
||||
})
|
||||
.await
|
||||
.unwrap()?
|
||||
};
|
||||
|
||||
|
||||
// Convert the reader into an async stream manually
|
||||
// Since ReaderStream requires AsyncRead, we'll create our own implementation
|
||||
use tokio_util::bytes::Bytes;
|
||||
|
||||
|
||||
// Create a channel to stream data between the blocking thread and async runtime
|
||||
let (tx, rx) = tokio::sync::mpsc::channel(1);
|
||||
|
||||
|
||||
// Spawn a blocking task to read from the reader and send chunks
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let mut reader = reader;
|
||||
@@ -217,11 +217,11 @@ impl AsyncItemService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Read and send data up to the specified length
|
||||
let mut remaining_length = length;
|
||||
let mut buffer = [0; PIPESIZE];
|
||||
|
||||
|
||||
loop {
|
||||
// Determine how much to read in this iteration
|
||||
let to_read = if length > 0 {
|
||||
@@ -230,11 +230,11 @@ impl AsyncItemService {
|
||||
} else {
|
||||
buffer.len()
|
||||
};
|
||||
|
||||
|
||||
if to_read == 0 {
|
||||
break; // We've read the requested length
|
||||
}
|
||||
|
||||
|
||||
match reader.read(&mut buffer[..to_read]) {
|
||||
Ok(0) => break, // EOF
|
||||
Ok(n) => {
|
||||
@@ -257,10 +257,10 @@ impl AsyncItemService {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// Convert the receiver into a stream
|
||||
let stream = tokio_stream::wrappers::ReceiverStream::new(rx);
|
||||
|
||||
|
||||
Ok((Box::pin(stream), mime_type))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user