-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlib.rs
More file actions
174 lines (151 loc) · 5.41 KB
/
lib.rs
File metadata and controls
174 lines (151 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone)]
pub struct FileNode {
pub name: String,
pub path: String,
pub is_directory: bool,
pub children: Vec<FileNode>,
}
fn sort_nodes(nodes: &mut Vec<FileNode>) {
nodes.sort_by(|a, b| {
if a.is_directory != b.is_directory {
return if a.is_directory {
std::cmp::Ordering::Less
} else {
std::cmp::Ordering::Greater
};
}
a.name.to_lowercase().cmp(&b.name.to_lowercase())
});
for node in nodes {
if !node.children.is_empty() {
sort_nodes(&mut node.children);
}
}
}
#[cfg(not(target_os = "android"))]
fn build_tree_recursive_desktop(path_str: &str) -> std::io::Result<Vec<FileNode>> {
use std::fs;
let mut nodes = Vec::new();
let entries = fs::read_dir(path_str)?;
for entry in entries {
if let Ok(entry) = entry {
if let Ok(metadata) = entry.metadata() {
let file_name = entry.file_name().to_string_lossy().to_string();
let is_directory = metadata.is_dir();
let starts_with_dot = file_name.starts_with('.');
let ends_with_md = file_name.ends_with(".md");
if (is_directory && !starts_with_dot) || ends_with_md {
let path = entry.path().to_string_lossy().to_string();
let mut children = Vec::new();
if is_directory {
if let Ok(sub_children) = build_tree_recursive_desktop(&path) {
children = sub_children;
}
}
nodes.push(FileNode {
name: file_name.trim_end_matches(".md").to_string(),
path,
is_directory,
children,
});
}
}
}
}
Ok(nodes)
}
#[cfg(target_os = "android")]
fn build_tree_recursive_android(
app: tauri::AppHandle,
path: String,
document_top_tree_uri: Option<String>,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Vec<FileNode>, String>> + Send>> {
Box::pin(async move {
use tauri_plugin_android_fs::AndroidFsExt;
use tauri_plugin_android_fs::FileUri;
let api = app.android_fs();
let json_obj = serde_json::json!({
"uri": path,
"documentTopTreeUri": document_top_tree_uri
});
let file_uri = FileUri::from_json_str(&json_obj.to_string())
.map_err(|e| format!("Failed to create FileUri: {}", e))?;
let entries = api.read_dir(&file_uri)
.map_err(|e| e.to_string())?;
let mut nodes = Vec::new();
for entry in entries {
let name = entry.name().to_string();
let is_directory = entry.is_dir();
let starts_with_dot = name.starts_with('.');
let ends_with_md = name.ends_with(".md");
if (is_directory && !starts_with_dot) || ends_with_md {
let path_uri = format!("{}%2F{}", path, urlencoding::encode(&name));
let mut children = Vec::new();
if is_directory {
children = build_tree_recursive_android(
app.clone(),
path_uri.clone(),
document_top_tree_uri.clone()
).await?;
}
nodes.push(FileNode {
name: name.trim_end_matches(".md").to_string(),
path: path_uri,
is_directory,
children,
});
}
}
Ok(nodes)
})
}
#[tauri::command]
async fn build_file_tree(
_app: tauri::AppHandle,
path: String,
_document_top_tree_uri: Option<String>,
) -> Result<Vec<FileNode>, String> {
let nodes;
#[cfg(target_os = "android")]
{
// On Android, we need the app handle and args.
// But we renamed arguments to start with _.
// We can use them directly.
let mut unsorted_nodes = build_tree_recursive_android(_app, path, _document_top_tree_uri).await?;
sort_nodes(&mut unsorted_nodes);
nodes = unsorted_nodes;
}
#[cfg(not(target_os = "android"))]
{
nodes = tauri::async_runtime::spawn_blocking(move || {
let mut n = build_tree_recursive_desktop(&path)?;
sort_nodes(&mut n);
Ok(n)
}).await.map_err(|e| e.to_string())?
.map_err(|e: std::io::Error| e.to_string())?;
}
Ok(nodes)
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_os::init())
.plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_store::Builder::new().build())
.plugin(tauri_plugin_fs::init())
.plugin(tauri_plugin_android_fs::init())
.invoke_handler(tauri::generate_handler![build_file_tree])
.setup(|app| {
if cfg!(debug_assertions) {
app.handle().plugin(
tauri_plugin_log::Builder::default()
.level(log::LevelFilter::Info)
.build(),
)?;
}
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}