add file_delete and copy

This commit is contained in:
Julian Freeman
2026-04-04 16:01:04 -04:00
parent c230847cc0
commit 1d53f42d10
2 changed files with 52 additions and 25 deletions

View File

@@ -245,30 +245,53 @@ async fn execute_post_install(handle: &AppHandle, log_id: &str, steps: Vec<PostI
}
}
},
PostInstallStep::FileReplace { url, target } => {
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 } => {

View File

@@ -20,10 +20,14 @@ pub enum PostInstallStep {
base_path: String,
values: HashMap<String, RegistryValue>,
},
#[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 {