Add directory dialog
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
fs::{self, OpenOptions},
|
||||
path::Path,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use serde::Serialize;
|
||||
@@ -13,6 +14,7 @@ pub struct Directory {
|
||||
name: String,
|
||||
path: String,
|
||||
parent: Option<String>,
|
||||
is_root: bool,
|
||||
}
|
||||
|
||||
fn is_readable_and_writable(path: &Path) -> bool {
|
||||
@@ -57,6 +59,7 @@ fn is_hidden(entry: &DirEntry) -> bool {
|
||||
|
||||
pub fn get_readable_writable_dirs(root: &Path) -> Vec<Directory> {
|
||||
let mut dirs = Vec::new();
|
||||
let mut all_paths = HashSet::new();
|
||||
|
||||
for entry in WalkDir::new(root)
|
||||
.follow_links(false)
|
||||
@@ -69,13 +72,27 @@ pub fn get_readable_writable_dirs(root: &Path) -> Vec<Directory> {
|
||||
let path = entry.path();
|
||||
|
||||
if is_readable_and_writable(path) {
|
||||
dirs.push(Directory {
|
||||
name: entry.file_name().to_string_lossy().to_string(),
|
||||
path: path.to_string_lossy().to_string(),
|
||||
parent: path.parent().map(|p| p.to_string_lossy().to_string()),
|
||||
});
|
||||
all_paths.insert(path.to_path_buf());
|
||||
}
|
||||
}
|
||||
|
||||
for path in &all_paths {
|
||||
let parent = path.parent().map(|p| p.to_string_lossy().to_string());
|
||||
let is_root = parent
|
||||
.as_ref()
|
||||
.map(|p| !all_paths.contains(&PathBuf::from(p)))
|
||||
.unwrap_or(true);
|
||||
|
||||
dirs.push(Directory {
|
||||
name: path
|
||||
.file_name()
|
||||
.map(|f| f.to_string_lossy().to_string())
|
||||
.unwrap_or_else(|| "/".to_string()),
|
||||
path: path.to_string_lossy().to_string(),
|
||||
parent,
|
||||
is_root,
|
||||
});
|
||||
}
|
||||
|
||||
dirs
|
||||
}
|
||||
|
Reference in New Issue
Block a user