Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
static_view.rs 1.72 KiB
Newer Older
  • Learn to ignore specific revisions
  • use crate::app_state::AppState;
    
    use std::fs::{create_dir, create_dir_all, remove_dir, remove_file, File};
    
    use std::io::Write;
    
    use std::path::PathBuf;
    
    
    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(())
    }