add exclusions

This commit is contained in:
Mylloon 2024-04-01 16:35:53 +02:00
parent faab49a385
commit 3ccadd2e0a
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
5 changed files with 28 additions and 11 deletions

1
Cargo.lock generated
View file

@ -1163,6 +1163,7 @@ dependencies = [
"minify-html", "minify-html",
"minify-js 0.6.0", "minify-js 0.6.0",
"ramhorns", "ramhorns",
"regex",
"reqwest", "reqwest",
"rss", "rss",
"serde", "serde",

View file

@ -27,6 +27,4 @@ chrono = { version = "0.4", default-features = false, features = ["clock"]}
chrono-tz = "0.8" chrono-tz = "0.8"
rss = { version = "2.0", features = ["atom"] } rss = { version = "2.0", features = ["atom"] }
lol_html = "1.2" lol_html = "1.2"
base64 = "0.22.0" regex = "1.10"
mime_guess = "2.0.4"
urlencoding = "2.1.3"

View file

@ -106,6 +106,7 @@ onion = "http://youraddress.onion/"
app_name = "Nickname" # fallback to 'EWP' if none app_name = "Nickname" # fallback to 'EWP' if none
name = "Firstname" name = "Firstname"
fullname = "Fullname" fullname = "Fullname"
exclude_courses = []
``` ```
## Link shortener for contacts ## Link shortener for contacts

View file

@ -27,6 +27,8 @@ pub struct FileConfig {
pub name: Option<String>, pub name: Option<String>,
/// Fullname of website owner /// Fullname of website owner
pub fullname: Option<String>, pub fullname: Option<String>,
/// List exclusion for courses
pub exclude_courses: Option<Vec<String>>,
} }
impl FileConfig { impl FileConfig {
@ -37,6 +39,7 @@ impl FileConfig {
domain: Some("localhost".into()), domain: Some("localhost".into()),
port: Some(8080), port: Some(8080),
app_name: Some("EWP".into()), app_name: Some("EWP".into()),
exclude_courses: Some([].into()),
..FileConfig::default() ..FileConfig::default()
} }
} }
@ -65,6 +68,7 @@ impl FileConfig {
app_name: test(a.app_name, d.app_name), app_name: test(a.app_name, d.app_name),
name: test(a.name, d.name), name: test(a.name, d.name),
fullname: test(a.fullname, d.fullname), fullname: test(a.fullname, d.fullname),
exclude_courses: test(a.exclude_courses, d.exclude_courses),
} }
} }
} }

View file

@ -2,6 +2,7 @@ use std::path::Path;
use actix_web::{get, web, Responder}; use actix_web::{get, web, Responder};
use ramhorns::Content; use ramhorns::Content;
use regex::Regex;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{ use crate::{
@ -38,7 +39,7 @@ struct FileNode {
} }
/// Build the filetree /// Build the filetree
fn get_filetree(dir_path: &str, exclusion_list: &[&str]) -> FileNode { fn get_filetree(dir_path: &str, exclusion_list: Vec<String>) -> FileNode {
let entries = std::fs::read_dir(dir_path).unwrap(); let entries = std::fs::read_dir(dir_path).unwrap();
let mut children = Vec::new(); let mut children = Vec::new();
@ -47,7 +48,16 @@ fn get_filetree(dir_path: &str, exclusion_list: &[&str]) -> FileNode {
let entry_name = entry_path.file_name().and_then(|n| n.to_str()).unwrap(); let entry_name = entry_path.file_name().and_then(|n| n.to_str()).unwrap();
// We should support regex? // We should support regex?
if !exclusion_list.contains(&entry_name) { if !exclusion_list
.iter()
.any(|pattern| match Regex::new(pattern) {
Ok(re) => re.is_match(entry_name),
Err(e) => {
eprintln!("{e}");
false
}
})
{
let filename = entry_name.to_string(); let filename = entry_name.to_string();
if entry_path.is_file() { if entry_path.is_file() {
children.push(FileNode { children.push(FileNode {
@ -56,7 +66,10 @@ fn get_filetree(dir_path: &str, exclusion_list: &[&str]) -> FileNode {
children: vec![], children: vec![],
}); });
} else { } else {
children.push(get_filetree(entry_path.to_str().unwrap(), exclusion_list)); children.push(get_filetree(
entry_path.to_str().unwrap(),
exclusion_list.to_owned(),
));
} }
} }
} }
@ -76,7 +89,7 @@ fn get_filetree(dir_path: &str, exclusion_list: &[&str]) -> FileNode {
fn get_content( fn get_content(
cours_dir: &str, cours_dir: &str,
path: &web::Query<PathRequest>, path: &web::Query<PathRequest>,
exclusion_list: &[&str], exclusion_list: Vec<String>,
) -> Option<File> { ) -> Option<File> {
let filename = match &path.q { let filename = match &path.q {
Some(q) => q, Some(q) => q,
@ -86,7 +99,7 @@ fn get_content(
// We should support regex? // We should support regex?
if exclusion_list if exclusion_list
.iter() .iter()
.any(|&excluded_term| filename.contains(excluded_term)) .any(|excluded_term| filename.contains(excluded_term.as_str()))
{ {
return None; return None;
} }
@ -101,8 +114,8 @@ fn get_content(
// TODO: Uncomment before release // TODO: Uncomment before release
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 = []; let exclusion_list = config.fc.exclude_courses.unwrap();
let filetree = get_filetree(cours_dir, &exclusion_list); let filetree = get_filetree(cours_dir, exclusion_list.to_owned());
config.tmpl.render( config.tmpl.render(
"cours.html", "cours.html",
@ -112,7 +125,7 @@ fn build_page(info: web::Query<PathRequest>, config: Config) -> String {
..NavBar::default() ..NavBar::default()
}, },
filetree: serde_json::to_string(&filetree).unwrap(), filetree: serde_json::to_string(&filetree).unwrap(),
content: get_content(cours_dir, &info, &exclusion_list), content: get_content(cours_dir, &info, exclusion_list),
}, },
Infos { Infos {
page_title: Some("Cours".into()), page_title: Some("Cours".into()),