use rocket_dyn_templates::Template; use rocket::fs::NamedFile; use std::path::{Path, PathBuf}; use std::collections::HashMap; #[get("/")] pub async fn home() -> Template { let mut h: HashMap<&'static str, &'static str> = HashMap::new(); h.insert("script_src", "index.js"); h.insert("page", "home"); return Template::render("list", &h); } #[get("/category/")] pub async fn category(cat: PathBuf) -> Template { let mut h: HashMap<&'static str, &str> = HashMap::new(); h.insert("script_src", "category.js"); h.insert("page", "category"); // Opengraph meta tags bro let cat = cat.file_name().unwrap().to_string_lossy(); h.insert("title", &cat); let og_cat = format!("/category/{}", &cat); h.insert("url", &og_cat); let og_desc = format!("{} clips", &cat); h.insert("desc", &og_desc); let og_img = format!("/thumbnail/{}/category-thumbnail.jpg", cat); h.insert("ogimg", &og_img); return Template::render("list", &h); } #[get("/clip//")] pub async fn video(cat: PathBuf, file_base: PathBuf) -> Template { let mut h: HashMap<&'static str, &str> = HashMap::new(); let cat = cat.to_string_lossy(); let file = file_base.to_string_lossy(); let mut file_pretty = file.to_string(); for c in ["-", "_", ".mp4", ".mkv", ".webm"] { file_pretty = file_pretty.replace(c, " "); } h.insert("title", &file_pretty); let url = format!("/clip/{}/{}", &cat, &file); h.insert("url", &url); h.insert("desc", &file_pretty); let thumbnail = format!("/thumbnail/{}/{}", cat, file); h.insert("ogimg", &thumbnail); let clip_url = format!("/video/{}/{}", &cat, &file); h.insert("clip_url", &clip_url); return Template::render("video", &h); } #[get("/")] pub async fn files(file: PathBuf) -> Option { // Used for things like javascript, css, and favicons NamedFile::open(Path::new("static/").join(file)).await.ok() }