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
use walkdir::WalkDir;
pub fn add_time_id() -> String {
let now = chrono::Utc::now();
let timestamp = format!("{}", now.format("%Y-%m-%d-%H-%M-%S"));
timestamp
}
pub async fn files_in_folder(path: &str) -> Vec<String> {
let mut files = Vec::new();
for entry in std::fs::read_dir(&path).expect(
format!(
"The data has not been gathered to and cannot be read from {}",
&path
)
.as_str(),
) {
let entry = entry.expect("Failed to get entry");
let path = entry.path();
if path.is_file() {
files.push(path.to_str().unwrap().to_string());
}
}
files
}
pub async fn create_file_index(root_path: String) -> Vec<String> {
let file_list = WalkDir::new(root_path)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.file_type().is_file())
.map(|e| e.path().to_str().unwrap().to_string())
.collect::<Vec<String>>();
return file_list;
}
pub fn check_slash(path: &str) -> String {
if path.ends_with("/") {
return path.to_string();
} else {
return format!("{}/", path);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_add_time_id() {
let result = add_time_id();
assert!(!result.contains(" "));
}
#[test]
fn test_check_slash() {
let result = check_slash("path");
assert!(result == "path/");
}
}