This commit is contained in:
parent
8fb4d5a5c5
commit
207cda7883
2 changed files with 27 additions and 4 deletions
|
@ -5,17 +5,34 @@ use rocket::{
|
||||||
State,
|
State,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use tokio::{fs::File, io::AsyncReadExt};
|
||||||
|
|
||||||
use crate::library::Library;
|
use crate::library::Library;
|
||||||
|
|
||||||
#[get("/video/raw?<v>")]
|
#[get("/video/raw?<v>")]
|
||||||
pub async fn video_file(v: &str, library: &State<Library>) -> Option<NamedFile> {
|
pub async fn video_file(
|
||||||
|
v: &str,
|
||||||
|
library: &State<Library>,
|
||||||
|
) -> Option<(Status, (ContentType, Vec<u8>))> {
|
||||||
let video = if let Some(video) = library.get_video_by_id(v).await {
|
let video = if let Some(video) = library.get_video_by_id(v).await {
|
||||||
video
|
video
|
||||||
} else {
|
} else {
|
||||||
library.get_video_by_youtube_id(v).await.unwrap()
|
library.get_video_by_youtube_id(v).await.unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
NamedFile::open(video.path).await.ok()
|
if let Ok(mut file) = File::open(&video.path).await {
|
||||||
|
let mut buf = Vec::with_capacity(51200);
|
||||||
|
file.read_to_end(&mut buf).await.ok()?;
|
||||||
|
let content_type = if video.path.ends_with("mp4") {
|
||||||
|
ContentType::new("video", "mp4")
|
||||||
|
} else {
|
||||||
|
ContentType::new("video", "webm")
|
||||||
|
};
|
||||||
|
|
||||||
|
return Some((Status::Ok, (content_type, buf)));
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/video/thumbnail?<v>")]
|
#[get("/video/thumbnail?<v>")]
|
||||||
|
@ -32,7 +49,13 @@ pub async fn video_thumbnail(v: &str, library: &State<Library>) -> Option<NamedF
|
||||||
let thumbnail_path = parent.join(thumbnail_path);
|
let thumbnail_path = parent.join(thumbnail_path);
|
||||||
let thumbnail_path = thumbnail_path.to_str().unwrap();
|
let thumbnail_path = thumbnail_path.to_str().unwrap();
|
||||||
|
|
||||||
NamedFile::open(format!("{thumbnail_path}.jpg")).await.ok()
|
for ext in ["jpg", "jpeg", "png", "avif"] {
|
||||||
|
if let Ok(file) = NamedFile::open(format!("{thumbnail_path}.{ext}")).await {
|
||||||
|
return Some(file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/favicon")]
|
#[get("/favicon")]
|
||||||
|
|
|
@ -40,7 +40,7 @@ pub async fn watch_page(
|
||||||
controls
|
controls
|
||||||
autoplay
|
autoplay
|
||||||
class="w-full h-full" {
|
class="w-full h-full" {
|
||||||
source src=(format!("/video/raw?v={}", video.id)) {
|
source src=(format!("/video/raw?v={}", video.id)) type="video/mp4" {
|
||||||
"Your browser does not support the video"
|
"Your browser does not support the video"
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue