@@ -421,6 +421,98 @@ fn clear_cache(app: tauri::AppHandle) -> Result<String, String> {
421421 Ok ( format ! ( "已清除 {} 个文件,共 {:.2} MB" , cleared_files, cleared_size as f64 / 1024.0 / 1024.0 ) )
422422}
423423
424+ /// 检查并执行自动清除缓存
425+ #[ tauri:: command]
426+ fn check_auto_clear_cache ( app : tauri:: AppHandle ) -> Result < bool , String > {
427+ let config_dir = app. path ( ) . app_config_dir ( )
428+ . map_err ( |e| format ! ( "Failed to get config dir: {}" , e) ) ?;
429+
430+ let config_file = config_dir. join ( "config.json" ) ;
431+
432+ if !config_file. exists ( ) {
433+ return Ok ( false ) ;
434+ }
435+
436+ let config_content = std:: fs:: read_to_string ( & config_file)
437+ . map_err ( |e| format ! ( "Failed to read config: {}" , e) ) ?;
438+
439+ let config: serde_json:: Value = serde_json:: from_str ( & config_content)
440+ . map_err ( |e| format ! ( "Failed to parse config: {}" , e) ) ?;
441+
442+ let auto_clear_days = config. get ( "autoClearCacheDays" )
443+ . and_then ( |v| v. as_u64 ( ) )
444+ . unwrap_or ( 0 ) ;
445+
446+ if auto_clear_days == 0 {
447+ log:: info!( "自动清除缓存已关闭" ) ;
448+ return Ok ( false ) ;
449+ }
450+
451+ let last_clear_date = config. get ( "lastCacheClearDate" )
452+ . and_then ( |v| v. as_str ( ) )
453+ . unwrap_or ( "" ) ;
454+
455+ let today = chrono:: Local :: now ( ) . format ( "%Y-%m-%d" ) . to_string ( ) ;
456+
457+ if last_clear_date == today {
458+ log:: info!( "今日已执行过自动清除缓存" ) ;
459+ return Ok ( false ) ;
460+ }
461+
462+ if last_clear_date. is_empty ( ) {
463+ let mut updated_config = config. clone ( ) ;
464+ updated_config[ "lastCacheClearDate" ] = serde_json:: json!( today) ;
465+ let updated_content = serde_json:: to_string_pretty ( & updated_config)
466+ . map_err ( |e| format ! ( "Failed to serialize config: {}" , e) ) ?;
467+ std:: fs:: write ( & config_file, updated_content)
468+ . map_err ( |e| format ! ( "Failed to write config: {}" , e) ) ?;
469+ log:: info!( "首次设置自动清除缓存日期" ) ;
470+ return Ok ( false ) ;
471+ }
472+
473+ let last_date = chrono:: NaiveDate :: parse_from_str ( last_clear_date, "%Y-%m-%d" )
474+ . map_err ( |e| format ! ( "Failed to parse last clear date: {}" , e) ) ?;
475+ let today_date = chrono:: Local :: now ( ) . date_naive ( ) ;
476+
477+ let days_since_last_clear = ( today_date - last_date) . num_days ( ) ;
478+
479+ if days_since_last_clear >= auto_clear_days as i64 {
480+ log:: info!( "执行自动清除缓存,距上次清除 {} 天" , days_since_last_clear) ;
481+
482+ let cache_dir = app. path ( ) . app_cache_dir ( )
483+ . map_err ( |e| format ! ( "Failed to get cache dir: {}" , e) ) ?;
484+
485+ if cache_dir. exists ( ) {
486+ fn remove_dir_contents ( path : & std:: path:: Path ) {
487+ if let Ok ( entries) = std:: fs:: read_dir ( path) {
488+ for entry in entries. flatten ( ) {
489+ let entry_path = entry. path ( ) ;
490+ if entry_path. is_dir ( ) {
491+ remove_dir_contents ( & entry_path) ;
492+ let _ = std:: fs:: remove_dir ( & entry_path) ;
493+ } else {
494+ let _ = std:: fs:: remove_file ( & entry_path) ;
495+ }
496+ }
497+ }
498+ }
499+ remove_dir_contents ( & cache_dir) ;
500+ }
501+
502+ let mut updated_config = config. clone ( ) ;
503+ updated_config[ "lastCacheClearDate" ] = serde_json:: json!( today) ;
504+ let updated_content = serde_json:: to_string_pretty ( & updated_config)
505+ . map_err ( |e| format ! ( "Failed to serialize config: {}" , e) ) ?;
506+ std:: fs:: write ( & config_file, updated_content)
507+ . map_err ( |e| format ! ( "Failed to write config: {}" , e) ) ?;
508+
509+ log:: info!( "自动清除缓存完成" ) ;
510+ return Ok ( true ) ;
511+ }
512+
513+ Ok ( false )
514+ }
515+
424516/// 获取应用配置目录
425517#[ tauri:: command]
426518fn get_config_dir ( app : tauri:: AppHandle ) -> Result < String , String > {
@@ -1053,7 +1145,9 @@ fn get_default_config() -> serde_json::Value {
10531145 { "r" : 255 , "g" : 255 , "b" : 255 }
10541146 ] ,
10551147 "fileAssociations" : false ,
1056- "wordAssociations" : false
1148+ "wordAssociations" : false ,
1149+ "autoClearCacheDays" : 15 ,
1150+ "lastCacheClearDate" : ""
10571151 } )
10581152}
10591153
@@ -1756,6 +1850,7 @@ pub fn run() {
17561850 . plugin ( tauri_plugin_opener:: init ( ) )
17571851 . plugin ( tauri_plugin_fs:: init ( ) )
17581852 . plugin ( tauri_plugin_dialog:: init ( ) )
1853+ . plugin ( tauri_plugin_shell:: init ( ) )
17591854 . plugin ( tauri_plugin_single_instance:: init ( |app, args, _cwd| {
17601855 println ! ( "单实例回调: args={:?}" , args) ;
17611856 if args. len ( ) > 1 {
@@ -1858,6 +1953,7 @@ pub fn run() {
18581953 get_cache_dir,
18591954 get_cache_size,
18601955 clear_cache,
1956+ check_auto_clear_cache,
18611957 get_config_dir,
18621958 get_cds_dir,
18631959 enhance_image,
0 commit comments