101 lines
2.6 KiB
Rust
101 lines
2.6 KiB
Rust
use crate::library::Libary;
|
|
use based::{
|
|
auth::User,
|
|
check_admin,
|
|
request::{
|
|
api::{api_error, to_uuid, FallibleApiResponse, ToAPI},
|
|
assets::DataResponse,
|
|
},
|
|
};
|
|
|
|
use rocket::{
|
|
get,
|
|
tokio::{self, io::AsyncReadExt},
|
|
State,
|
|
};
|
|
use serde_json::json;
|
|
|
|
#[get("/track/<track_id>")]
|
|
pub async fn track_route(track_id: &str, lib: &State<Libary>) -> FallibleApiResponse {
|
|
Ok(lib
|
|
.get_track_by_id(&to_uuid(track_id)?)
|
|
.await
|
|
.ok_or_else(|| api_error("No track with that ID found"))?
|
|
.api()
|
|
.await)
|
|
}
|
|
|
|
#[get("/track/<track_id>/reload")]
|
|
pub async fn track_reload_meta_route(
|
|
track_id: &str,
|
|
lib: &State<Libary>,
|
|
u: User,
|
|
) -> FallibleApiResponse {
|
|
check_admin!(u);
|
|
lib.reload_metadata(&to_uuid(track_id)?)
|
|
.await
|
|
.map_err(|_| api_error("Error reloading metadata"))?;
|
|
Ok(json!({"ok": 1}))
|
|
}
|
|
|
|
#[get("/track/<track_id>/audio")]
|
|
pub async fn track_audio_route(track_id: &str, lib: &State<Libary>) -> Option<DataResponse> {
|
|
let track = lib.get_track_by_id(&to_uuid(track_id).ok()?).await?;
|
|
|
|
let mut buf = Vec::new();
|
|
tokio::fs::File::open(track.path)
|
|
.await
|
|
.ok()?
|
|
.read(&mut buf)
|
|
.await
|
|
.ok()?;
|
|
|
|
Some(DataResponse::new(buf, "audio/ogg", Some(60 * 60 * 24 * 5)))
|
|
}
|
|
|
|
#[get("/track/<track_id>/audio/opus128")]
|
|
pub async fn track_audio_opus128_route(
|
|
track_id: &str,
|
|
lib: &State<Libary>,
|
|
) -> Option<DataResponse> {
|
|
let track = lib.get_track_by_id(&to_uuid(track_id).ok()?).await?;
|
|
|
|
let mut buf = Vec::new();
|
|
tokio::fs::File::open(track.get_opus(128)?)
|
|
.await
|
|
.ok()?
|
|
.read(&mut buf)
|
|
.await
|
|
.ok()?;
|
|
|
|
Some(DataResponse::new(buf, "audio/opus", Some(60 * 60 * 24 * 5)))
|
|
}
|
|
|
|
#[get("/track/<track_id>/audio/aac128")]
|
|
pub async fn track_audio_aac128_route(track_id: &str, lib: &State<Libary>) -> Option<DataResponse> {
|
|
let track = lib.get_track_by_id(&to_uuid(track_id).ok()?).await?;
|
|
|
|
let mut buf = Vec::new();
|
|
tokio::fs::File::open(track.get_aac(128)?)
|
|
.await
|
|
.ok()?
|
|
.read(&mut buf)
|
|
.await
|
|
.ok()?;
|
|
|
|
Some(DataResponse::new(buf, "audio/aac", Some(60 * 60 * 24 * 5)))
|
|
}
|
|
|
|
#[get("/track/<track_id>/lyrics")]
|
|
pub async fn track_lyrics_route(track_id: &str, lib: &State<Libary>) -> FallibleApiResponse {
|
|
let track = lib
|
|
.get_track_by_id(&to_uuid(track_id)?)
|
|
.await
|
|
.ok_or_else(|| api_error("No such track"))?;
|
|
|
|
if let Some(lyrics) = track.get_lyrics().await {
|
|
return Ok(lyrics);
|
|
}
|
|
|
|
Ok(json!({}))
|
|
}
|