From 207cda7883ddcb2f9c66ae7fc283c082a43620f3 Mon Sep 17 00:00:00 2001 From: JMARyA Date: Mon, 16 Dec 2024 22:21:09 +0100 Subject: [PATCH] raw video send --- src/pages/assets.rs | 29 ++++++++++++++++++++++++++--- src/pages/watch.rs | 2 +- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/pages/assets.rs b/src/pages/assets.rs index 27dca1a..010e31d 100644 --- a/src/pages/assets.rs +++ b/src/pages/assets.rs @@ -5,17 +5,34 @@ use rocket::{ State, }; +use tokio::{fs::File, io::AsyncReadExt}; + use crate::library::Library; #[get("/video/raw?")] -pub async fn video_file(v: &str, library: &State) -> Option { +pub async fn video_file( + v: &str, + library: &State, +) -> Option<(Status, (ContentType, Vec))> { let video = if let Some(video) = library.get_video_by_id(v).await { video } else { 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?")] @@ -32,7 +49,13 @@ pub async fn video_thumbnail(v: &str, library: &State) -> Option