* refactor: improve code quality with clippy fixes and standardized logging - Replace dangerous unwrap()/expect() calls with proper error handling - Standardize logging from log:: to logging\! macro with Type:: classifications - Fix app handle panics with graceful fallback patterns - Improve error resilience across 35+ modules without breaking functionality - Reduce clippy warnings from 300+ to 0 in main library code * chore: update Cargo.toml configuration * refactor: resolve all clippy warnings - Fix Arc clone warnings using explicit Arc::clone syntax across 9 files - Add #[allow(clippy::expect_used)] to test functions for appropriate expect usage - Remove no-effect statements from debug code cleanup - Apply clippy auto-fixes for dbg\! macro removals and path statements - Achieve zero clippy warnings on all targets with -D warnings flag * chore: update Cargo.toml clippy configuration * refactor: simplify macOS job configuration and improve caching * refactor: remove unnecessary async/await from service and proxy functions * refactor: streamline pnpm installation in CI configuration * refactor: simplify error handling and remove unnecessary else statements * refactor: replace async/await with synchronous locks for core management * refactor: add workflow_dispatch trigger to clippy job * refactor: convert async functions to synchronous for service management * refactor: convert async functions to synchronous for UWP tool invocation * fix: change wrong logging * refactor: convert proxy restoration functions to async * Revert "refactor: convert proxy restoration functions to async" This reverts commit b82f5d250b2af7151e4dfd7dd411630b34ed2c18. * refactor: update proxy restoration functions to return Result types * fix: handle errors during proxy restoration and update async function signatures * fix: handle errors during proxy restoration and update async function signatures * refactor: update restore_pac_proxy and restore_sys_proxy functions to async * fix: convert restore_pac_proxy and restore_sys_proxy functions to async * fix: await restore_sys_proxy calls in proxy restoration logic * fix: suppress clippy warnings for unused async functions in proxy restoration * fix: suppress clippy warnings for unused async functions in proxy restoration
92 lines
2.7 KiB
Rust
92 lines
2.7 KiB
Rust
use crate::{
|
|
config::{Config, IVerge},
|
|
core::backup,
|
|
logging_error,
|
|
utils::{dirs::app_home_dir, logging::Type},
|
|
};
|
|
use anyhow::Result;
|
|
use reqwest_dav::list_cmd::ListFile;
|
|
use std::fs;
|
|
|
|
/// Create a backup and upload to WebDAV
|
|
pub async fn create_backup_and_upload_webdav() -> Result<()> {
|
|
let (file_name, temp_file_path) = backup::create_backup().map_err(|err| {
|
|
log::error!(target: "app", "Failed to create backup: {err:#?}");
|
|
err
|
|
})?;
|
|
|
|
if let Err(err) = backup::WebDavClient::global()
|
|
.upload(temp_file_path.clone(), file_name)
|
|
.await
|
|
{
|
|
log::error!(target: "app", "Failed to upload to WebDAV: {err:#?}");
|
|
return Err(err);
|
|
}
|
|
|
|
if let Err(err) = std::fs::remove_file(&temp_file_path) {
|
|
log::warn!(target: "app", "Failed to remove temp file: {err:#?}");
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// List WebDAV backups
|
|
pub async fn list_wevdav_backup() -> Result<Vec<ListFile>> {
|
|
backup::WebDavClient::global().list().await.map_err(|err| {
|
|
log::error!(target: "app", "Failed to list WebDAV backup files: {err:#?}");
|
|
err
|
|
})
|
|
}
|
|
|
|
/// Delete WebDAV backup
|
|
pub async fn delete_webdav_backup(filename: String) -> Result<()> {
|
|
backup::WebDavClient::global()
|
|
.delete(filename)
|
|
.await
|
|
.map_err(|err| {
|
|
log::error!(target: "app", "Failed to delete WebDAV backup file: {err:#?}");
|
|
err
|
|
})
|
|
}
|
|
|
|
/// Restore WebDAV backup
|
|
pub async fn restore_webdav_backup(filename: String) -> Result<()> {
|
|
let verge = Config::verge();
|
|
let verge_data = verge.latest_ref().clone();
|
|
let webdav_url = verge_data.webdav_url.clone();
|
|
let webdav_username = verge_data.webdav_username.clone();
|
|
let webdav_password = verge_data.webdav_password.clone();
|
|
|
|
let backup_storage_path = app_home_dir()
|
|
.map_err(|e| anyhow::anyhow!("Failed to get app home dir: {e}"))?
|
|
.join(&filename);
|
|
backup::WebDavClient::global()
|
|
.download(filename, backup_storage_path.clone())
|
|
.await
|
|
.map_err(|err| {
|
|
log::error!(target: "app", "Failed to download WebDAV backup file: {err:#?}");
|
|
err
|
|
})?;
|
|
|
|
// extract zip file
|
|
let mut zip = zip::ZipArchive::new(fs::File::open(backup_storage_path.clone())?)?;
|
|
zip.extract(app_home_dir()?)?;
|
|
logging_error!(
|
|
Type::Backup,
|
|
true,
|
|
super::patch_verge(
|
|
IVerge {
|
|
webdav_url,
|
|
webdav_username,
|
|
webdav_password,
|
|
..IVerge::default()
|
|
},
|
|
false
|
|
)
|
|
.await
|
|
);
|
|
// 最后删除临时文件
|
|
fs::remove_file(backup_storage_path)?;
|
|
Ok(())
|
|
}
|