add thumbnail feature
All checks were successful
ci/woodpecker/push/build Pipeline was successful

This commit is contained in:
JMARyA 2024-12-16 23:45:15 +01:00
parent 0d0265ab14
commit 764bef6457
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
5 changed files with 113 additions and 11 deletions

View file

@ -36,23 +36,18 @@ pub async fn video_file(
}
#[get("/video/thumbnail?<v>")]
pub async fn video_thumbnail(v: &str, library: &State<Library>) -> Option<NamedFile> {
pub async fn video_thumbnail(
v: &str,
library: &State<Library>,
) -> Option<(Status, (ContentType, Vec<u8>))> {
let video = if let Some(video) = library.get_video_by_id(v).await {
video
} else {
library.get_video_by_youtube_id(v).await.unwrap()
};
let path = std::path::Path::new(&video.path);
let parent = path.parent().unwrap();
let thumbnail_path = path.file_stem().unwrap().to_str().unwrap();
let thumbnail_path = parent.join(thumbnail_path);
let thumbnail_path = thumbnail_path.to_str().unwrap();
for ext in ["jpg", "jpeg", "png", "avif"] {
if let Ok(file) = NamedFile::open(format!("{thumbnail_path}.{ext}")).await {
return Some(file);
}
if let Some(data) = library.get_thumbnail(&video).await {
return Some((Status::Ok, (ContentType::PNG, data)));
}
None