support clean orogress
This commit is contained in:
@@ -2,8 +2,11 @@ use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::backend::fast_clean::clean_directory_contents;
|
||||
use crate::backend::models::{BrowserProfile, BrowserScanResult, BrowserType, CleanResult};
|
||||
use crate::backend::models::{
|
||||
BrowserProfile, BrowserScanResult, BrowserType, CleanResult, ProjectCleanProgress,
|
||||
};
|
||||
use crate::backend::utils::{format_size, get_dir_size_simple};
|
||||
use tauri::Emitter;
|
||||
|
||||
const BROWSER_CACHE_DIRS: &[&str] = &[
|
||||
"Cache",
|
||||
@@ -80,18 +83,23 @@ pub async fn run_browser_scan(browser: BrowserType) -> Result<BrowserScanResult,
|
||||
pub async fn run_browser_clean(
|
||||
browser: BrowserType,
|
||||
profile_paths: Vec<String>,
|
||||
app_handle: tauri::AppHandle,
|
||||
) -> Result<CleanResult, String> {
|
||||
let user_data_path = browser.get_user_data_path()?;
|
||||
let mut total_freed = 0;
|
||||
let mut success_count = 0;
|
||||
let mut fail_count = 0;
|
||||
let mut approx_completed_bytes = 0;
|
||||
let total_items = profile_paths.len() as u32;
|
||||
|
||||
for profile_dir in profile_paths {
|
||||
for (index, profile_dir) in profile_paths.into_iter().enumerate() {
|
||||
let profile_path = user_data_path.join(&profile_dir);
|
||||
let mut profile_estimated_size = 0;
|
||||
if profile_path.exists() {
|
||||
for sub_dir in BROWSER_CACHE_DIRS {
|
||||
let target = profile_path.join(sub_dir);
|
||||
if target.exists() {
|
||||
profile_estimated_size += get_dir_size_simple(&target);
|
||||
let (freed, success, fail) = clean_directory_contents(&target, None);
|
||||
total_freed += freed;
|
||||
success_count += success;
|
||||
@@ -99,6 +107,17 @@ pub async fn run_browser_clean(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
approx_completed_bytes += profile_estimated_size;
|
||||
let _ = app_handle.emit(
|
||||
"browser-clean-progress",
|
||||
ProjectCleanProgress {
|
||||
completed_items: (index + 1) as u32,
|
||||
total_items,
|
||||
current_item: profile_dir,
|
||||
approx_completed_bytes,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Ok(CleanResult {
|
||||
|
||||
@@ -2,7 +2,9 @@ use std::fs;
|
||||
use std::path::Path;
|
||||
use std::time::{Duration, SystemTime};
|
||||
|
||||
use crate::backend::models::{CleanResult, CleaningConfig, FastScanResult, ScanItem};
|
||||
use tauri::Emitter;
|
||||
|
||||
use crate::backend::models::{CleanResult, CleaningConfig, FastScanResult, ProjectCleanProgress, ScanItem};
|
||||
use crate::backend::utils::format_size;
|
||||
|
||||
fn get_fast_cleaning_configs() -> Vec<CleaningConfig> {
|
||||
@@ -89,22 +91,42 @@ fn get_dir_stats(path: &Path, filter_days: Option<u64>) -> (u64, u32) {
|
||||
(size, count)
|
||||
}
|
||||
|
||||
pub async fn run_fast_clean(selected_paths: Vec<String>) -> Result<CleanResult, String> {
|
||||
let configs = get_fast_cleaning_configs();
|
||||
pub async fn run_fast_clean(
|
||||
selected_paths: Vec<String>,
|
||||
app_handle: tauri::AppHandle,
|
||||
) -> Result<CleanResult, String> {
|
||||
let selected_configs: Vec<CleaningConfig> = get_fast_cleaning_configs()
|
||||
.into_iter()
|
||||
.filter(|config| selected_paths.contains(&config.path))
|
||||
.collect();
|
||||
|
||||
let mut success_count = 0;
|
||||
let mut fail_count = 0;
|
||||
let mut total_freed = 0;
|
||||
let mut approx_completed_bytes = 0;
|
||||
let total_items = selected_configs.len() as u32;
|
||||
|
||||
for config in configs {
|
||||
if selected_paths.contains(&config.path) {
|
||||
let path = Path::new(&config.path);
|
||||
if path.exists() {
|
||||
let (freed, success, fail) = clean_directory_contents(path, config.filter_days);
|
||||
total_freed += freed;
|
||||
success_count += success;
|
||||
fail_count += fail;
|
||||
}
|
||||
for (index, config) in selected_configs.into_iter().enumerate() {
|
||||
let path = Path::new(&config.path);
|
||||
let item_size = get_dir_stats(path, config.filter_days).0;
|
||||
|
||||
if path.exists() {
|
||||
let (freed, success, fail) = clean_directory_contents(path, config.filter_days);
|
||||
total_freed += freed;
|
||||
success_count += success;
|
||||
fail_count += fail;
|
||||
}
|
||||
|
||||
approx_completed_bytes += item_size;
|
||||
let _ = app_handle.emit(
|
||||
"fast-clean-progress",
|
||||
ProjectCleanProgress {
|
||||
completed_items: (index + 1) as u32,
|
||||
total_items,
|
||||
current_item: config.name,
|
||||
approx_completed_bytes,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Ok(CleanResult {
|
||||
|
||||
@@ -18,6 +18,14 @@ pub struct ScanProgress {
|
||||
pub current_path: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Clone)]
|
||||
pub struct ProjectCleanProgress {
|
||||
pub completed_items: u32,
|
||||
pub total_items: u32,
|
||||
pub current_item: String,
|
||||
pub approx_completed_bytes: u64,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CleaningConfig {
|
||||
pub name: String,
|
||||
|
||||
@@ -10,8 +10,11 @@ async fn start_fast_scan() -> backend::models::FastScanResult {
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn start_fast_clean(selected_paths: Vec<String>) -> Result<backend::models::CleanResult, String> {
|
||||
backend::fast_clean::run_fast_clean(selected_paths).await
|
||||
async fn start_fast_clean(
|
||||
selected_paths: Vec<String>,
|
||||
app_handle: tauri::AppHandle,
|
||||
) -> Result<backend::models::CleanResult, String> {
|
||||
backend::fast_clean::run_fast_clean(selected_paths, app_handle).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -66,6 +69,7 @@ async fn start_browser_scan(browser: String) -> Result<backend::models::BrowserS
|
||||
async fn start_browser_clean(
|
||||
browser: String,
|
||||
profiles: Vec<String>,
|
||||
app_handle: tauri::AppHandle,
|
||||
) -> Result<backend::models::CleanResult, String> {
|
||||
let browser_type = if browser == "chrome" {
|
||||
backend::models::BrowserType::Chrome
|
||||
@@ -73,7 +77,7 @@ async fn start_browser_clean(
|
||||
backend::models::BrowserType::Edge
|
||||
};
|
||||
|
||||
backend::browser_clean::run_browser_clean(browser_type, profiles).await
|
||||
backend::browser_clean::run_browser_clean(browser_type, profiles, app_handle).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
|
||||
Reference in New Issue
Block a user