86 lines
2.6 KiB
Rust
86 lines
2.6 KiB
Rust
use rocket_dyn_templates::Template;
|
|
use rocket::fs::NamedFile;
|
|
use std::path::{Path, PathBuf};
|
|
use std::collections::HashMap;
|
|
use std::env;
|
|
|
|
// TODO: try... literally try to reduce the number of clones that happen here
|
|
|
|
lazy_static! {
|
|
static ref SITENAME: String = {
|
|
env::var("SITE_NAME").unwrap_or("Clippable".to_string())
|
|
};
|
|
static ref SITEDESC: String = {
|
|
env::var("SITE_DESC").unwrap_or("Short clips".to_string())
|
|
};
|
|
static ref SITEURL: String = {
|
|
env::var("SITE_URL").unwrap_or("#".to_string())
|
|
};
|
|
}
|
|
|
|
fn default_map() -> HashMap<&'static str, String> {
|
|
let mut h: HashMap<&'static str, String> = HashMap::new();
|
|
h.insert("sitetitle", SITENAME.clone());
|
|
h.insert("sitedesc", SITEDESC.clone());
|
|
h.insert("siteurl", SITEURL.clone());
|
|
return h;
|
|
}
|
|
|
|
#[get("/")]
|
|
pub async fn home() -> Template {
|
|
let mut h = default_map();
|
|
h.insert("script_src", String::from("index.js"));
|
|
h.insert("page", String::from("home"));
|
|
h.insert("title", SITENAME.clone());
|
|
|
|
return Template::render("list", &h);
|
|
}
|
|
|
|
#[get("/category/<cat..>")]
|
|
pub async fn category(cat: PathBuf) -> Template {
|
|
let mut h = default_map();
|
|
h.insert("script_src", String::from("category.js"));
|
|
h.insert("page", String::from("category"));
|
|
|
|
// Opengraph meta tags bro
|
|
let cat = cat.file_name().unwrap().to_string_lossy();
|
|
h.insert("title", cat.to_string());
|
|
h.insert("url", format!("/category/{}", cat));
|
|
h.insert("desc", format!("{} clips", cat));
|
|
h.insert("ogimg", format!("/thumbnail/{}/category-thumbnail.jpg", cat));
|
|
|
|
return Template::render("list", &h);
|
|
}
|
|
|
|
#[get("/clip/<cat>/<file_base>")]
|
|
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("/<file..>")]
|
|
pub async fn files(file: PathBuf) -> Option<NamedFile> {
|
|
// Used for things like javascript, css, and favicons
|
|
NamedFile::open(Path::new("static/").join(file)).await.ok()
|
|
}
|