From d6b076970f3c0b1cdbcbad9d08607abedfafe063 Mon Sep 17 00:00:00 2001 From: shockrah Date: Wed, 13 Oct 2021 00:21:27 -0700 Subject: [PATCH] + Video clip endpoint Let's users actually request videos + Meta data handling for video clip pages + Template for video pages --- api/src/main.rs | 10 ++++++---- api/src/page.rs | 29 +++++++++++++++++++++++++---- api/src/video.rs | 11 ++++++++++- api/templates/video.html.tera | 30 ++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 api/templates/video.html.tera diff --git a/api/src/main.rs b/api/src/main.rs index 4229b29..0e9016a 100644 --- a/api/src/main.rs +++ b/api/src/main.rs @@ -17,11 +17,13 @@ async fn main() { * Some of the target vars that we look for * CLIP_DIR */ + // Comments denote what kind of data each sub uri actually returns let _ = rocket::build() - .mount("/", routes![page::home, page::category, page::video]) - .mount("/static", routes![page::files]) - .mount("/api", routes![category::list, video::list]) - .mount("/thumbnail", routes![thumbnail::get]) + .mount("/", routes![page::home, page::category, page::video]) // html + .mount("/static", routes![page::files]) // files + .mount("/api", routes![category::list, video::list]) // json + .mount("/thumbnail", routes![thumbnail::get]) // images + .mount("/video", routes![video::get_video]) // videos .attach(Template::fairing()) .launch().await; } diff --git a/api/src/page.rs b/api/src/page.rs index d1144fc..1fd1047 100644 --- a/api/src/page.rs +++ b/api/src/page.rs @@ -35,10 +35,31 @@ pub async fn category(cat: PathBuf) -> Template { return Template::render("list", &h); } -#[get("/clip/")] -pub async fn video(id: String) -> Template { - println!("id: {}", id); - return Template::render("video", &{}) +#[get("/clip//")] +pub async fn video(cat: PathBuf, file_base: PathBuf) -> Template { + // NOTE: the "file name" does not include the + println!("{:?}, {:?}", cat, file_base); + + 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); + + return Template::render("video", &h); } #[get("/")] diff --git a/api/src/video.rs b/api/src/video.rs index 654a00b..d0a982a 100644 --- a/api/src/video.rs +++ b/api/src/video.rs @@ -1,8 +1,9 @@ -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::fs::DirEntry; use std::ffi::OsStr; use serde::Serialize; use rocket::serde::json::Json; +use rocket::fs::NamedFile; use crate::common::get_clips_dir; #[derive(Serialize)] @@ -60,3 +61,11 @@ pub fn list(cat: PathBuf) -> Option>> { return None; } + +#[get("/clip//")] +pub async fn get_video(cat: PathBuf, file: PathBuf) -> Option { + let clips_dir = get_clips_dir(); + let path = Path::new(&clips_dir).join(cat).join(file); + println!("path to grab from : {:?}", path); + NamedFile::open(path).await.ok() +} diff --git a/api/templates/video.html.tera b/api/templates/video.html.tera new file mode 100644 index 0000000..2808215 --- /dev/null +++ b/api/templates/video.html.tera @@ -0,0 +1,30 @@ + + + + + + + + Shockrah's Clips + + + + + + + {# Image tag requires a default in case of disk memery #} + + {% if script %} + + {% endif %} + + +
+
+

+ +
+
+ +