clippable/api/src/page.rs
shockrah c53d9a293e ! Finalizing video fetch endpoint
With this the backend is officially at an MVP stage and now requires polishing
Proper data streaming for larger clips is basically required
for a really good backend(especially async streaming)
and some better css for the video player
2021-10-16 18:14:04 -07:00

69 lines
1.9 KiB
Rust

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/<cat..>")]
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/<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()
}