diff --git a/api/src/category.rs b/api/src/category.rs index b3c277c..e49b576 100644 --- a/api/src/category.rs +++ b/api/src/category.rs @@ -1,8 +1,9 @@ use serde::Serialize; use std::fs::DirEntry; +use std::path::Path; use std::io; use rocket::serde::json::Json; -use crate::common::get_clips_dir; +use crate::common::{get_clips_dir, thumbs_dir}; #[derive(Serialize)] pub struct Category { @@ -34,6 +35,24 @@ pub fn get_category_dirs(path: &str) -> std::io::Result> { return Ok(ret); } +pub fn get_category_thumbnail(category: &str) -> std::io::Result { + let pathname = format!("{}{}", thumbs_dir(), &category); + let path = Path::new(&pathname); + println!("Path to use {:?}", path); + // Assume its a directory + let mut ret = "/static/cantfindshit.jpg".to_string(); + for ent in path.read_dir()? { + if let Ok(ent) = ent { + let name = ent.file_name().into_string().unwrap(); + if name == "category-thumbnail.jpg" { + ret = format!("/thumbnail/{}/{}", category, name); + } + } + } + + return Ok(ret); +} + #[get("/categories")] pub fn list() -> Json> { // WARN: misconfigured servers are just going to get shafted and serve up @@ -46,7 +65,10 @@ pub fn list() -> Json> { // That way we can do this blindly without 9999 allocs for d in dirs { let name = d.file_name().to_string_lossy().to_string(); - let thumbnail = format!("/thumbnail/{}/category-thumbnail.jpg", name); + let thumbnail = match get_category_thumbnail(&name) { + Ok(s) => s, + _ => "/static/cantfindshit.jpg".to_string() + }; cats.push(Category {name, thumbnail}); } } diff --git a/api/src/thumbnail.rs b/api/src/thumbnail.rs index da78e3a..7176fe9 100644 --- a/api/src/thumbnail.rs +++ b/api/src/thumbnail.rs @@ -11,17 +11,15 @@ pub async fn get(file: PathBuf) -> Option { if file_path.is_file() { return match file_path.extension() { Some(ext) => { - if ext == "jpg" || ext == "png" || ext == "jpeg" { - NamedFile::open(file_path).await.ok() - } else { - println!("bad extension"); - None + match ext == "jpg" { + true => NamedFile::open(file_path).await.ok(), + false => None } }, None => None } } else { - let path = Path::new("static/cantfindshit.gif"); + let path = Path::new("static/cantfindshit.jpg"); return NamedFile::open(path).await.ok(); } } diff --git a/api/src/video.rs b/api/src/video.rs index 8aec679..1c7d11f 100644 --- a/api/src/video.rs +++ b/api/src/video.rs @@ -48,7 +48,7 @@ pub fn list(cat: PathBuf) -> Option>> { let name = name.to_string_lossy(); let cat = cat.to_string_lossy(); - let thumbnail = format!("/thumbnail/{}/{}", cat, name); + let thumbnail = format!("/thumbnail/{}/{}.jpg", cat, name); let item = VideoPreview { name: name.to_string(), diff --git a/api/static/cantfindshit.gif b/api/static/cantfindshit.gif deleted file mode 100644 index 270cddb..0000000 Binary files a/api/static/cantfindshit.gif and /dev/null differ diff --git a/api/static/cantfindshit.jpg b/api/static/cantfindshit.jpg new file mode 100644 index 0000000..59f704d --- /dev/null +++ b/api/static/cantfindshit.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bc7637a1b3f7e75322e3dddb3499931dc9cd57804afbf46c7941e6bc5211d03c +size 21076 diff --git a/scripts/tn-tree-clone.sh b/scripts/tn-tree-clone.sh new file mode 100644 index 0000000..496fe04 --- /dev/null +++ b/scripts/tn-tree-clone.sh @@ -0,0 +1,50 @@ +#!/bin/sh + +set -e + +# This script basically clones the tree structure of the videos directory +# At the same time it will generate thumbnails for each video + +vids="$1" # root dir for videos that we're going to clone +thumbs="$2" # root dir for thumbnails we'll create + +_show_usage() { +cat << EOF +Generate thumbnails for a whole tree of clips +Usage: + $0 CLIPS_ROOT_PATH TARGET_ROOT +EOF +exit 1 +} + +if [ -z "$vids" ];then + echo 'Missing root video directory!' + _show_usage +fi + +if [ -z "$thumbs" ];then + echo 'Missing target thumbnails directory' + _show_usage +fi + +echo Cloning directory structure +pushd "$vids" > /dev/null + find . -type d | while read f; do + mkdir -p "$thumbs/$f" + done +popd > /dev/null + +echo Generating thumbnails +thumbs="`realpath "$thumbs"`" +pushd "$vids" > /dev/null + find . -type f -name '*.mkv' -o -name '*.mp4' -o -name '*.webm' | while read f; do + if="`realpath "${vids}/$f"`" + of="`realpath "${thumbs}/$f"`" + # Make sure only errors pop up here + ffmpeg -hide_banner -loglevel error \ + -y -ss 00:00:01 -i "$if" -frames:v 1 "$of.jpg" > /dev/null + ffmpeg -hide_banner -loglevel error \ + -y -i "$of.jpg" -vf scale=640:-1 "$of.jpg" > /dev/null + echo $of + done +popd > /dev/null \ No newline at end of file