From c66d83ff76fb3b66fb6bcd790376b185c4376a36 Mon Sep 17 00:00:00 2001 From: Tw93 Date: Sat, 8 Apr 2023 22:06:49 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E5=87=8F=E5=B0=91=E4=BD=93=E7=A7=AF/?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E5=90=8D=E7=A7=B0=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/Cargo.lock | 1 - src-tauri/Cargo.toml | 1 - src-tauri/src/util.rs | 43 +++++++++++++++++++++++++++++++------------ 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index a21994a..00e2d87 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -46,7 +46,6 @@ dependencies = [ "download_rs", "home", "image", - "libc", "serde", "serde_json", "tauri", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index cfc3c7b..2747529 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -21,7 +21,6 @@ tauri = { version = "1.2.4", features = ["api-all", "system-tray"] } image = "0.24.5" home = "0.5" dirs = "5.0" -libc = "0.2" download_rs = { version = "0.2.0", features = ["sync_download"] } tauri-plugin-window-state = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "dev" } diff --git a/src-tauri/src/util.rs b/src-tauri/src/util.rs index e6f4674..b94f83b 100644 --- a/src-tauri/src/util.rs +++ b/src-tauri/src/util.rs @@ -1,7 +1,6 @@ use crate::app::config::PakeConfig; use dirs::config_dir; -use libc::getenv; -use std::ffi::CStr; +use std::env; use std::path::PathBuf; use tauri::{Config, Window}; @@ -36,25 +35,45 @@ pub fn show_toast(window: &Window, message: &str) { } pub fn get_download_message() -> String { - let lang_env_var = unsafe { CStr::from_ptr(getenv(b"LANG\0".as_ptr() as *const i8)) }; - let lang_str = lang_env_var.to_string_lossy().to_lowercase(); - if lang_str.starts_with("zh") { - "下载成功,已保存到下载目录~".to_string() - } else { - "Download successful, saved to download directory~".to_string() - } + let default_message = "Download successful, saved to download directory~"; + let chinese_message = "下载成功,已保存到下载目录~"; + + env::var("LANG") + .map(|lang| { + if lang.starts_with("zh") { + chinese_message + } else { + default_message + } + }) + .unwrap_or(default_message) + .to_string() } // Check if the file exists, if it exists, add a number to file name pub fn check_file_or_append(file_path: &str) -> String { let mut new_path = PathBuf::from(file_path); - let mut counter = 1; + let mut counter = 0; + while new_path.exists() { let file_stem = new_path.file_stem().unwrap().to_string_lossy().to_string(); let extension = new_path.extension().unwrap().to_string_lossy().to_string(); let parent_dir = new_path.parent().unwrap(); - new_path = parent_dir.join(format!("{}-{}.{}", file_stem, counter, extension)); - counter += 1; + + let new_file_stem = match file_stem.rfind('-') { + Some(index) if file_stem[index + 1..].parse::().is_ok() => { + let base_name = &file_stem[..index]; + counter = file_stem[index + 1..].parse::().unwrap() + 1; + format!("{}-{}", base_name, counter) + } + _ => { + counter += 1; + format!("{}-{}", file_stem, counter) + } + }; + + new_path = parent_dir.join(format!("{}.{}", new_file_stem, extension)); } + new_path.to_string_lossy().into_owned() }