diff --git a/UPDATELOG.md b/UPDATELOG.md index 979d72d0..03e5fe8f 100644 --- a/UPDATELOG.md +++ b/UPDATELOG.md @@ -52,6 +52,7 @@ - 修复 Linux WebKit 网络进程的崩溃 - 修复无法导入订阅 - 修复实际导入成功但显示导入失败的问题 +- 修复删除订阅时未能实际删除相关文件 - 修复 macOS 连接界面显示异常 ## v2.4.2 diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 1220d27c..e14907f2 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -1104,6 +1104,7 @@ version = "2.4.3" dependencies = [ "aes-gcm", "anyhow", + "async-trait", "backoff", "base64 0.22.1", "boa_engine", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 9693c08b..2193d6f0 100755 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -89,6 +89,7 @@ clash_verge_logger = { version = "0.1.0", git = "https://github.com/clash-verge- clash_verge_service_ipc = { version = "2.0.16", features = [ "client", ], git = "https://github.com/clash-verge-rev/clash-verge-service-ipc" } +async-trait = "0.1.89" # clash_verge_service_ipc = { version = "2.0.16", features = [ # "client", # ], path = "../../clash-verge-service-ipc" } diff --git a/src-tauri/src/cmd/profile.rs b/src-tauri/src/cmd/profile.rs index 6a926fae..a86c9ff9 100644 --- a/src-tauri/src/cmd/profile.rs +++ b/src-tauri/src/cmd/profile.rs @@ -1,5 +1,6 @@ use super::CmdResult; use crate::{ + cmd::StringifyErr, config::{ Config, IProfiles, PrfItem, PrfOption, profiles::{ @@ -191,8 +192,10 @@ pub async fn update_profile(index: String, option: Option) -> CmdResu /// 删除配置文件 #[tauri::command] pub async fn delete_profile(index: String) -> CmdResult { + println!("delete_profile: {}", index); // 使用Send-safe helper函数 let should_update = wrap_err!(profiles_delete_item_safe(index.clone()).await)?; + profiles_save_file_safe().await.stringify_err()?; if should_update { match CoreManager::global().update_config().await { diff --git a/src-tauri/src/config/profiles.rs b/src-tauri/src/config/profiles.rs index a33ff0f0..7d95a392 100644 --- a/src-tauri/src/config/profiles.rs +++ b/src-tauri/src/config/profiles.rs @@ -1,7 +1,7 @@ use super::{PrfOption, prfitem::PrfItem}; -use crate::{ - logging_error, - utils::{dirs, help, logging::Type}, +use crate::utils::{ + dirs::{self, PathBufExec}, + help, }; use anyhow::{Context, Result, bail}; use serde::{Deserialize, Serialize}; @@ -290,20 +290,10 @@ impl IProfiles { if let Some(index) = index && let Some(file) = items.remove(index).file { - let _ = dirs::app_profiles_dir().map(async move |path| { - let path = path.join(file); - if path.exists() { - let result = fs::remove_file(path.clone()).await; - if let Err(err) = result { - logging_error!( - Type::Config, - "[配置文件删除] 删除文件 {} 失败: {}", - path.display(), - err - ); - } - } - }); + let _ = dirs::app_profiles_dir()? + .join(file) + .remove_if_exists() + .await; } // get the merge index for (i, _) in items.iter().enumerate() { @@ -315,20 +305,10 @@ impl IProfiles { if let Some(index) = merge_index && let Some(file) = items.remove(index).file { - let _ = dirs::app_profiles_dir().map(async move |path| { - let path = path.join(file); - if path.exists() { - let result = fs::remove_file(path.clone()).await; - if let Err(err) = result { - logging_error!( - Type::Config, - "[配置文件删除] 删除文件 {} 失败: {}", - path.display(), - err - ); - } - } - }); + let _ = dirs::app_profiles_dir()? + .join(file) + .remove_if_exists() + .await; } // get the script index for (i, _) in items.iter().enumerate() { @@ -340,20 +320,10 @@ impl IProfiles { if let Some(index) = script_index && let Some(file) = items.remove(index).file { - let _ = dirs::app_profiles_dir().map(async move |path| { - let path = path.join(file); - if path.exists() { - let result = fs::remove_file(path.clone()).await; - if let Err(err) = result { - logging_error!( - Type::Config, - "[配置文件删除] 删除文件 {} 失败: {}", - path.display(), - err - ); - } - } - }); + let _ = dirs::app_profiles_dir()? + .join(file) + .remove_if_exists() + .await; } // get the rules index for (i, _) in items.iter().enumerate() { @@ -365,20 +335,10 @@ impl IProfiles { if let Some(index) = rules_index && let Some(file) = items.remove(index).file { - let _ = dirs::app_profiles_dir().map(async move |path| { - let path = path.join(file); - if path.exists() { - let result = fs::remove_file(path.clone()).await; - if let Err(err) = result { - logging_error!( - Type::Config, - "[配置文件删除] 删除文件 {} 失败: {}", - path.display(), - err - ); - } - } - }); + let _ = dirs::app_profiles_dir()? + .join(file) + .remove_if_exists() + .await; } // get the proxies index for (i, _) in items.iter().enumerate() { @@ -390,20 +350,10 @@ impl IProfiles { if let Some(index) = proxies_index && let Some(file) = items.remove(index).file { - let _ = dirs::app_profiles_dir().map(async move |path| { - let path = path.join(file); - if path.exists() { - let result = fs::remove_file(path.clone()).await; - if let Err(err) = result { - logging_error!( - Type::Config, - "[配置文件删除] 删除文件 {} 失败: {}", - path.display(), - err - ); - } - } - }); + let _ = dirs::app_profiles_dir()? + .join(file) + .remove_if_exists() + .await; } // get the groups index for (i, _) in items.iter().enumerate() { @@ -415,20 +365,10 @@ impl IProfiles { if let Some(index) = groups_index && let Some(file) = items.remove(index).file { - let _ = dirs::app_profiles_dir().map(async move |path| { - let path = path.join(file); - if path.exists() { - let result = fs::remove_file(path.clone()).await; - if let Err(err) = result { - logging_error!( - Type::Config, - "[配置文件删除] 删除文件 {} 失败: {}", - path.display(), - err - ); - } - } - }); + let _ = dirs::app_profiles_dir()? + .join(file) + .remove_if_exists() + .await; } // delete the original uid if current == uid { diff --git a/src-tauri/src/utils/dirs.rs b/src-tauri/src/utils/dirs.rs index 0b581a4d..f985fe56 100644 --- a/src-tauri/src/utils/dirs.rs +++ b/src-tauri/src/utils/dirs.rs @@ -1,5 +1,6 @@ -use crate::core::handle; +use crate::{core::handle, logging, utils::logging::Type}; use anyhow::Result; +use async_trait::async_trait; use once_cell::sync::OnceCell; use std::{fs, path::PathBuf}; use tauri::Manager; @@ -232,3 +233,18 @@ pub fn ipc_path() -> Result { pub fn ipc_path() -> Result { Ok(PathBuf::from(r"\\.\pipe\verge-mihomo")) } +#[async_trait] +pub trait PathBufExec { + async fn remove_if_exists(&self) -> Result<()>; +} + +#[async_trait] +impl PathBufExec for PathBuf { + async fn remove_if_exists(&self) -> Result<()> { + if self.exists() { + tokio::fs::remove_file(self).await?; + logging!(info, Type::File, "Removed file: {:?}", self); + } + Ok(()) + } +} diff --git a/src-tauri/src/utils/logging.rs b/src-tauri/src/utils/logging.rs index 784a3242..c40daf23 100644 --- a/src-tauri/src/utils/logging.rs +++ b/src-tauri/src/utils/logging.rs @@ -25,6 +25,7 @@ pub enum Type { Timer, Frontend, Backup, + File, Lightweight, Network, ProxyMode, @@ -47,6 +48,7 @@ impl fmt::Display for Type { Type::Timer => write!(f, "[Timer]"), Type::Frontend => write!(f, "[Frontend]"), Type::Backup => write!(f, "[Backup]"), + Type::File => write!(f, "[File]"), Type::Lightweight => write!(f, "[Lightweight]"), Type::Network => write!(f, "[Network]"), Type::ProxyMode => write!(f, "[ProxMode]"),