enhance(fs):删除Walkdir依赖,改用标准库实现。
This commit is contained in:
		
							
								
								
									
										1
									
								
								src-tauri/Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										1
									
								
								src-tauri/Cargo.lock
									
									
									
										generated
									
									
									
								
							| @@ -344,7 +344,6 @@ dependencies = [ | |||||||
|  "tokio", |  "tokio", | ||||||
|  "urlencoding", |  "urlencoding", | ||||||
|  "uuid 1.3.0", |  "uuid 1.3.0", | ||||||
|  "walkdir", |  | ||||||
| ] | ] | ||||||
|  |  | ||||||
| [[package]] | [[package]] | ||||||
|   | |||||||
| @@ -20,7 +20,6 @@ serde_json = "1.0" | |||||||
| chrono = { version = "0.4.23", features = ["serde"] } | chrono = { version = "0.4.23", features = ["serde"] } | ||||||
| anyhow = "1.0.68" | anyhow = "1.0.68" | ||||||
| thiserror = "1.0.38" | thiserror = "1.0.38" | ||||||
| walkdir = "2.3.2" |  | ||||||
| serde_repr = "0.1.10" | serde_repr = "0.1.10" | ||||||
| tokio = { version = "1.23.1", features = ["full"] } | tokio = { version = "1.23.1", features = ["full"] } | ||||||
| image = "0.24.5" | image = "0.24.5" | ||||||
|   | |||||||
| @@ -1,10 +1,8 @@ | |||||||
| use std::path::Path; | use std::{fs::DirEntry, path::Path}; | ||||||
|  |  | ||||||
| use anyhow::anyhow; |  | ||||||
| use mountpoints::mountinfos; | use mountpoints::mountinfos; | ||||||
| use serde::Serialize; | use serde::Serialize; | ||||||
| use tauri::Runtime; | use tauri::Runtime; | ||||||
| use walkdir::{DirEntry, WalkDir}; |  | ||||||
|  |  | ||||||
| use crate::utils; | use crate::utils; | ||||||
|  |  | ||||||
| @@ -33,10 +31,6 @@ fn is_hidden(entry: &DirEntry) -> bool { | |||||||
|         .unwrap_or(false) |         .unwrap_or(false) | ||||||
| } | } | ||||||
|  |  | ||||||
| fn is_self<P: AsRef<Path>>(entry: &DirEntry, target: P) -> bool { |  | ||||||
|     entry.path().eq(target.as_ref()) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| fn is_root(entry: &DirEntry) -> bool { | fn is_root(entry: &DirEntry) -> bool { | ||||||
|     entry |     entry | ||||||
|         .path() |         .path() | ||||||
| @@ -47,43 +41,43 @@ fn is_root(entry: &DirEntry) -> bool { | |||||||
|  |  | ||||||
| #[tauri::command] | #[tauri::command] | ||||||
| pub async fn scan_directory(target: String) -> Result<Vec<FileItem>, String> { | pub async fn scan_directory(target: String) -> Result<Vec<FileItem>, String> { | ||||||
|     let mut file_items = WalkDir::new(target) |     let mut file_items: Vec<FileItem> = Vec::new(); | ||||||
|         .max_depth(1) |     for entry in std::fs::read_dir(target).map_err(|e| format!("无法读取指定文件夹,{}", e))? | ||||||
|         .into_iter() |     { | ||||||
|         .filter_entry(|entry| !is_hidden(entry)) |         let entry = entry.map_err(|e| format!("无法获取指定文件夹信息,{}", e))?; | ||||||
|         .filter_map(|f| f.ok()) |         if !entry.path().is_file() { | ||||||
|         .filter(|f| f.path().is_file()) |             continue; | ||||||
|         .map(|f| { |         } | ||||||
|             let (width, height) = image::image_dimensions(f.path())?; |         let (width, height) = image::image_dimensions(entry.path()) | ||||||
|             let file_hash_id = f |             .map_err(|_e| format!("读取图片文件 {} 元信息失败。", entry.path().display()))?; | ||||||
|  |         let file_hash_id = entry | ||||||
|             .path() |             .path() | ||||||
|             .to_str() |             .to_str() | ||||||
|             .map(crate::utils::md5_hash) |             .map(crate::utils::md5_hash) | ||||||
|             .map(|hash| utils::uuid_from(hash.as_slice())) |             .map(|hash| utils::uuid_from(hash.as_slice())) | ||||||
|                 .transpose() |             .transpose()? | ||||||
|                 .map_err(|e| anyhow!(e))? |             .unwrap_or(uuid::Uuid::new_v4().to_string()); | ||||||
|                 .unwrap_or_else(|| uuid::Uuid::new_v4().to_string()); |         let filename = entry | ||||||
|             Ok(FileItem { |  | ||||||
|                 id: file_hash_id, |  | ||||||
|                 filename: f |  | ||||||
|             .path() |             .path() | ||||||
|             .file_name() |             .file_name() | ||||||
|                     .ok_or(anyhow!("不能获取到文件名。"))? |             .ok_or(String::from("不能获取到文件名。"))? | ||||||
|             .to_owned() |             .to_owned() | ||||||
|             .into_string() |             .into_string() | ||||||
|                     .unwrap(), |             .unwrap(); | ||||||
|                 path: f |         let path = entry | ||||||
|             .path() |             .path() | ||||||
|             .clone() |             .clone() | ||||||
|             .to_str() |             .to_str() | ||||||
|                     .ok_or(anyhow!("不能获取到文件路径。"))? |             .ok_or(String::from("不能获取到文件路径。"))? | ||||||
|                     .to_string(), |             .to_string(); | ||||||
|                 width, |         file_items.push(FileItem { | ||||||
|  |             id: file_hash_id, | ||||||
|  |             filename, | ||||||
|  |             path, | ||||||
|             height, |             height, | ||||||
|             }) |             width, | ||||||
|         }) |         }); | ||||||
|         .collect::<Result<Vec<FileItem>, anyhow::Error>>() |     } | ||||||
|         .map_err(|e| e.to_string())?; |  | ||||||
|     file_items.sort_by(|a, b| a.filename.partial_cmp(&b.filename).unwrap()); |     file_items.sort_by(|a, b| a.filename.partial_cmp(&b.filename).unwrap()); | ||||||
|  |  | ||||||
|     Ok(file_items) |     Ok(file_items) | ||||||
| @@ -147,42 +141,41 @@ pub async fn scan_for_child_dirs<R: Runtime>( | |||||||
|     } else { |     } else { | ||||||
|         Path::new(&target) |         Path::new(&target) | ||||||
|     }; |     }; | ||||||
|     let mut child_dirs = WalkDir::new(target) |  | ||||||
|         .max_depth(1) |     let mut child_dirs: Vec<DirItem> = Vec::new(); | ||||||
|         .into_iter() |     for entry in std::fs::read_dir(target).map_err(|e| format!("无法读取指定文件夹,{}", e))? | ||||||
|         .filter_entry(|entry| !is_hidden(entry) && !is_root(entry)) |     { | ||||||
|         .filter_map(|d| d.ok()) |         let entry = entry.map_err(|e| format!("无法获取指定文件夹信息,{}", e))?; | ||||||
|         .filter(|d| d.path().is_dir() && !is_self(d, target)) |         if is_hidden(&entry) || is_root(&entry) { | ||||||
|         .map(|d| { |             continue; | ||||||
|             println!("扫描到的文件夹:{}", d.path().display()); |         } | ||||||
|             let dir_hash_id = d |         let dir_hash_id = entry | ||||||
|             .path() |             .path() | ||||||
|             .to_str() |             .to_str() | ||||||
|             .map(crate::utils::md5_hash) |             .map(crate::utils::md5_hash) | ||||||
|             .map(|hash| utils::uuid_from(hash.as_slice())) |             .map(|hash| utils::uuid_from(hash.as_slice())) | ||||||
|                 .transpose() |             .transpose()? | ||||||
|                 .map_err(|e| anyhow!(e))? |             .unwrap_or(uuid::Uuid::new_v4().to_string()); | ||||||
|                 .unwrap_or_else(|| uuid::Uuid::new_v4().to_string()); |         let dirname = entry | ||||||
|             Ok(DirItem { |  | ||||||
|                 id: dir_hash_id, |  | ||||||
|                 dirname: d |  | ||||||
|             .path() |             .path() | ||||||
|             .file_name() |             .file_name() | ||||||
|                     .ok_or(anyhow!("不能获取到文件夹的名称。"))? |             .ok_or(String::from("不能获取到文件夹的名称。"))? | ||||||
|             .to_owned() |             .to_owned() | ||||||
|             .into_string() |             .into_string() | ||||||
|                     .unwrap(), |             .unwrap(); | ||||||
|                 path: d |         let path = entry | ||||||
|             .path() |             .path() | ||||||
|             .clone() |             .clone() | ||||||
|             .to_str() |             .to_str() | ||||||
|                     .ok_or(anyhow!("不能获取到文件夹路径。"))? |             .ok_or(String::from("不能获取到文件夹路径。"))? | ||||||
|                     .to_string(), |             .to_string(); | ||||||
|  |         child_dirs.push(DirItem { | ||||||
|  |             id: dir_hash_id, | ||||||
|  |             dirname, | ||||||
|  |             path, | ||||||
|             root: false, |             root: false, | ||||||
|             }) |         }); | ||||||
|         }) |     } | ||||||
|         .collect::<Result<Vec<DirItem>, anyhow::Error>>() |  | ||||||
|         .map_err(|e| e.to_string())?; |  | ||||||
|     child_dirs.sort_by(|a, b| a.dirname.partial_cmp(&b.dirname).unwrap()); |     child_dirs.sort_by(|a, b| a.dirname.partial_cmp(&b.dirname).unwrap()); | ||||||
|     Ok(child_dirs) |     Ok(child_dirs) | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user