Move things to file_helpers

These were meant to be here anyway but I didn't put it here because other similar methods were in system_helpers
This commit is contained in:
Benj
2022-07-12 14:28:08 +08:00
parent ba2a8b7fec
commit a703843eed
5 changed files with 54 additions and 35 deletions

View File

@@ -59,3 +59,33 @@ pub fn copy_file(path: String, new_path: String) -> bool {
}
}
}
#[tauri::command]
pub fn copy_file_with_new_name(path: String, new_path: String, new_name: String) -> bool {
let mut new_path_buf = std::path::PathBuf::from(&new_path);
// If the new path doesn't exist, create it.
if !dir_exists(new_path_buf.pop().to_string().as_str()) {
std::fs::create_dir_all(&new_path).unwrap();
}
// Copy old to new
match std::fs::copy(&path, format!("{}/{}", new_path, new_name)) {
Ok(_) => true,
Err(e) => {
println!("Failed to copy file: {}", e);
false
}
}
}
#[tauri::command]
pub fn delete_file(path: String) -> bool {
match std::fs::remove_file(path) {
Ok(_) => return true,
Err(e) => {
println!("Failed to delete file: {}", e);
return false
}
};
}

View File

@@ -40,9 +40,6 @@ fn main() {
system_helpers::run_program,
system_helpers::run_jar,
system_helpers::open_in_browser,
system_helpers::copy_file,
system_helpers::copy_file_with_new_name,
system_helpers::delete_file,
system_helpers::install_location,
system_helpers::is_elevated,
proxy::set_proxy_addr,
@@ -53,6 +50,8 @@ fn main() {
file_helpers::dir_is_empty,
file_helpers::dir_delete,
file_helpers::copy_file,
file_helpers::copy_file_with_new_name,
file_helpers::delete_file,
file_helpers::are_files_identical,
downloader::download_file,
downloader::stop_download,

View File

@@ -38,37 +38,6 @@ pub fn open_in_browser(url: String) {
};
}
#[tauri::command]
pub fn copy_file_with_new_name(path: String, new_path: String, new_name: String) -> bool {
let mut new_path_buf = std::path::PathBuf::from(&new_path);
// If the new path doesn't exist, create it.
if !file_helpers::dir_exists(new_path_buf.pop().to_string().as_str()) {
std::fs::create_dir_all(&new_path).unwrap();
}
// Copy old to new
match std::fs::copy(&path, format!("{}/{}", new_path, new_name)) {
Ok(_) => true,
Err(e) => {
println!("Failed to copy file: {}", e);
false
}
}
}
#[tauri::command]
pub fn delete_file(path: String) -> bool {
match std::fs::remove_file(path) {
Ok(_) => return true,
Err(e) => {
println!("Failed to delete file: {}", e);
return false
}
};
}
#[tauri::command]
pub fn install_location() -> String {
let mut exe_path = std::env::current_exe().unwrap();