79 lines
1.9 KiB
Rust
79 lines
1.9 KiB
Rust
use based::{auth::MaybeUser, request::assets::DataResponse};
|
|
use rocket::{get, State};
|
|
|
|
use tokio::{fs::File, io::AsyncReadExt};
|
|
|
|
use crate::{config::Config, library::Library};
|
|
|
|
#[get("/video/raw?<v>")]
|
|
pub async fn video_file(
|
|
v: &str,
|
|
library: &State<Library>,
|
|
conf: &State<Config>,
|
|
user: MaybeUser,
|
|
) -> Option<DataResponse> {
|
|
if conf.general.private && user.user().is_none() {
|
|
return None;
|
|
}
|
|
|
|
let video = if let Some(video) = library.get_video_by_id(v).await {
|
|
video
|
|
} else {
|
|
library.get_video_by_youtube_id(v).await.unwrap()
|
|
};
|
|
|
|
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") {
|
|
"video/mp4"
|
|
} else {
|
|
"video/webm"
|
|
};
|
|
|
|
return Some(DataResponse::new(
|
|
buf,
|
|
content_type.to_string(),
|
|
Some(60 * 60 * 24 * 3),
|
|
));
|
|
}
|
|
|
|
None
|
|
}
|
|
|
|
#[get("/video/thumbnail?<v>")]
|
|
pub async fn video_thumbnail(
|
|
v: &str,
|
|
library: &State<Library>,
|
|
conf: &State<Config>,
|
|
user: MaybeUser,
|
|
) -> Option<DataResponse> {
|
|
if conf.general.private && user.user().is_none() {
|
|
return None;
|
|
}
|
|
|
|
let video = if let Some(video) = library.get_video_by_id(v).await {
|
|
video
|
|
} else {
|
|
library.get_video_by_youtube_id(v).await.unwrap()
|
|
};
|
|
|
|
if let Some(data) = library.get_thumbnail(&video).await {
|
|
return Some(DataResponse::new(
|
|
data,
|
|
"image/png".to_string(),
|
|
Some(60 * 60 * 24 * 3),
|
|
));
|
|
}
|
|
|
|
None
|
|
}
|
|
|
|
#[get("/favicon")]
|
|
pub async fn fav_icon() -> DataResponse {
|
|
DataResponse::new(
|
|
include_bytes!("../../src/icon.png").to_vec(),
|
|
"image/png".to_string(),
|
|
Some(60 * 60 * 24 * 30),
|
|
)
|
|
}
|