exclude empty directories
Some checks are pending
ci/woodpecker/push/publish Pipeline is pending approval

This commit is contained in:
Mylloon 2024-04-01 17:36:42 +02:00
parent 197a1cbde1
commit 05185f48e9
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -1,6 +1,7 @@
use std::path::Path; use std::path::Path;
use actix_web::{get, web, Responder}; use actix_web::{get, web, Responder};
use cached::proc_macro::cached;
use ramhorns::Content; use ramhorns::Content;
use regex::Regex; use regex::Regex;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -31,21 +32,23 @@ struct CoursTemplate {
content: Option<File>, content: Option<File>,
} }
#[derive(Debug, Serialize)] #[derive(Debug, Serialize, Clone)]
struct FileNode { struct FileNode {
name: String, name: String,
is_dir: bool, is_dir: bool,
children: Vec<FileNode>, children: Vec<FileNode>,
} }
fn compile_patterns(exclusion_list: &[String]) -> Vec<Regex> { #[cached]
fn compile_patterns(exclusion_list: Vec<String>) -> Vec<Regex> {
exclusion_list exclusion_list
.iter() .iter()
.map(|pattern| Regex::new(pattern).unwrap()) .map(|pattern| Regex::new(pattern).unwrap())
.collect() .collect()
} }
fn get_filetree(dir_path: &str, exclusion_patterns: Vec<Regex>) -> FileNode { // #[once(time = 240)]
fn get_filetree(dir_path: &str, exclusion_patterns: &Vec<Regex>) -> FileNode {
let children = std::fs::read_dir(dir_path) let children = std::fs::read_dir(dir_path)
.unwrap() .unwrap()
.filter_map(Result::ok) .filter_map(Result::ok)
@ -65,10 +68,14 @@ fn get_filetree(dir_path: &str, exclusion_patterns: Vec<Regex>) -> FileNode {
children: vec![], children: vec![],
}) })
} else { } else {
Some(get_filetree( // Exclude empty directories
entry_path.to_str().unwrap(), let children_of_children =
exclusion_patterns.to_owned(), get_filetree(entry_path.to_str().unwrap(), exclusion_patterns);
)) if children_of_children.is_dir && children_of_children.children.is_empty() {
None
} else {
Some(children_of_children)
}
} }
}) })
.collect(); .collect();
@ -109,13 +116,13 @@ fn get_content(
) )
} }
// #[once(time = 60)]
// TODO: Uncomment before release // TODO: Uncomment before release
// #[once(time = 60)]
fn build_page(info: web::Query<PathRequest>, config: Config) -> String { fn build_page(info: web::Query<PathRequest>, config: Config) -> String {
let cours_dir = "data/cours"; let cours_dir = "data/cours";
let exclusion_list = config.fc.exclude_courses.unwrap(); let exclusion_list = config.fc.exclude_courses.unwrap();
let exclusion_patterns = compile_patterns(&exclusion_list); let exclusion_patterns = compile_patterns(exclusion_list.to_owned());
let filetree = get_filetree(cours_dir, exclusion_patterns); let filetree = get_filetree(cours_dir, &exclusion_patterns);
config.tmpl.render( config.tmpl.render(
"cours.html", "cours.html",