This commit is contained in:
JMARyA 2024-12-25 17:34:13 +01:00
parent 1dfb3d4964
commit 7eb2b7d00d
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
7 changed files with 248 additions and 63 deletions

207
Cargo.lock generated
View file

@ -137,6 +137,12 @@ dependencies = [
"windows-targets 0.52.6", "windows-targets 0.52.6",
] ]
[[package]]
name = "base64"
version = "0.21.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567"
[[package]] [[package]]
name = "base64" name = "base64"
version = "0.22.1" version = "0.22.1"
@ -152,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#98048ce522db134fe3e863538db6793068085b80" source = "git+https://git.hydrar.de/jmarya/based#cd10c64a1f96703894de9e40a95fd81cc50d244a"
dependencies = [ dependencies = [
"bcrypt 0.16.0", "bcrypt 0.16.0",
"chrono", "chrono",
@ -166,6 +172,7 @@ dependencies = [
"rand", "rand",
"rayon", "rayon",
"regex", "regex",
"reqwest",
"ring", "ring",
"rocket", "rocket",
"rocket_cors", "rocket_cors",
@ -183,7 +190,7 @@ version = "0.15.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e65938ed058ef47d92cf8b346cc76ef48984572ade631927e9937b5ffc7662c7" checksum = "e65938ed058ef47d92cf8b346cc76ef48984572ade631927e9937b5ffc7662c7"
dependencies = [ dependencies = [
"base64", "base64 0.22.1",
"blowfish", "blowfish",
"getrandom", "getrandom",
"subtle", "subtle",
@ -196,7 +203,7 @@ version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b1866ecef4f2d06a0bb77880015fdf2b89e25a1c2e5addacb87e459c86dc67e" checksum = "2b1866ecef4f2d06a0bb77880015fdf2b89e25a1c2e5addacb87e459c86dc67e"
dependencies = [ dependencies = [
"base64", "base64 0.22.1",
"blowfish", "blowfish",
"getrandom", "getrandom",
"subtle", "subtle",
@ -209,6 +216,12 @@ version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "383d29d513d8764dcdc42ea295d979eb99c3c9f00607b3692cf68a431f7dca72" checksum = "383d29d513d8764dcdc42ea295d979eb99c3c9f00607b3692cf68a431f7dca72"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "2.6.0" version = "2.6.0"
@ -245,9 +258,9 @@ checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c"
[[package]] [[package]]
name = "bytemuck" name = "bytemuck"
version = "1.20.0" version = "1.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b37c88a63ffd85d15b406896cc343916d7cf57838a847b3a6f2ca5d39a5695a" checksum = "ef657dfab802224e671f5818e9a4935f9b1957ed18e58292690cc39e7a4092a3"
[[package]] [[package]]
name = "byteorder" name = "byteorder"
@ -263,9 +276,9 @@ checksum = "325918d6fe32f23b19878fe4b34794ae41fc19ddbe53b10571a4874d44ffd39b"
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.2.4" version = "1.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9157bbaa6b165880c27a4293a474c91cdcf265cc68cc829bf10be0964a391caf" checksum = "c31a0499c1dc64f458ad13872de75c0eb7e3fdb0e67964610c914b034fc5956e"
dependencies = [ dependencies = [
"shlex", "shlex",
] ]
@ -477,7 +490,7 @@ version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b035a542cf7abf01f2e3c4d5a7acbaebfefe120ae4efc7bde3df98186e4b8af7" checksum = "b035a542cf7abf01f2e3c4d5a7acbaebfefe120ae4efc7bde3df98186e4b8af7"
dependencies = [ dependencies = [
"bitflags", "bitflags 2.6.0",
"proc-macro2", "proc-macro2",
"proc-macro2-diagnostics", "proc-macro2-diagnostics",
"quote", "quote",
@ -533,9 +546,9 @@ dependencies = [
[[package]] [[package]]
name = "env_filter" name = "env_filter"
version = "0.1.2" version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f2c92ceda6ceec50f43169f9ee8424fe2db276791afde7b2cd8bc084cb376ab" checksum = "186e05a59d4c50738528153b83b0b0194d3a29507dfec16eccd4b342903397d0"
dependencies = [ dependencies = [
"log", "log",
] ]
@ -555,9 +568,9 @@ dependencies = [
[[package]] [[package]]
name = "env_logger" name = "env_logger"
version = "0.11.5" version = "0.11.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e13fa619b91fb2381732789fc5de83b45675e882f66623b7d8cb4f643017018d" checksum = "dcaee3d8e3cfc3fd92428d477bc97fc29ec8716d180c0d74c643bb26166660e0"
dependencies = [ dependencies = [
"env_filter", "env_filter",
"log", "log",
@ -978,6 +991,19 @@ dependencies = [
"want", "want",
] ]
[[package]]
name = "hyper-tls"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905"
dependencies = [
"bytes",
"hyper",
"native-tls",
"tokio",
"tokio-native-tls",
]
[[package]] [[package]]
name = "iana-time-zone" name = "iana-time-zone"
version = "0.1.61" version = "0.1.61"
@ -1166,6 +1192,12 @@ dependencies = [
"generic-array", "generic-array",
] ]
[[package]]
name = "ipnet"
version = "2.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddc24109865250148c2e0f3d25d4f0f479571723792d3802153c60922a4fb708"
[[package]] [[package]]
name = "is-terminal" name = "is-terminal"
version = "0.4.13" version = "0.4.13"
@ -1204,9 +1236,9 @@ dependencies = [
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.168" version = "0.2.169"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5aaeb2981e0606ca11d79718f8bb01164f1d6ed75080182d3abf017e6d244b6d" checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a"
[[package]] [[package]]
name = "libm" name = "libm"
@ -1468,9 +1500,9 @@ dependencies = [
[[package]] [[package]]
name = "object" name = "object"
version = "0.36.5" version = "0.36.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aedf0a2d09c573ed1d8d85b30c119153926a2b36dce0ab28322c09a117a4683e" checksum = "62948e14d923ea95ea2c7c86c71013138b66525b86bdc08d2dcc262bdb497b87"
dependencies = [ dependencies = [
"memchr", "memchr",
] ]
@ -1487,7 +1519,7 @@ version = "0.10.68"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6174bc48f102d208783c2c84bf931bb75927a617866870de8a4ea85597f871f5" checksum = "6174bc48f102d208783c2c84bf931bb75927a617866870de8a4ea85597f871f5"
dependencies = [ dependencies = [
"bitflags", "bitflags 2.6.0",
"cfg-if", "cfg-if",
"foreign-types", "foreign-types",
"libc", "libc",
@ -1768,7 +1800,7 @@ version = "0.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03a862b389f93e68874fbf580b9de08dd02facb9a788ebadaf4a3fd33cf58834" checksum = "03a862b389f93e68874fbf580b9de08dd02facb9a788ebadaf4a3fd33cf58834"
dependencies = [ dependencies = [
"bitflags", "bitflags 2.6.0",
] ]
[[package]] [[package]]
@ -1835,6 +1867,46 @@ version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
[[package]]
name = "reqwest"
version = "0.11.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd67538700a17451e7cba03ac727fb961abb7607553461627b97de0b89cf4a62"
dependencies = [
"base64 0.21.7",
"bytes",
"encoding_rs",
"futures-core",
"futures-util",
"h2",
"http 0.2.12",
"http-body",
"hyper",
"hyper-tls",
"ipnet",
"js-sys",
"log",
"mime",
"native-tls",
"once_cell",
"percent-encoding",
"pin-project-lite",
"rustls-pemfile",
"serde",
"serde_json",
"serde_urlencoded",
"sync_wrapper",
"system-configuration",
"tokio",
"tokio-native-tls",
"tower-service",
"url",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"winreg",
]
[[package]] [[package]]
name = "ring" name = "ring"
version = "0.16.20" version = "0.16.20"
@ -1981,13 +2053,22 @@ version = "0.38.42"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f93dc38ecbab2eb790ff964bb77fa94faf256fd3e73285fd7ba0903b76bedb85" checksum = "f93dc38ecbab2eb790ff964bb77fa94faf256fd3e73285fd7ba0903b76bedb85"
dependencies = [ dependencies = [
"bitflags", "bitflags 2.6.0",
"errno", "errno",
"libc", "libc",
"linux-raw-sys", "linux-raw-sys",
"windows-sys 0.59.0", "windows-sys 0.59.0",
] ]
[[package]]
name = "rustls-pemfile"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c74cae0a4cf6ccbbf5f359f08efdf8ee7e1dc532573bf0db71968cb56b1448c"
dependencies = [
"base64 0.21.7",
]
[[package]] [[package]]
name = "rustversion" name = "rustversion"
version = "1.0.18" version = "1.0.18"
@ -2036,7 +2117,7 @@ version = "2.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "897b2245f0b511c87893af39b033e5ca9cce68824c4d7e7630b5a1d339658d02" checksum = "897b2245f0b511c87893af39b033e5ca9cce68824c4d7e7630b5a1d339658d02"
dependencies = [ dependencies = [
"bitflags", "bitflags 2.6.0",
"core-foundation", "core-foundation",
"core-foundation-sys", "core-foundation-sys",
"libc", "libc",
@ -2075,9 +2156,9 @@ dependencies = [
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.133" version = "1.0.134"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7fceb2473b9166b2294ef05efcb65a3db80803f0b03ef86a5fc88a2b85ee377" checksum = "d00f4175c42ee48b15416f6193a959ba3a0d67fc699a0db9ad12df9f83991c7d"
dependencies = [ dependencies = [
"itoa", "itoa",
"memchr", "memchr",
@ -2326,8 +2407,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64bb4714269afa44aef2755150a0fc19d756fb580a67db8885608cf02f47d06a" checksum = "64bb4714269afa44aef2755150a0fc19d756fb580a67db8885608cf02f47d06a"
dependencies = [ dependencies = [
"atoi", "atoi",
"base64", "base64 0.22.1",
"bitflags", "bitflags 2.6.0",
"byteorder", "byteorder",
"bytes", "bytes",
"chrono", "chrono",
@ -2370,8 +2451,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6fa91a732d854c5d7726349bb4bb879bb9478993ceb764247660aee25f67c2f8" checksum = "6fa91a732d854c5d7726349bb4bb879bb9478993ceb764247660aee25f67c2f8"
dependencies = [ dependencies = [
"atoi", "atoi",
"base64", "base64 0.22.1",
"bitflags", "bitflags 2.6.0",
"byteorder", "byteorder",
"chrono", "chrono",
"crc", "crc",
@ -2471,15 +2552,21 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.90" version = "2.0.91"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31" checksum = "d53cbcb5a243bd33b7858b1d7f4aca2153490815872d86d955d6ea29f743c035"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "sync_wrapper"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160"
[[package]] [[package]]
name = "synstructure" name = "synstructure"
version = "0.13.1" version = "0.13.1"
@ -2499,7 +2586,7 @@ dependencies = [
"bcrypt 0.15.1", "bcrypt 0.15.1",
"chrono", "chrono",
"data-encoding", "data-encoding",
"env_logger 0.11.5", "env_logger 0.11.6",
"log", "log",
"rand", "rand",
"rocket", "rocket",
@ -2511,6 +2598,27 @@ dependencies = [
"walkdir", "walkdir",
] ]
[[package]]
name = "system-configuration"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba3a3adc5c275d719af8cb4272ea1c4a6d668a777f37e115f6d11ddbc1c8e0e7"
dependencies = [
"bitflags 1.3.2",
"core-foundation",
"system-configuration-sys",
]
[[package]]
name = "system-configuration-sys"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75fb188eb626b924683e3b95e3a48e63551fcfb51949de2f06a9d91dbee93c9"
dependencies = [
"core-foundation-sys",
"libc",
]
[[package]] [[package]]
name = "tempfile" name = "tempfile"
version = "3.14.0" version = "3.14.0"
@ -2606,9 +2714,9 @@ dependencies = [
[[package]] [[package]]
name = "tinyvec" name = "tinyvec"
version = "1.8.0" version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "445e881f4f6d382d5f27c034e25eb92edd7c784ceab92a0937db7f2e9471b938" checksum = "022db8904dfa342efe721985167e9fcd16c29b226db4397ed752a761cfce81e8"
dependencies = [ dependencies = [
"tinyvec_macros", "tinyvec_macros",
] ]
@ -2648,6 +2756,16 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "tokio-native-tls"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2"
dependencies = [
"native-tls",
"tokio",
]
[[package]] [[package]]
name = "tokio-stream" name = "tokio-stream"
version = "0.1.17" version = "0.1.17"
@ -2807,9 +2925,9 @@ dependencies = [
[[package]] [[package]]
name = "unicase" name = "unicase"
version = "2.8.0" version = "2.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e51b68083f157f853b6379db119d1c1be0e6e4dec98101079dec41f6f5cf6df" checksum = "75b844d17643ee918803943289730bec8aac480150456169e647ed0b576ba539"
[[package]] [[package]]
name = "unicase_serde" name = "unicase_serde"
@ -2973,6 +3091,19 @@ dependencies = [
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
[[package]]
name = "wasm-bindgen-futures"
version = "0.4.49"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38176d9b44ea84e9184eff0bc34cc167ed044f816accfe5922e54d84cf48eca2"
dependencies = [
"cfg-if",
"js-sys",
"once_cell",
"wasm-bindgen",
"web-sys",
]
[[package]] [[package]]
name = "wasm-bindgen-macro" name = "wasm-bindgen-macro"
version = "0.2.99" version = "0.2.99"
@ -3228,6 +3359,16 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "winreg"
version = "0.50.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1"
dependencies = [
"cfg-if",
"windows-sys 0.48.0",
]
[[package]] [[package]]
name = "write16" name = "write16"
version = "1.0.0" version = "1.0.0"

View file

@ -16,6 +16,7 @@ pub struct Album {
/// Optional foreign key referencing the artist ID. If present, this indicates that this album is associated with a specific artist. /// Optional foreign key referencing the artist ID. If present, this indicates that this album is associated with a specific artist.
pub artist: Option<uuid::Uuid>, pub artist: Option<uuid::Uuid>,
} }
impl Album { impl Album {
/// Creates a new album in the database with the given `title` and optional `artist`. /// Creates a new album in the database with the given `title` and optional `artist`.
pub async fn create(title: &str, artist: Option<uuid::Uuid>) -> Self { pub async fn create(title: &str, artist: Option<uuid::Uuid>) -> Self {

View file

@ -3,7 +3,6 @@ use based::get_pg;
use based::request::cache; use based::request::cache;
use library::Libary; use library::Libary;
use rocket::routes; use rocket::routes;
use rocket::tokio::sync::OnceCell;
use rocket::{http::Method, launch}; use rocket::{http::Method, launch};
mod library; mod library;

View file

@ -3,9 +3,10 @@ use crate::library::album::Album;
use crate::library::track::Track; use crate::library::track::Track;
use crate::library::Libary; use crate::library::Libary;
use based::request::api::{api_error, to_uuid, vec_to_api, FallibleApiResponse, ToAPI}; use based::request::api::{api_error, to_uuid, vec_to_api, FallibleApiResponse, ToAPI};
use based::request::assets::DataResponse;
use based::use_api_cache; use based::use_api_cache;
use rocket::fs::NamedFile; use rocket::tokio::io::AsyncReadExt;
use rocket::{get, State}; use rocket::{get, tokio, State};
use serde_json::json; use serde_json::json;
use std::cmp::Ordering; use std::cmp::Ordering;
@ -37,8 +38,9 @@ pub async fn album_cover_route(
album_id: &str, album_id: &str,
lib: &State<Libary>, lib: &State<Libary>,
cache: &State<RouteCache>, cache: &State<RouteCache>,
) -> Option<NamedFile> { ) -> Option<DataResponse> {
NamedFile::open( let mut buf = Vec::new();
tokio::fs::File::open(
cache cache
.get_option("album_cover_route", album_id, || async { .get_option("album_cover_route", album_id, || async {
let album = lib.get_album_by_id(&to_uuid(album_id).unwrap()).await?; let album = lib.get_album_by_id(&to_uuid(album_id).unwrap()).await?;
@ -47,7 +49,12 @@ pub async fn album_cover_route(
.await?, .await?,
) )
.await .await
.ok() .ok()?
.read(&mut buf)
.await
.ok()?;
Some(DataResponse::new(buf, "image/png", Some(60 * 60 * 24 * 5)))
} }
#[get("/albums/latest")] #[get("/albums/latest")]

View file

@ -1,6 +1,7 @@
use based::request::api::{api_error, to_uuid, vec_to_api, FallibleApiResponse, ToAPI}; use based::request::api::{api_error, to_uuid, vec_to_api, FallibleApiResponse, ToAPI};
use fs::NamedFile; use based::request::assets::DataResponse;
use rocket::{fs, get, State}; use rocket::tokio::io::AsyncReadExt;
use rocket::{get, tokio, State};
use crate::cache::RouteCache; use crate::cache::RouteCache;
use crate::library::artist::Artist; use crate::library::artist::Artist;
@ -14,14 +15,22 @@ pub async fn artists_route(lib: &State<Libary>) -> FallibleApiResponse {
} }
#[get("/artist/<id>/image")] #[get("/artist/<id>/image")]
pub async fn artist_image_route(id: &str, cache: &State<RouteCache>) -> Option<NamedFile> { pub async fn artist_image_route(id: &str, cache: &State<RouteCache>) -> Option<DataResponse> {
let image = cache let image = cache
.get_option("artist_image_route", id, || async { .get_option("artist_image_route", id, || async {
Artist::get_image_of(&to_uuid(id).ok()?).await Artist::get_image_of(&to_uuid(id).ok()?).await
}) })
.await; .await?;
NamedFile::open(image?).await.ok() let mut buf = Vec::new();
tokio::fs::File::open(image)
.await
.ok()?
.read(&mut buf)
.await
.ok()?;
Some(DataResponse::new(buf, "image/png", Some(60 * 60 * 24 * 5)))
} }
#[get("/artist/<id>")] #[get("/artist/<id>")]

View file

@ -1,11 +1,4 @@
use std::str::FromStr; use rocket::{get, response::Redirect, uri};
use rocket::{
get,
response::{status::BadRequest, Redirect},
uri,
};
use serde_json::json;
pub mod admin; pub mod admin;
pub mod album; pub mod album;

View file

@ -1,11 +1,18 @@
use crate::library::{track, Libary}; use crate::library::Libary;
use based::{ use based::{
auth::User, auth::User,
check_admin, check_admin,
request::api::{api_error, to_uuid, FallibleApiResponse, ToAPI}, request::{
api::{api_error, to_uuid, FallibleApiResponse, ToAPI},
assets::DataResponse,
},
};
use rocket::{
get,
tokio::{self, io::AsyncReadExt},
State,
}; };
use fs::NamedFile;
use rocket::{fs, get, State};
use serde_json::json; use serde_json::json;
#[get("/track/<track_id>")] #[get("/track/<track_id>")]
@ -32,23 +39,51 @@ pub async fn track_reload_meta_route(
} }
#[get("/track/<track_id>/audio")] #[get("/track/<track_id>/audio")]
pub async fn track_audio_route(track_id: &str, lib: &State<Libary>) -> Option<NamedFile> { 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 track = lib.get_track_by_id(&to_uuid(track_id).ok()?).await?;
NamedFile::open(std::path::Path::new(&track.path))
let mut buf = Vec::new();
tokio::fs::File::open(track.path)
.await .await
.ok() .ok()?
.read(&mut buf)
.await
.ok()?;
Some(DataResponse::new(buf, "audio/ogg", Some(60 * 60 * 24 * 5)))
} }
#[get("/track/<track_id>/audio/opus128")] #[get("/track/<track_id>/audio/opus128")]
pub async fn track_audio_opus128_route(track_id: &str, lib: &State<Libary>) -> Option<NamedFile> { 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 track = lib.get_track_by_id(&to_uuid(track_id).ok()?).await?;
NamedFile::open(track.get_opus(128)?).await.ok()
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")] #[get("/track/<track_id>/audio/aac128")]
pub async fn track_audio_aac128_route(track_id: &str, lib: &State<Libary>) -> Option<NamedFile> { 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 track = lib.get_track_by_id(&to_uuid(track_id).ok()?).await?;
NamedFile::open(track.get_aac(128)?).await.ok()
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")] #[get("/track/<track_id>/lyrics")]