use std::path::PathBuf;
use std::sync::Mutex;
use tauri::State;
use crate::state::{VaultViewerState, DeletedFile};
#[tauri::command]
pub async fn delete_file(
path: String,
vault_root: String,
state: State<'_, Mutex<VaultViewerState>>,
) -> Result<(), String> {
let vault_root_path = PathBuf::from(&vault_root);
let mut viewer_state = state.lock().map_err(|e| format!("Failed to lock state: {}", e))?;
viewer_state.delete_file(path, &vault_root_path)
}
#[tauri::command]
pub async fn restore_file(
file_id: String,
vault_root: String,
state: State<'_, Mutex<VaultViewerState>>,
) -> Result<(), String> {
let vault_root_path = PathBuf::from(&vault_root);
let mut viewer_state = state.lock().map_err(|e| format!("Failed to lock state: {}", e))?;
viewer_state.restore_file(&file_id, &vault_root_path)
}
#[tauri::command]
pub async fn permanently_delete(
file_id: String,
vault_root: String,
state: State<'_, Mutex<VaultViewerState>>,
) -> Result<(), String> {
let vault_root_path = PathBuf::from(&vault_root);
let mut viewer_state = state.lock().map_err(|e| format!("Failed to lock state: {}", e))?;
viewer_state.permanently_delete(&file_id, &vault_root_path)
}
#[tauri::command]
pub async fn get_deleted_files(
state: State<'_, Mutex<VaultViewerState>>,
) -> Result<Vec<DeletedFile>, String> {
let viewer_state = state.lock().map_err(|e| format!("Failed to lock state: {}", e))?;
Ok(viewer_state.deleted_files.clone())
}
#[tauri::command]
pub async fn empty_recycle_bin(
vault_root: String,
state: State<'_, Mutex<VaultViewerState>>,
) -> Result<(), String> {
let vault_root_path = PathBuf::from(&vault_root);
let mut viewer_state = state.lock().map_err(|e| format!("Failed to lock state: {}", e))?;
let file_ids: Vec<String> = viewer_state.deleted_files.iter().map(|f| f.id.clone()).collect();
for file_id in file_ids {
viewer_state.permanently_delete(&file_id, &vault_root_path)?;
}
Ok(())
}