+ Added support on /clips/<cat>/<file> to ignore the file extension

With this the frontend can produce opengraph fetch-friendly url's
This commit is contained in:
shockrah 2021-10-25 23:31:18 -07:00
parent 54b2b55fcb
commit 6bb44e5d05
2 changed files with 29 additions and 3 deletions

View File

@ -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/<cat>/<file_base>")]
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"] {

View File

@ -66,6 +66,5 @@ pub fn list(cat: PathBuf) -> Option<Json<Vec<VideoPreview>>> {
pub async fn get_video(cat: PathBuf, file: PathBuf) -> Option<NamedFile> {
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()
}