enhance(fs):删除Walkdir依赖,改用标准库实现。

This commit is contained in:
徐涛 2023-03-21 08:35:43 +08:00
parent 6609d64752
commit 39accb3cb7
3 changed files with 73 additions and 82 deletions

1
src-tauri/Cargo.lock generated
View File

@ -344,7 +344,6 @@ dependencies = [
"tokio", "tokio",
"urlencoding", "urlencoding",
"uuid 1.3.0", "uuid 1.3.0",
"walkdir",
] ]
[[package]] [[package]]

View File

@ -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"

View File

@ -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()))?;
.path() let file_hash_id = entry
.to_str() .path()
.map(crate::utils::md5_hash) .to_str()
.map(|hash| utils::uuid_from(hash.as_slice())) .map(crate::utils::md5_hash)
.transpose() .map(|hash| utils::uuid_from(hash.as_slice()))
.map_err(|e| anyhow!(e))? .transpose()?
.unwrap_or_else(|| uuid::Uuid::new_v4().to_string()); .unwrap_or(uuid::Uuid::new_v4().to_string());
Ok(FileItem { let filename = entry
id: file_hash_id, .path()
filename: f .file_name()
.path() .ok_or(String::from("不能获取到文件名。"))?
.file_name() .to_owned()
.ok_or(anyhow!("不能获取到文件名。"))? .into_string()
.to_owned() .unwrap();
.into_string() let path = entry
.unwrap(), .path()
path: f .clone()
.path() .to_str()
.clone() .ok_or(String::from("不能获取到文件路径。"))?
.to_str() .to_string();
.ok_or(anyhow!("不能获取到文件路径。"))? file_items.push(FileItem {
.to_string(), id: file_hash_id,
width, filename,
height, path,
}) 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 { .path()
id: dir_hash_id, .file_name()
dirname: d .ok_or(String::from("不能获取到文件夹的名称。"))?
.path() .to_owned()
.file_name() .into_string()
.ok_or(anyhow!("不能获取到文件夹的名称。"))? .unwrap();
.to_owned() let path = entry
.into_string() .path()
.unwrap(), .clone()
path: d .to_str()
.path() .ok_or(String::from("不能获取到文件夹路径。"))?
.clone() .to_string();
.to_str() child_dirs.push(DirItem {
.ok_or(anyhow!("不能获取到文件夹路径。"))? id: dir_hash_id,
.to_string(), dirname,
root: false, path,
}) 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)
} }