use crate::compression_engine::{get_compression_engine, CompressionType}; use crate::services::error::CoreError; use std::io::Read; use std::path::PathBuf; use std::str::FromStr; use anyhow::anyhow; pub struct CompressionService; impl CompressionService { pub fn new() -> Self { Self } pub fn get_item_content(&self, item_path: PathBuf, compression: &str) -> Result, CoreError> { let compression_type = CompressionType::from_str(compression) .map_err(|e| CoreError::Compression(e.to_string()))?; let engine = get_compression_engine(compression_type) .map_err(|e| CoreError::Other(anyhow!(e.to_string())))?; let mut reader = engine.open(item_path.clone()) .map_err(|e| CoreError::Other(anyhow!("Failed to open item file {:?}: {}", item_path, e)))?; let mut content = Vec::new(); reader.read_to_end(&mut content)?; Ok(content) } pub fn stream_item_content( &self, item_path: PathBuf, compression: &str, ) -> Result, CoreError> { let compression_type = CompressionType::from_str(compression) .map_err(|e| CoreError::Compression(e.to_string()))?; let engine = get_compression_engine(compression_type) .map_err(|e| CoreError::Other(anyhow!(e.to_string())))?; let reader = engine.open(item_path.clone()) .map_err(|e| CoreError::Other(anyhow!("Failed to open item file {:?}: {}", item_path, e)))?; // Since we can't guarantee the reader implements Send, we need to wrap it // We'll read the content into a buffer and return a Cursor which is Send // This is not ideal for large files, but it ensures Send is implemented let mut content = Vec::new(); let mut temp_reader = reader; temp_reader.read_to_end(&mut content)?; Ok(Box::new(std::io::Cursor::new(content))) } } impl Default for CompressionService { fn default() -> Self { Self::new() } }