synthwave/src/route/track.rs
2024-08-01 16:36:54 +02:00

32 lines
951 B
Rust

use super::api_error;
use super::FallibleApiResponse;
use super::ToAPI;
use fs::NamedFile;
use mongodb::bson::doc;
use rocket::*;
use crate::library::Libary;
#[get("/track/<track_id>")]
pub async fn track_route(track_id: &str, lib: &State<Libary>) -> FallibleApiResponse {
Ok(lib
.get_track_by_id(track_id)
.await
.ok_or_else(|| api_error("No track with that ID found"))?
.api()
.await)
}
#[get("/track/<track_id>/audio")]
pub async fn track_audio_route(track_id: &str, lib: &State<Libary>) -> Option<NamedFile> {
let track = lib.get_track_by_id(track_id).await?;
NamedFile::open(std::path::Path::new(&track.path))
.await
.ok()
}
#[get("/track/<track_id>/audio/opus128")]
pub async fn track_audio_opus128_route(track_id: &str, lib: &State<Libary>) -> Option<NamedFile> {
let track = lib.get_track_by_id(track_id).await?;
NamedFile::open(track.get_opus(128)).await.ok()
}