diff --git a/api/src/page.rs b/api/src/page.rs index a99379d..f0fa79e 100644 --- a/api/src/page.rs +++ b/api/src/page.rs @@ -2,6 +2,7 @@ use rocket_dyn_templates::Template; use rocket::fs::NamedFile; use std::path::{Path, PathBuf}; use std::collections::HashMap; +use crate::common::get_clips_dir; use std::env; // TODO: try... literally try to reduce the number of clones that happen here @@ -52,12 +53,38 @@ pub async fn category(cat: PathBuf) -> Template { return Template::render("list", &h); } +fn map_base_vfile(category: &PathBuf, base: &PathBuf) -> String { + // This takes a category and base filename and basically looks for the file + // that maps to those parameters + // This basically aims to get rid of the extension in the URL so that social + // media sites will bother to fetch open graph tags + let basename = base.to_string_lossy(); + let dirname = format!("{}/{}/", get_clips_dir(), category.to_string_lossy()); + let dir = Path::new(&dirname); + let file = dir.read_dir().ok().unwrap().find(|item| { + if let Ok(item) = item { + let name = item.file_name().into_string().unwrap(); + println!("Checking name {:?}", name); + name.starts_with(basename.as_ref()) + } else { + false + } + }); + match file { + Some(ent) => ent.unwrap().file_name().into_string().unwrap(), + None => "/static/cantfindshit.jpg".to_string() + } +} + #[get("/clip//")] -pub async fn video(cat: PathBuf, file_base: PathBuf) -> Template { +pub fn video(cat: PathBuf, file_base: PathBuf) -> Template { let mut h: HashMap<&'static str, &str> = HashMap::new(); + // Look for the file_base + [extension] + let file = map_base_vfile(&cat, &file_base); + let cat = cat.to_string_lossy(); - let file = file_base.to_string_lossy(); + // First we have to try and find the file let mut file_pretty = file.to_string(); for c in ["-", "_", ".mp4", ".mkv", ".webm"] { diff --git a/api/src/video.rs b/api/src/video.rs index 1c7d11f..b21ef4b 100644 --- a/api/src/video.rs +++ b/api/src/video.rs @@ -66,6 +66,5 @@ pub fn list(cat: PathBuf) -> Option>> { 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() }