static files

This commit is contained in:
JMARyA 2024-12-24 15:58:31 +01:00
parent a0e7c5d3c1
commit 6c1f2bb84b
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
3 changed files with 15 additions and 23 deletions

2
Cargo.lock generated
View file

@ -158,7 +158,7 @@ checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b"
[[package]] [[package]]
name = "based" name = "based"
version = "0.1.0" version = "0.1.0"
source = "git+https://git.hydrar.de/jmarya/based#a89c5661208585b2a9f09d5ee337ad1c60ea9a49" source = "git+https://git.hydrar.de/jmarya/based#cd10c64a1f96703894de9e40a95fd81cc50d244a"
dependencies = [ dependencies = [
"bcrypt", "bcrypt",
"chrono", "chrono",

View file

@ -24,4 +24,4 @@ maud = "0.26.0"
rand = "0.8.5" rand = "0.8.5"
data-encoding = "2.6.0" data-encoding = "2.6.0"
bcrypt = "0.16.0" bcrypt = "0.16.0"
based = { git = "https://git.hydrar.de/jmarya/based", features = ["cache"] } based = { git = "https://git.hydrar.de/jmarya/based", features = ["cache", "htmx"] }

View file

@ -1,18 +1,12 @@
use rocket::{ use based::request::assets::DataResponse;
get, use rocket::{get, State};
http::{ContentType, Status},
State,
};
use tokio::{fs::File, io::AsyncReadExt}; 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( pub async fn video_file(v: &str, library: &State<Library>) -> Option<DataResponse> {
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 {
@ -23,22 +17,19 @@ pub async fn video_file(
let mut buf = Vec::with_capacity(51200); let mut buf = Vec::with_capacity(51200);
file.read_to_end(&mut buf).await.ok()?; file.read_to_end(&mut buf).await.ok()?;
let content_type = if video.path.ends_with("mp4") { let content_type = if video.path.ends_with("mp4") {
ContentType::new("video", "mp4") "video/mp4"
} else { } else {
ContentType::new("video", "webm") "video/webm"
}; };
return Some((Status::Ok, (content_type, buf))); return Some(DataResponse::new(buf, content_type, Some(60 * 60 * 24 * 3)));
} }
None None
} }
#[get("/video/thumbnail?<v>")] #[get("/video/thumbnail?<v>")]
pub async fn video_thumbnail( pub async fn video_thumbnail(v: &str, library: &State<Library>) -> Option<DataResponse> {
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 {
@ -46,16 +37,17 @@ pub async fn video_thumbnail(
}; };
if let Some(data) = library.get_thumbnail(&video).await { if let Some(data) = library.get_thumbnail(&video).await {
return Some((Status::Ok, (ContentType::PNG, data))); return Some(DataResponse::new(data, "image/png", Some(60 * 60 * 24 * 3)));
} }
None None
} }
#[get("/favicon")] #[get("/favicon")]
pub async fn fav_icon() -> (Status, (ContentType, &'static [u8])) { pub async fn fav_icon() -> DataResponse {
( DataResponse::new(
Status::Ok, include_bytes!("../../src/icon.png").to_vec(),
(ContentType::PNG, include_bytes!("../../src/icon.png")), "image/png",
Some(60 * 60 * 24 * 30),
) )
} }