Newer
Older
use crate::app_state::AppState;
use std::fs::{create_dir, create_dir_all, remove_dir, remove_file, File};
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
pub fn create_static_view(
app_state: &AppState,
category: String,
filename: String,
html: String,
) -> Result<(), String> {
let view_path = app_state.env.public_dir.join(&category).join("view");
if !view_path.exists() {
if let Err(e) = create_dir(&view_path) {
return Err(format!("Couldn't create directory {:?}: {}", view_path, e));
}
}
let d_path = app_state
.env
.public_dir
.join(&category)
.join("view")
.join(&filename);
if let Err(e) = create_dir_all(&d_path) {
return Err(format!("Error creating directory {:?} : {}", d_path, e));
}
let f_path = d_path.join("index.html");
match File::create(&f_path) {
Ok(mut f) => {
if let Err(e) = f.write_all(html.as_bytes()) {
return Err(format!("Error writing to {:?} : {}", f_path, e));
}
Ok(())
}
Err(e) => Err(format!("Error creating {:?} : {}", f_path, e)),
}
}
pub fn delete_static_view(path: &Option<PathBuf>) -> Result<(), String> {
if let Some(path) = path {
if path.exists() {
if let Err(e) = remove_file(path) {
return Err(format!("Error deleting static view at {:?} : {}", path, e));
};
let parent = path.parent().unwrap();
if let Err(e) = remove_dir(parent) {
return Err(format!(
"Error deleting static view directory at {:?} : {}",
parent, e
));
}
}
}
Ok(())
}