use crate::state::SessionState; use crate::store::StoreOps; use serde::Serialize; use serde_json::json; use std::fs; use std::path::PathBuf; const KEY_LAST_PROJECT: &str = "last_project_path"; const KEY_SELECTED_MODEL: &str = "selected_model"; const KEY_KNOWN_PROJECTS: &str = "known_projects"; /// Resolves a relative path against the active project root (pure function for testing). /// Returns error if path attempts traversal (..). fn resolve_path_impl(root: PathBuf, relative_path: &str) -> Result { if relative_path.contains("..") { return Err("Security Violation: Directory traversal ('..') is not allowed.".to_string()); } Ok(root.join(relative_path)) } /// Resolves a relative path against the active project root. /// Returns error if no project is open or if path attempts traversal (..). fn resolve_path(state: &SessionState, relative_path: &str) -> Result { let root = state.get_project_root()?; resolve_path_impl(root, relative_path) } /// Validate that a path exists and is a directory (pure function for testing) async fn validate_project_path(path: PathBuf) -> Result<(), String> { tokio::task::spawn_blocking(move || { if !path.exists() { return Err(format!("Path does not exist: {}", path.display())); } if !path.is_dir() { return Err(format!("Path is not a directory: {}", path.display())); } Ok(()) }) .await .map_err(|e| format!("Task failed: {}", e))? } pub async fn open_project( path: String, state: &SessionState, store: &dyn StoreOps, ) -> Result { let p = PathBuf::from(&path); validate_project_path(p.clone()).await?; { let mut root = state.project_root.lock().map_err(|e| e.to_string())?; *root = Some(p); } store.set(KEY_LAST_PROJECT, json!(path)); let mut known_projects = get_known_projects(store)?; known_projects.retain(|p| p != &path); known_projects.insert(0, path.clone()); store.set(KEY_KNOWN_PROJECTS, json!(known_projects)); store.save()?; Ok(path) } pub fn close_project(state: &SessionState, store: &dyn StoreOps) -> Result<(), String> { { let mut root = state.project_root.lock().map_err(|e| e.to_string())?; *root = None; } store.delete(KEY_LAST_PROJECT); store.save()?; Ok(()) } pub fn get_current_project( state: &SessionState, store: &dyn StoreOps, ) -> Result, String> { { let root = state.project_root.lock().map_err(|e| e.to_string())?; if let Some(path) = &*root { return Ok(Some(path.to_string_lossy().to_string())); } } if let Some(path_str) = store .get(KEY_LAST_PROJECT) .as_ref() .and_then(|val| val.as_str()) { let p = PathBuf::from(path_str); if p.exists() && p.is_dir() { let mut root = state.project_root.lock().map_err(|e| e.to_string())?; *root = Some(p); return Ok(Some(path_str.to_string())); } } Ok(None) } pub fn get_known_projects(store: &dyn StoreOps) -> Result, String> { let projects = store .get(KEY_KNOWN_PROJECTS) .and_then(|val| val.as_array().cloned()) .unwrap_or_default() .into_iter() .filter_map(|val| val.as_str().map(|s| s.to_string())) .collect(); Ok(projects) } pub fn get_model_preference(store: &dyn StoreOps) -> Result, String> { if let Some(model) = store .get(KEY_SELECTED_MODEL) .as_ref() .and_then(|val| val.as_str()) { return Ok(Some(model.to_string())); } Ok(None) } pub fn set_model_preference(model: String, store: &dyn StoreOps) -> Result<(), String> { store.set(KEY_SELECTED_MODEL, json!(model)); store.save()?; Ok(()) } async fn read_file_impl(full_path: PathBuf) -> Result { tokio::task::spawn_blocking(move || { fs::read_to_string(&full_path).map_err(|e| format!("Failed to read file: {}", e)) }) .await .map_err(|e| format!("Task failed: {}", e))? } pub async fn read_file(path: String, state: &SessionState) -> Result { let full_path = resolve_path(state, &path)?; read_file_impl(full_path).await } async fn write_file_impl(full_path: PathBuf, content: String) -> Result<(), String> { tokio::task::spawn_blocking(move || { if let Some(parent) = full_path.parent() { fs::create_dir_all(parent) .map_err(|e| format!("Failed to create directories: {}", e))?; } fs::write(&full_path, content).map_err(|e| format!("Failed to write file: {}", e)) }) .await .map_err(|e| format!("Task failed: {}", e))? } pub async fn write_file(path: String, content: String, state: &SessionState) -> Result<(), String> { let full_path = resolve_path(state, &path)?; write_file_impl(full_path, content).await } #[derive(Serialize, Debug, poem_openapi::Object)] pub struct FileEntry { pub name: String, pub kind: String, } async fn list_directory_impl(full_path: PathBuf) -> Result, String> { tokio::task::spawn_blocking(move || { let entries = fs::read_dir(&full_path).map_err(|e| format!("Failed to read dir: {}", e))?; let mut result = Vec::new(); for entry in entries { let entry = entry.map_err(|e| e.to_string())?; let ft = entry.file_type().map_err(|e| e.to_string())?; let name = entry.file_name().to_string_lossy().to_string(); result.push(FileEntry { name, kind: if ft.is_dir() { "dir".to_string() } else { "file".to_string() }, }); } result.sort_by(|a, b| match (a.kind.as_str(), b.kind.as_str()) { ("dir", "file") => std::cmp::Ordering::Less, ("file", "dir") => std::cmp::Ordering::Greater, _ => a.name.cmp(&b.name), }); Ok(result) }) .await .map_err(|e| format!("Task failed: {}", e))? } pub async fn list_directory(path: String, state: &SessionState) -> Result, String> { let full_path = resolve_path(state, &path)?; list_directory_impl(full_path).await }