From 7ef2ac670b750c6a6f46d4771740393368b13c65 Mon Sep 17 00:00:00 2001 From: Andrew Phillips Date: Mon, 25 Aug 2025 18:50:21 -0300 Subject: [PATCH] fix: resolve lifetime issues and remove unused imports Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) --- src/modes/server/api/item.rs | 4 -- src/services/async_item_service.rs | 76 ++++++------------------------ 2 files changed, 14 insertions(+), 66 deletions(-) diff --git a/src/modes/server/api/item.rs b/src/modes/server/api/item.rs index 14fd5fa..30edd2f 100644 --- a/src/modes/server/api/item.rs +++ b/src/modes/server/api/item.rs @@ -10,10 +10,6 @@ use std::collections::HashMap; use crate::services::async_item_service::AsyncItemService; use crate::services::error::CoreError; use crate::modes::server::common::{AppState, ApiResponse, ItemInfo, TagsQuery, ListItemsQuery, ItemInfoListResponse, ItemInfoResponse, MetadataResponse, ItemQuery, ItemContentQuery}; -use crate::common::is_binary::is_binary; -use tokio::io::{AsyncReadExt, AsyncSeekExt}; -use tokio_util::io::ReaderStream; -use tokio_util::bytes::Bytes; #[utoipa::path( get, diff --git a/src/services/async_item_service.rs b/src/services/async_item_service.rs index 485b1f5..efde841 100644 --- a/src/services/async_item_service.rs +++ b/src/services/async_item_service.rs @@ -52,13 +52,14 @@ impl AsyncItemService { .unwrap() } - pub async fn stream_item_content( + + pub async fn stream_item_content_by_id( &self, item_id: i64, allow_binary: bool, offset: u64, length: u64, - ) -> Result<(impl tokio_stream::Stream>, String), CoreError> { + ) -> Result<(std::pin::Pin> + Send>>, String), CoreError> { let item_with_content = self.get_item_content(item_id).await?; let metadata = item_with_content.item_with_meta.meta_as_map(); @@ -68,7 +69,7 @@ impl AsyncItemService { binary_val == "true" } else { // If binary metadata not available, check the actual content - is_binary(&item_with_content.content) + crate::common::is_binary::is_binary(&item_with_content.content) }; if is_content_binary { @@ -83,74 +84,25 @@ impl AsyncItemService { // Open the file for streaming let file_path = self.data_dir.join(format!("{}.dat", item_id)); - let file = tokio::fs::File::open(&file_path).await?; + let file = tokio::fs::File::open(&file_path).await + .map_err(|e| CoreError::Io(e))?; let mut buffered_file = tokio::io::BufReader::new(file); // Seek to the requested offset if needed if offset > 0 { - buffered_file.seek(std::io::SeekFrom::Start(offset)).await?; + buffered_file.seek(std::io::SeekFrom::Start(offset)).await + .map_err(|e| CoreError::Io(e))?; } // Create a reader stream with optional length limit - // Create a reader stream with optional length limit - let stream = if length > 0 { - // Limit the stream to the specified length - Box::pin(ReaderStream::new(buffered_file.take(length))) as std::pin::Pin> + Send>> - } else { - // Stream the entire file from the offset - Box::pin(ReaderStream::new(buffered_file)) as std::pin::Pin> + Send>> - }; - - Ok((stream, mime_type)) - } - - pub async fn stream_item_content_by_id( - &self, - item_id: i64, - allow_binary: bool, - offset: u64, - length: u64, - ) -> anyhow::Result<(impl tokio_stream::Stream>, String)> { - let item_with_content = self.get_item_content(item_id).await?; - let metadata = item_with_content.item_with_meta.meta_as_map(); - - // 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" + let stream: std::pin::Pin> + Send>> = + if length > 0 { + // Limit the stream to the specified length + Box::pin(ReaderStream::new(buffered_file.take(length))) } else { - // If binary metadata not available, check the actual content - is_binary(&item_with_content.content) + // Stream the entire file from the offset + Box::pin(ReaderStream::new(buffered_file)) }; - - if is_content_binary { - return Err(anyhow::anyhow!("Binary content not allowed")); - } - } - - let mime_type = metadata - .get("mime_type") - .map(|s| s.to_string()) - .unwrap_or_else(|| "application/octet-stream".to_string()); - - // Open the file for streaming - let file_path = self.data_dir.join(format!("{}.dat", item_id)); - let file = tokio::fs::File::open(&file_path).await?; - let mut buffered_file = tokio::io::BufReader::new(file); - - // Seek to the requested offset if needed - if offset > 0 { - buffered_file.seek(std::io::SeekFrom::Start(offset)).await?; - } - - // Create a reader stream with optional length limit - let stream = if length > 0 { - // Limit the stream to the specified length - Box::pin(ReaderStream::new(buffered_file.take(length))) as std::pin::Pin> + Send>> - } else { - // Stream the entire file from the offset - Box::pin(ReaderStream::new(buffered_file)) as std::pin::Pin> + Send>> - }; Ok((stream, mime_type)) }