From 1d53f42d10d72c2877f334023a4ba04754b55608 Mon Sep 17 00:00:00 2001 From: Julian Freeman Date: Sat, 4 Apr 2026 16:01:04 -0400 Subject: [PATCH] add file_delete and copy --- src-tauri/src/lib.rs | 65 ++++++++++++++++++++++++++++------------- src-tauri/src/winget.rs | 12 +++++--- 2 files changed, 52 insertions(+), 25 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 95806c6..eaf064c 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -245,30 +245,53 @@ async fn execute_post_install(handle: &AppHandle, log_id: &str, steps: Vec { - let target_path = expand_win_path(&target); - emit_log(handle, log_id, "File Replace", &format!("{}Downloading configuration file to {:?}...", step_prefix, target_path), "info"); + PostInstallStep::FileCopy { src, dest } => { + let dest_path = expand_win_path(&dest); + let src_is_url = src.starts_with("http://") || src.starts_with("https://"); - let client = reqwest::Client::new(); - match client.get(&url).timeout(std::time::Duration::from_secs(30)).send().await { - Ok(resp) if resp.status().is_success() => { - if let Ok(bytes) = resp.bytes().await { - if let Some(parent) = target_path.parent() { - let _ = fs::create_dir_all(parent); + if src_is_url { + emit_log(handle, log_id, "File Download", &format!("{}Downloading {:?} to {:?}...", step_prefix, src, dest_path), "info"); + let client = reqwest::Client::new(); + match client.get(&src).timeout(std::time::Duration::from_secs(60)).send().await { + Ok(resp) if resp.status().is_success() => { + if let Ok(bytes) = resp.bytes().await { + if let Some(parent) = dest_path.parent() { + let _ = fs::create_dir_all(parent); + } + if let Err(e) = fs::write(&dest_path, bytes) { + emit_log(handle, log_id, "File Error", &format!("Failed to write to {:?}: {}", dest_path, e), "error"); + } else { + emit_log(handle, log_id, "Success", "File downloaded and saved successfully.", "success"); + } } - if let Err(e) = fs::write(&target_path, bytes) { - emit_log(handle, log_id, "File Error", &format!("Failed to write to {:?}: {}", target_path, e), "error"); - } else { - emit_log(handle, log_id, "Success", "File replaced successfully.", "success"); - } - } - }, - Ok(resp) => { - emit_log(handle, log_id, "Download Error", &format!("HTTP Status: {}", resp.status()), "error"); - }, - Err(e) => { - emit_log(handle, log_id, "Download Error", &e.to_string(), "error"); + }, + Ok(resp) => emit_log(handle, log_id, "Download Error", &format!("HTTP Status: {}", resp.status()), "error"), + Err(e) => emit_log(handle, log_id, "Download Error", &e.to_string(), "error"), } + } else { + let src_path = expand_win_path(&src); + emit_log(handle, log_id, "File Copy", &format!("{}Copying {:?} to {:?}...", step_prefix, src_path, dest_path), "info"); + if let Some(parent) = dest_path.parent() { + let _ = fs::create_dir_all(parent); + } + if let Err(e) = fs::copy(&src_path, &dest_path) { + emit_log(handle, log_id, "File Error", &format!("Failed to copy file: {}", e), "error"); + } else { + emit_log(handle, log_id, "Success", "File copied successfully.", "success"); + } + } + }, + PostInstallStep::FileDelete { path } => { + let full_path = expand_win_path(&path); + emit_log(handle, log_id, "File Delete", &format!("{}Deleting {:?}...", step_prefix, full_path), "info"); + if full_path.exists() { + if let Err(e) = fs::remove_file(&full_path) { + emit_log(handle, log_id, "File Error", &format!("Failed to delete file: {}", e), "error"); + } else { + emit_log(handle, log_id, "Success", "File deleted successfully.", "success"); + } + } else { + emit_log(handle, log_id, "File Info", "File does not exist, skipping.", "info"); } }, PostInstallStep::Command { run } => { diff --git a/src-tauri/src/winget.rs b/src-tauri/src/winget.rs index f916f06..7d84565 100644 --- a/src-tauri/src/winget.rs +++ b/src-tauri/src/winget.rs @@ -20,10 +20,14 @@ pub enum PostInstallStep { base_path: String, values: HashMap, }, - #[serde(rename = "file_replace")] - FileReplace { - url: String, - target: String, // 支持 %AppData% 等环境变量占位符 + #[serde(rename = "file_copy")] + FileCopy { + src: String, // 支持 URL 或本地路径(含环境变量) + dest: String, // 目标路径(含环境变量) + }, + #[serde(rename = "file_delete")] + FileDelete { + path: String, // 要删除的文件路径(含环境变量) }, #[serde(rename = "command")] Command {