use crate::services::error::CoreError; use crate::services::item_service::ItemService; use crate::services::types::{ItemWithContent, ItemWithMeta}; use rusqlite::Connection; use std::collections::HashMap; use std::path::PathBuf; use std::str::FromStr; use std::sync::Arc; use tokio::sync::Mutex; use tokio::io::AsyncReadExt; use tokio_util::io::ReaderStream; /// 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 /// on a dedicated thread pool, preventing them from blocking the async runtime. #[allow(dead_code)] pub struct AsyncItemService { pub data_dir: PathBuf, db: Arc>, item_service: Arc, } #[allow(dead_code)] impl AsyncItemService { pub fn new(data_dir: PathBuf, db: Arc>, item_service: Arc) -> Self { Self { data_dir, db, item_service } } pub async fn get_item(&self, id: i64) -> Result { let db = self.db.clone(); let item_service = self.item_service.clone(); tokio::task::spawn_blocking(move || { let conn = db.blocking_lock(); item_service.get_item(&conn, id) }) .await .unwrap() // Propagate panics from spawn_blocking } pub async fn get_item_content(&self, id: i64) -> Result { let db = self.db.clone(); let item_service = self.item_service.clone(); tokio::task::spawn_blocking(move || { let conn = db.blocking_lock(); item_service.get_item_content(&conn, id) }) .await .unwrap() } pub async fn stream_item_content_by_id( &self, item_id: i64, allow_binary: bool, offset: u64, length: u64, ) -> Result<(std::pin::Pin> + Send>>, String), CoreError> { 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 || { let conn = db.blocking_lock(); item_service.get_item_content(&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())); } } // 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 let start = std::cmp::min(offset, content_len); let end = if length > 0 { std::cmp::min(start + length, content_len) } 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)) } pub async fn find_item( &self, ids: Vec, tags: Vec, meta: HashMap, ) -> Result { let db = self.db.clone(); let item_service = self.item_service.clone(); tokio::task::spawn_blocking(move || { let conn = db.blocking_lock(); item_service.find_item(&conn, &ids, &tags, &meta) }) .await .unwrap() } pub async fn list_items( &self, tags: Vec, meta: HashMap, ) -> Result, CoreError> { let db = self.db.clone(); let item_service = self.item_service.clone(); tokio::task::spawn_blocking(move || { let conn = db.blocking_lock(); item_service.list_items(&conn, &tags, &meta) }) .await .unwrap() } pub async fn delete_item(&self, id: i64) -> Result<(), CoreError> { let db = self.db.clone(); let item_service = self.item_service.clone(); tokio::task::spawn_blocking(move || { let mut conn = db.blocking_lock(); item_service.delete_item(&mut conn, id) }) .await .unwrap() } pub async fn save_item_from_mcp( &self, content: Vec, tags: Vec, metadata: HashMap, ) -> Result { let db = self.db.clone(); let item_service = self.item_service.clone(); tokio::task::spawn_blocking(move || { let mut conn = db.blocking_lock(); item_service.save_item_from_mcp(&content, &tags, &metadata, &mut conn) }) .await .unwrap() } }