@@ -34,18 +34,54 @@ pub struct RenameBatchResult {
3434 pub failed : usize ,
3535}
3636
37+ /// 文件元数据,供前端按大小/时间排序。时间戳为 Unix 毫秒,取不到(平台不支持等)为 null。
38+ #[ derive( Serialize ) ]
39+ #[ serde( rename_all = "camelCase" ) ]
40+ pub struct FileMeta {
41+ pub size : u64 ,
42+ pub created_ms : Option < i64 > ,
43+ pub modified_ms : Option < i64 > ,
44+ }
45+
46+ /// 批量获取文件元数据,与入参顺序一一对应,单个失败对应项为 null。
47+ #[ tauri:: command( rename_all = "camelCase" ) ]
48+ pub async fn batch_rename_stat ( paths : Vec < String > ) -> Result < Vec < Option < FileMeta > > , String > {
49+ tauri:: async_runtime:: spawn_blocking ( move || {
50+ Ok ( paths. iter ( ) . map ( |path| stat_one ( path) ) . collect ( ) )
51+ } )
52+ . await
53+ . map_err ( |error| format ! ( "读取文件信息任务失败: {error}" ) ) ?
54+ }
55+
56+ fn stat_one ( path : & str ) -> Option < FileMeta > {
57+ let meta = std:: fs:: metadata ( path) . ok ( ) ?;
58+ let to_ms = |time : std:: io:: Result < std:: time:: SystemTime > | {
59+ time. ok ( ) ?
60+ . duration_since ( std:: time:: UNIX_EPOCH )
61+ . ok ( )
62+ . map ( |d| d. as_millis ( ) as i64 )
63+ } ;
64+ Some ( FileMeta {
65+ size : meta. len ( ) ,
66+ created_ms : to_ms ( meta. created ( ) ) ,
67+ modified_ms : to_ms ( meta. modified ( ) ) ,
68+ } )
69+ }
70+
3771/// 判断路径是否为文件夹(拖拽进来的路径需要过滤掉目录)。
3872#[ tauri:: command( rename_all = "camelCase" ) ]
3973pub async fn batch_rename_is_dir ( path : String ) -> Result < bool , String > {
4074 Ok ( std:: path:: Path :: new ( & path) . is_dir ( ) )
4175}
4276
43- /// 列出文件夹的直接子项(含子目录,不递归),按文件名排序 。
77+ /// 列出文件夹的直接子项(recursive=true 时递归收集所有文件,不含子目录),按路径排序 。
4478#[ tauri:: command( rename_all = "camelCase" ) ]
45- pub async fn batch_rename_list_dir ( path : String ) -> Result < Vec < DirEntry > , String > {
46- tauri:: async_runtime:: spawn_blocking ( move || list_dir ( path) )
47- . await
48- . map_err ( |error| format ! ( "读取文件夹任务失败: {error}" ) ) ?
79+ pub async fn batch_rename_list_dir ( path : String , recursive : bool ) -> Result < Vec < DirEntry > , String > {
80+ tauri:: async_runtime:: spawn_blocking ( move || {
81+ if recursive { walk_dir ( path) } else { list_dir ( path) }
82+ } )
83+ . await
84+ . map_err ( |error| format ! ( "读取文件夹任务失败: {error}" ) ) ?
4985}
5086
5187fn list_dir ( path : String ) -> Result < Vec < DirEntry > , String > {
@@ -67,6 +103,37 @@ fn list_dir(path: String) -> Result<Vec<DirEntry>, String> {
67103 Ok ( items)
68104}
69105
106+ /// 递归收集文件夹下所有文件(不含子目录条目)。DirEntry::file_type 不跟随符号链接,
107+ /// 因此软链目录不会被展开,天然避免循环。
108+ fn walk_dir ( path : String ) -> Result < Vec < DirEntry > , String > {
109+ let mut items: Vec < DirEntry > = Vec :: new ( ) ;
110+ let mut stack = vec ! [ std:: path:: PathBuf :: from( & path) ] ;
111+ while let Some ( dir) = stack. pop ( ) {
112+ let entries = match std:: fs:: read_dir ( & dir) {
113+ Ok ( entries) => entries,
114+ // 单个子目录读取失败(权限等)不中断整体,跳过继续
115+ Err ( _) => continue ,
116+ } ;
117+ for entry in entries. flatten ( ) {
118+ let is_dir = entry. file_type ( ) . map ( |t| t. is_dir ( ) ) . unwrap_or ( false ) ;
119+ if is_dir {
120+ stack. push ( entry. path ( ) ) ;
121+ } else {
122+ items. push ( DirEntry {
123+ path : entry. path ( ) . to_string_lossy ( ) . to_string ( ) ,
124+ name : entry. file_name ( ) . to_string_lossy ( ) . to_string ( ) ,
125+ is_dir,
126+ } ) ;
127+ }
128+ }
129+ }
130+ if items. is_empty ( ) {
131+ return Err ( format ! ( "文件夹为空或无法读取内容: {path}" ) ) ;
132+ }
133+ items. sort_by ( |a, b| a. path . to_lowercase ( ) . cmp ( & b. path . to_lowercase ( ) ) ) ;
134+ Ok ( items)
135+ }
136+
70137/// 批量执行重命名。逐项独立处理,单项失败不中断批次;整体只在参数为空时报错。
71138#[ tauri:: command( rename_all = "camelCase" ) ]
72139pub async fn batch_rename_execute ( items : Vec < RenameRequestItem > ) -> Result < RenameBatchResult , String > {
0 commit comments