From b00424471cd9431fb130ba72721e9e53924d6118 Mon Sep 17 00:00:00 2001 From: JMARyA Date: Thu, 19 Sep 2024 16:44:44 +0200 Subject: [PATCH] update --- Cargo.lock | 38 +++++++++++++++++++------------------- docs/Library.md | 2 ++ src/library/album.rs | 5 +++-- src/library/artist.rs | 2 +- src/library/playlist.rs | 7 ++----- src/library/search.rs | 2 +- src/library/track.rs | 6 ++---- src/library/user.rs | 2 +- src/route/admin.rs | 6 +++--- src/route/album.rs | 12 ++++++------ src/route/artist.rs | 6 +++--- src/route/mod.rs | 15 --------------- src/route/playlist.rs | 10 +++++----- src/route/track.rs | 2 +- src/route/user.rs | 5 +++-- 15 files changed, 52 insertions(+), 68 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a033d35..7976442 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -254,9 +254,9 @@ dependencies = [ [[package]] name = "bson" -version = "2.11.0" +version = "2.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8a88e82b9106923b5c4d6edfca9e7db958d4e98a478ec115022e81b9b38e2c8" +checksum = "80cf6f7806607bd58ad490bab34bf60e25455ea4aaf995f897a13324d41ea580" dependencies = [ "ahash", "base64 0.13.1", @@ -293,9 +293,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "bytes" -version = "1.7.1" +version = "1.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8318a53db07bb3f8dca91a600466bdb3f2eaadeedfdbcf02e1accbad9271ba50" +checksum = "428d9aa8fbc0670b7b8d6030a7fadd0f86151cae55e4dbbece15f3780a3dfaf3" [[package]] name = "case" @@ -305,9 +305,9 @@ checksum = "fd6c0e7b807d60291f42f33f58480c0bfafe28ed08286446f45e463728cf9c1c" [[package]] name = "cc" -version = "1.1.18" +version = "1.1.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b62ac837cdb5cb22e10a256099b4fc502b1dfe560cb282963a974d7abd80e476" +checksum = "07b1695e2c7e8fc85310cde85aeaab7e3097f593c91d209d3f9df76c928100f0" dependencies = [ "shlex", ] @@ -896,9 +896,9 @@ dependencies = [ [[package]] name = "iana-time-zone" -version = "0.1.60" +version = "0.1.61" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141" +checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220" dependencies = [ "android_system_properties", "core-foundation-sys", @@ -1150,8 +1150,8 @@ dependencies = [ [[package]] name = "mongod" -version = "0.2.1" -source = "git+https://git.hydrar.de/jmarya/mongod#d830e677baddd79839d199b0c58091ff2ec41209" +version = "0.2.2" +source = "git+https://git.hydrar.de/jmarya/mongod#58150002e60802d7b00fe0537e4a2e8c4e389a4a" dependencies = [ "chrono", "futures", @@ -1167,7 +1167,7 @@ dependencies = [ [[package]] name = "mongod_derive" version = "0.1.0" -source = "git+https://git.hydrar.de/jmarya/mongod#d830e677baddd79839d199b0c58091ff2ec41209" +source = "git+https://git.hydrar.de/jmarya/mongod#58150002e60802d7b00fe0537e4a2e8c4e389a4a" dependencies = [ "case", "proc-macro2", @@ -1460,9 +1460,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.3" +version = "0.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2a908a6e00f1fdd0dfd9c0eb08ce85126f6d8bbda50017e74bc4a4b7d4a926a4" +checksum = "0884ad60e090bf1345b93da0a5de8923c93884cd03f40dfcfddd3b4bee661853" dependencies = [ "bitflags 2.6.0", ] @@ -1691,9 +1691,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.36" +version = "0.38.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f55e80d50763938498dd5ebb18647174e0c76dc38c5505294bb224624f30f36" +checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811" dependencies = [ "bitflags 2.6.0", "errno", @@ -2227,9 +2227,9 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.22.20" +version = "0.22.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "583c44c02ad26b0c3f3066fe629275e50627026c51ac2e595cca4c230ce1ce1d" +checksum = "3b072cee73c449a636ffd6f32bd8de3a9f7119139aff882f44943ce2986dc5cf" dependencies = [ "indexmap", "serde", @@ -2425,9 +2425,9 @@ checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" [[package]] name = "unicode-normalization" -version = "0.1.23" +version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a56d1686db2308d901306f92a263857ef59ea39678a5458e7cb17f01415101f5" +checksum = "5033c97c4262335cded6d6fc3e5c18ab755e1a3dc96376350f3d8e9f009ad956" dependencies = [ "tinyvec", ] diff --git a/docs/Library.md b/docs/Library.md index de4d63a..bfd6fff 100644 --- a/docs/Library.md +++ b/docs/Library.md @@ -1,6 +1,8 @@ # Library Synthwave searches for music files inside of the `./media` directory. The general structure should be like `Artist/Album/Track`. +It is assumed that an audio file is inside a "Album" folder and the "Album" folder is inside an "Artist" folder. +Besides that Synthwave enforces no folder structure, so something like `Category/Year/Artist/Album/Track` would also work. ## Artist Image The artists image is an image file named `artist` in the artists presumed directory, which is derived from the folder structure. This image can have animation. diff --git a/src/library/album.rs b/src/library/album.rs index c8ee4b5..430ee24 100644 --- a/src/library/album.rs +++ b/src/library/album.rs @@ -7,7 +7,8 @@ use mongodb::bson::doc; use serde::{Deserialize, Serialize}; use serde_json::json; -use crate::{library::artist::Artist, route::ToAPI}; +use crate::library::artist::Artist; +use mongod::ToAPI; use super::track::Track; @@ -24,7 +25,7 @@ impl Album { _id: uuid::Uuid::new_v4().to_string(), title: title.to_string(), artist_id: if let Some(artist) = artist { - Some(Reference::new_raw(artist).await.unwrap()) + Some(Reference::new_raw(artist)) } else { None }, diff --git a/src/library/artist.rs b/src/library/artist.rs index 3412979..557b29c 100644 --- a/src/library/artist.rs +++ b/src/library/artist.rs @@ -6,7 +6,7 @@ use mongodb::bson::doc; use serde::{Deserialize, Serialize}; use serde_json::json; -use crate::route::ToAPI; +use mongod::ToAPI; use super::track::Track; diff --git a/src/library/playlist.rs b/src/library/playlist.rs index 0ff515b..a1186b7 100644 --- a/src/library/playlist.rs +++ b/src/library/playlist.rs @@ -5,11 +5,8 @@ use mongod::{ }; use serde::{Deserialize, Serialize}; -use crate::{ - library::{track::Track, user::User}, - route::ToAPI, -}; - +use crate::library::{track::Track, user::User}; +use mongod::ToAPI; #[derive(Debug, Clone, Serialize, Deserialize, Model, Referencable)] pub struct Playlist { pub _id: String, diff --git a/src/library/search.rs b/src/library/search.rs index f92573a..680abfc 100644 --- a/src/library/search.rs +++ b/src/library/search.rs @@ -4,7 +4,7 @@ use mongod::Model; use mongodb::bson::doc; use serde::Serialize; -use crate::route::ToAPI; +use mongod::ToAPI; use super::{album::Album, artist::Artist, playlist::Playlist, track::Track}; diff --git a/src/library/track.rs b/src/library/track.rs index 1ece95d..4d1cd25 100644 --- a/src/library/track.rs +++ b/src/library/track.rs @@ -9,10 +9,8 @@ use mongodb::bson::doc; use serde::{Deserialize, Serialize}; use serde_json::json; -use crate::{ - library::{album::Album, artist::Artist}, - route::ToAPI, -}; +use crate::library::{album::Album, artist::Artist}; +use mongod::ToAPI; use super::{event::Event, metadata::AudioMetadata, user::User}; diff --git a/src/library/user.rs b/src/library/user.rs index 913239b..a3ee36f 100644 --- a/src/library/user.rs +++ b/src/library/user.rs @@ -9,7 +9,7 @@ use rand::RngCore; use serde::{Deserialize, Serialize}; use serde_json::json; -use crate::route::ToAPI; +use mongod::ToAPI; fn gen_token(token_length: usize) -> String { let mut token_bytes = vec![0u8; token_length]; diff --git a/src/route/admin.rs b/src/route/admin.rs index a434b40..957c3af 100644 --- a/src/route/admin.rs +++ b/src/route/admin.rs @@ -1,5 +1,6 @@ use super::api_error; use super::FallibleApiResponse; +use mongod::vec_to_api; use mongod::Model; use mongodb::bson::doc; use rocket::{get, State}; @@ -9,7 +10,6 @@ use crate::check_admin; use crate::library::track::Track; use crate::library::user::User; use crate::library::Libary; -use crate::route::to_api; #[get("/library/clean")] pub async fn clean_library(lib: &State, u: User) -> FallibleApiResponse { @@ -29,12 +29,12 @@ pub async fn get_singles_route(u: User) -> FallibleApiResponse { .await .unwrap(); - Ok(json!(to_api(&singles).await)) + Ok(json!(vec_to_api(&singles).await)) } #[get("/library/orphans")] pub async fn get_orphans_route(u: User) -> FallibleApiResponse { check_admin!(u); let orphans = Track::get_orphans().await; - Ok(json!(to_api(&orphans).await)) + Ok(json!(vec_to_api(&orphans).await)) } diff --git a/src/route/album.rs b/src/route/album.rs index 7d7abe8..c4269ae 100644 --- a/src/route/album.rs +++ b/src/route/album.rs @@ -2,7 +2,7 @@ use std::cmp::Ordering; use super::api_error; use super::FallibleApiResponse; -use super::ToAPI; +use mongod::vec_to_api; use mongod::Model; use mongod::Referencable; use mongodb::bson::doc; @@ -14,8 +14,8 @@ use crate::cache::RouteCache; use crate::library::album::Album; use crate::library::track::Track; use crate::library::Libary; -use crate::route::to_api; use crate::use_api_cache; +use mongod::ToAPI; #[get("/artist//albums")] pub async fn albums_route(artist_id: &str, lib: &State) -> FallibleApiResponse { @@ -24,8 +24,8 @@ pub async fn albums_route(artist_id: &str, lib: &State) -> FallibleApiRe Ok(json!({ "artist": artist_id, - "albums": to_api(&albums).await, - "singles": to_api(&singles).await + "albums": vec_to_api(&albums).await, + "singles": vec_to_api(&singles).await })) } @@ -82,7 +82,7 @@ pub async fn latest_albums_route(cache: &State) -> FallibleApiRespon let (albums, _): (Vec<_>, Vec<_>) = joined.into_iter().unzip(); - let albums = to_api(&albums).await; + let albums = vec_to_api(&albums).await; cache .insert("albums", "latest", serde_json::to_string(&albums).unwrap()) @@ -106,7 +106,7 @@ pub async fn album_route( let tracks = Album::get_tracks_of_album(&format!("album::{}", album._id)).await; - let mut tracks = to_api(&tracks).await; + let mut tracks = vec_to_api(&tracks).await; tracks.sort_by(sort_by_tracknumber); diff --git a/src/route/artist.rs b/src/route/artist.rs index 30fff23..055347a 100644 --- a/src/route/artist.rs +++ b/src/route/artist.rs @@ -1,9 +1,9 @@ use super::api_error; -use super::to_api; use super::FallibleApiResponse; -use super::ToAPI; use fs::NamedFile; +use mongod::vec_to_api; use mongod::Model; +use mongod::ToAPI; use mongodb::bson::doc; use rocket::{fs, get, State}; @@ -15,7 +15,7 @@ use crate::library::Libary; #[get("/artists")] pub async fn artists_route(lib: &State) -> FallibleApiResponse { let artists = lib.get_artists().await; - Ok(serde_json::to_value(&to_api(&artists).await).unwrap()) + Ok(serde_json::to_value(&vec_to_api(&artists).await).unwrap()) } #[get("/artist//image")] diff --git a/src/route/mod.rs b/src/route/mod.rs index 22ac0b3..2f20fa8 100644 --- a/src/route/mod.rs +++ b/src/route/mod.rs @@ -25,21 +25,6 @@ pub fn api_error(msg: &str) -> ApiError { })) } -pub trait ToAPI: Sized { - /// Generate public API JSON - fn api(&self) -> impl std::future::Future; -} - -pub async fn to_api(albums: &[impl ToAPI]) -> Vec { - let mut ret = Vec::new(); - - for e in albums { - ret.push(e.api().await); - } - - ret -} - #[get("/")] pub fn index_redir() -> Redirect { Redirect::to(uri!("/web")) diff --git a/src/route/playlist.rs b/src/route/playlist.rs index 0090643..576455a 100644 --- a/src/route/playlist.rs +++ b/src/route/playlist.rs @@ -1,6 +1,7 @@ use crate::library::track::Track; use crate::library::user::User; use mongod::reference_of; +use mongod::vec_to_api; use mongod::Model; use mongod::Referencable; use mongodb::bson::doc; @@ -14,8 +15,7 @@ use crate::library::playlist::Visibility; use crate::route::FallibleApiResponse; use super::api_error; -use super::to_api; -use super::ToAPI; +use mongod::ToAPI; #[get("/playlists")] pub async fn playlists_route(u: User) -> FallibleApiResponse { @@ -42,7 +42,7 @@ pub async fn recently_added_playlist() -> FallibleApiResponse { let tracks = Track::find(doc! {}, Some(90), Some(doc! { "date_added": -1 })) .await .unwrap(); - Ok(json!(to_api(&tracks).await)) + Ok(json!(vec_to_api(&tracks).await)) } #[get("/playlist/")] @@ -88,7 +88,7 @@ pub async fn playlist_route(id: &str, u: User) -> FallibleApiResponse { pub async fn playlist_tracks_route(id: &str, u: User) -> FallibleApiResponse { if id == "recents" { let tracks = Track::get_latest_of_user(&u).await; - return Ok(json!(to_api(&tracks).await)); + return Ok(json!(vec_to_api(&tracks).await)); } if id == "recentlyAdded" { @@ -111,7 +111,7 @@ pub async fn playlist_tracks_route(id: &str, u: User) -> FallibleApiResponse { tracks.push(track.get().await); } - Ok(json!(to_api(&tracks).await)) + Ok(json!(vec_to_api(&tracks).await)) } #[derive(rocket::serde::Deserialize)] diff --git a/src/route/track.rs b/src/route/track.rs index b1a69f0..8dcc11a 100644 --- a/src/route/track.rs +++ b/src/route/track.rs @@ -1,8 +1,8 @@ use super::api_error; use super::FallibleApiResponse; -use super::ToAPI; use crate::library::user::User; use fs::NamedFile; +use mongod::ToAPI; use mongodb::bson::doc; use rocket::{fs, get, State}; use serde_json::json; diff --git a/src/route/user.rs b/src/route/user.rs index 5df3719..40b5f40 100644 --- a/src/route/user.rs +++ b/src/route/user.rs @@ -1,7 +1,8 @@ use crate::library::user::Session; use crate::library::user::User; -use crate::route::to_api; +use mongod::vec_to_api; use mongod::Model; +use mongod::ToAPI; use mongodb::bson::doc; use rocket::get; use rocket::http::Status; @@ -83,7 +84,7 @@ pub async fn passwd_route(passwd: Json, u: User) -> FallibleApiRespo pub async fn users_route(u: User) -> FallibleApiResponse { check_admin!(u); - let users: Vec<_> = to_api(&User::find_all().await.unwrap()).await; + let users: Vec<_> = vec_to_api(&User::find_all().await.unwrap()).await; Ok(json!({"users": users})) }