From da86c3e0544ea5a9170888ddeec5c413d834d804 Mon Sep 17 00:00:00 2001 From: phoenix Date: Tue, 14 Oct 2025 03:11:29 +0000 Subject: [PATCH] tsk-52: Cleanup bug (#53) Closes #52 Reviewed-on: https://git.kundeng.us/phoenix/songparser/pulls/53 Co-authored-by: phoenix Co-committed-by: phoenix --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/api.rs | 203 ++++++++++++++++++++++++++++++++++++-- src/main.rs | 57 ++++++----- src/responses.rs | 17 ---- src/the_rest.rs | 128 ------------------------ src/update_queued_song.rs | 36 ------- 7 files changed, 228 insertions(+), 217 deletions(-) delete mode 100644 src/responses.rs delete mode 100644 src/the_rest.rs delete mode 100644 src/update_queued_song.rs diff --git a/Cargo.lock b/Cargo.lock index f702a6c..a08966e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1349,7 +1349,7 @@ dependencies = [ [[package]] name = "songparser" -version = "0.4.0" +version = "0.4.1" dependencies = [ "futures", "icarus_envy", diff --git a/Cargo.toml b/Cargo.toml index 50e39f1..8559bc1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "songparser" -version = "0.4.0" +version = "0.4.1" edition = "2024" rust-version = "1.90" diff --git a/src/api.rs b/src/api.rs index ff23283..7beaec0 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1,12 +1,33 @@ -pub async fn fetch_next_queue_item( - app: &crate::config::App, -) -> Result { - let client = reqwest::Client::new(); - let fetch_endpoint = String::from("api/v2/song/queue/next"); - let api_url = format!("{}/{fetch_endpoint}", app.uri); - let (key, header) = auth_header(app).await; +pub mod fetch_next_queue_item { - client.get(api_url).header(key, header).send().await + pub async fn fetch_next_queue_item( + app: &crate::config::App, + ) -> Result { + let client = reqwest::Client::new(); + let fetch_endpoint = String::from("api/v2/song/queue/next"); + let api_url = format!("{}/{fetch_endpoint}", app.uri); + let (key, header) = super::auth_header(app).await; + + client.get(api_url).header(key, header).send().await + } + + pub mod response { + use serde::{Deserialize, Serialize}; + + #[derive(Debug, Deserialize, Serialize)] + pub struct QueueItem { + pub id: uuid::Uuid, + pub filename: String, + pub status: String, + pub user_id: uuid::Uuid, + } + + #[derive(Debug, Deserialize, Serialize)] + pub struct SongQueueItem { + pub message: String, + pub data: Vec, + } + } } pub async fn auth_header( @@ -167,3 +188,169 @@ pub mod refresh_token { } } } + +pub mod update_queued_song { + pub async fn update_queued_song( + app: &crate::config::App, + song_path: &String, + song_queue_id: &uuid::Uuid, + ) -> Result { + let client = reqwest::Client::builder().build()?; + + println!("Song path: {song_path:?}"); + + // TODO: Make the filename random + let form = reqwest::multipart::Form::new().part( + "file", + reqwest::multipart::Part::bytes(std::fs::read(song_path).unwrap()) + .file_name("track01.flac"), + ); + + let url = format!("{}/api/v2/song/queue/{song_queue_id}", app.uri); + println!("Url: {url:?}"); + + let (key, header) = crate::api::auth_header(app).await; + let request = client.patch(url).multipart(form).header(key, header); + + let response = request.send().await?; + + Ok(response) + } + + pub mod response { + use serde::{Deserialize, Serialize}; + + #[derive(Debug, Deserialize, Serialize)] + pub struct Response { + pub message: String, + pub data: Vec, + } + } +} + +pub mod create_song { + pub async fn create( + app: &crate::config::App, + metadata_queue: &crate::api::get_metadata_queue::response::Metadata, + user_id: &uuid::Uuid, + song_type: &String, + ) -> Result { + let payload = serde_json::json!( + { + "album": &metadata_queue.album, + "album_artist": &metadata_queue.album_artist, + "artist": &metadata_queue.artist, + "disc": metadata_queue.disc, + "disc_count": metadata_queue.disc_count, + "duration": metadata_queue.duration, + "genre": &metadata_queue.genre, + "title": &metadata_queue.title, + "track": metadata_queue.track, + "track_count": metadata_queue.track_count, + "date": metadata_queue.year.to_string(), + "audio_type": &song_type, + "user_id": &user_id, + "song_queue_id": &metadata_queue.song_queue_id, + } + ); + + let client = reqwest::Client::builder().build()?; + + let url = format!("{}/api/v2/song", app.uri); + let (key, header) = crate::api::auth_header(app).await; + + let request = client.post(url).json(&payload).header(key, header); + request.send().await + } + + pub mod response { + #[derive(Debug, serde::Deserialize, serde::Serialize)] + pub struct Response { + pub message: String, + pub data: Vec, + } + } +} + +pub mod create_coverart { + + pub async fn create( + app: &crate::config::App, + song_id: &uuid::Uuid, + coverart_queue_id: &uuid::Uuid, + ) -> Result { + let client = reqwest::Client::builder().build()?; + let url = format!("{}/api/v2/coverart", app.uri); + let payload = get_payload(song_id, coverart_queue_id); + let (key, header) = crate::api::auth_header(app).await; + let request = client.post(url).json(&payload).header(key, header); + + request.send().await + } + + fn get_payload(song_id: &uuid::Uuid, coverart_queue_id: &uuid::Uuid) -> serde_json::Value { + serde_json::json!({ + "song_id": &song_id, + "coverart_queue_id": &coverart_queue_id, + }) + } + + pub mod response { + #[derive(Debug, serde::Deserialize, serde::Serialize)] + pub struct Response { + pub message: String, + pub data: Vec, + } + } +} + +pub mod wipe_data { + pub mod song_queue { + pub async fn wipe_data( + app: &crate::config::App, + song_queue_id: &uuid::Uuid, + ) -> Result { + let client = reqwest::Client::builder().build()?; + let url = format!("{}/api/v2/song/queue/data/wipe", app.uri); + let payload = serde_json::json!({ + "song_queue_id": song_queue_id + }); + let (key, header) = crate::api::auth_header(app).await; + let request = client.patch(url).json(&payload).header(key, header); + + request.send().await + } + + pub mod response { + #[derive(Debug, serde::Deserialize, serde::Serialize)] + pub struct Response { + pub message: String, + pub data: Vec, + } + } + } + pub mod coverart_queue { + pub async fn wipe_data( + app: &crate::config::App, + coverart_queue_id: &uuid::Uuid, + ) -> Result { + let client = reqwest::Client::builder().build()?; + let url = format!("{}/api/v2/coverart/queue/data/wipe", app.uri); + let payload = serde_json::json!({ + "coverart_queue_id": coverart_queue_id + }); + let (key, header) = crate::api::auth_header(app).await; + let request = client.patch(url).json(&payload).header(key, header); + + request.send().await + } + + pub mod response { + #[derive(Debug, serde::Deserialize, serde::Serialize)] + pub struct Response { + pub message: String, + pub data: Vec, + } + } + } +} diff --git a/src/main.rs b/src/main.rs index 88c20d9..a8a92ae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,5 @@ pub mod api; pub mod config; -pub mod responses; -pub mod the_rest; -pub mod update_queued_song; pub mod util; pub const SECONDS_TO_SLEEP: u64 = 5; @@ -59,7 +56,6 @@ async fn main() -> Result<(), Box> { let song_queue_id = song_queue_item.data[0].id; let user_id = song_queue_item.data[0].user_id; - // TODO: Do something with the result later match some_work(&app, &song_queue_id, &user_id).await { Ok(( song, @@ -174,15 +170,15 @@ async fn wipe_data_from_queues( song_queue_id: &uuid::Uuid, coverart_queue_id: &uuid::Uuid, ) -> Result<(), std::io::Error> { - match the_rest::wipe_data::song_queue::wipe_data(app, song_queue_id).await { + match api::wipe_data::song_queue::wipe_data(app, song_queue_id).await { Ok(response) => match response - .json::() + .json::() .await { Ok(_resp) => { - match the_rest::wipe_data::coverart_queue::wipe_data(app, coverart_queue_id).await { + match api::wipe_data::coverart_queue::wipe_data(app, coverart_queue_id).await { Ok(inner_response) => match inner_response - .json::() + .json::() .await { Ok(_inner_resp) => { @@ -220,11 +216,11 @@ async fn cleanup( async fn is_queue_empty( app: &config::App, -) -> Result<(bool, responses::fetch_next_queue_item::SongQueueItem), reqwest::Error> { - match api::fetch_next_queue_item(app).await { +) -> Result<(bool, api::fetch_next_queue_item::response::SongQueueItem), reqwest::Error> { + match api::fetch_next_queue_item::fetch_next_queue_item(app).await { Ok(response) => { match response - .json::() + .json::() .await { Ok(response) => { @@ -255,10 +251,15 @@ async fn some_work( std::io::Error, > { match prep_song(app, song_queue_id).await { - Ok((song_queue_path, coverart_queue_path, metadata, coverart_queue_id)) => { + Ok(((song_directory, song_filename), coverart_queue_path, metadata, coverart_queue_id)) => { + let mut song_queue_path: String = String::new(); + let p = std::path::Path::new(&song_directory); + let sp = p.join(&song_filename); + song_queue_path.push_str(sp.to_str().unwrap_or_default()); + match apply_metadata(&song_queue_path, &coverart_queue_path, &metadata).await { Ok(_applied) => { - match update_queued_song::update_queued_song( + match api::update_queued_song::update_queued_song( app, &song_queue_path, song_queue_id, @@ -267,7 +268,7 @@ async fn some_work( { Ok(response) => { match response - .json::() + .json::() .await { Ok(_inner_response) => { @@ -276,25 +277,29 @@ async fn some_work( // TODO: Place this somewhere else let song_type = String::from("flac"); - match the_rest::create_song::create( + match api::create_song::create( app, &metadata, user_id, &song_type, ) .await { Ok(response) => match response - .json::() + .json::() .await { Ok(resp) => { println!("Response: {resp:?}"); - let song = &resp.data[0]; - match the_rest::create_coverart::create(app, &song.id, &coverart_queue_id).await { - Ok(response) => match response.json::().await { + let mut song = resp.data[0].clone(); + song.directory = song_directory; + song.filename = song_filename; + + match api::create_coverart::create(app, &song.id, &coverart_queue_id).await { + Ok(response) => match response.json::().await { Ok(resp) => { println!("CoverArt sent and successfully parsed response"); println!("json: {resp:?}"); - let coverart = &resp.data[0]; + let mut coverart = resp.data[0].clone(); + coverart.path = coverart_queue_path.clone(); Ok((song.clone(), coverart.clone(), (metadata.song_queue_id, song_queue_path), (coverart_queue_id, coverart_queue_path))) } Err(err) => { @@ -329,7 +334,7 @@ async fn prep_song( song_queue_id: &uuid::Uuid, ) -> Result< ( - String, + (String, String), String, api::get_metadata_queue::response::Metadata, uuid::Uuid, @@ -341,10 +346,11 @@ async fn prep_song( // Process data here... match api::parsing::parse_response_into_bytes(response).await { Ok(song_bytes) => { - let (directory, filename) = generate_song_queue_dir_and_filename().await; + let (song_directory, song_filename) = + generate_song_queue_dir_and_filename().await; let song = icarus_models::song::Song { - directory, - filename, + directory: song_directory, + filename: song_filename, data: song_bytes, ..Default::default() }; @@ -392,8 +398,7 @@ async fn prep_song( println!("Saved coverart queue file at: {coverart_queue_path:?}"); let c_path = util::path_buf_to_string(coverart_queue_path); - let s_path = util::path_buf_to_string(song_queue_path); - Ok((s_path, c_path, metadata.clone(), *coverart_queue_id)) + Ok(((song.directory, song.filename), c_path, metadata.clone(), *coverart_queue_id)) } Err(err) => { Err(err) diff --git a/src/responses.rs b/src/responses.rs deleted file mode 100644 index 3f990a5..0000000 --- a/src/responses.rs +++ /dev/null @@ -1,17 +0,0 @@ -pub mod fetch_next_queue_item { - use serde::{Deserialize, Serialize}; - - #[derive(Debug, Deserialize, Serialize)] - pub struct QueueItem { - pub id: uuid::Uuid, - pub filename: String, - pub status: String, - pub user_id: uuid::Uuid, - } - - #[derive(Debug, Deserialize, Serialize)] - pub struct SongQueueItem { - pub message: String, - pub data: Vec, - } -} diff --git a/src/the_rest.rs b/src/the_rest.rs deleted file mode 100644 index 5d66e39..0000000 --- a/src/the_rest.rs +++ /dev/null @@ -1,128 +0,0 @@ -// TODO: Refactor this file when this app is functional - -pub mod create_song { - pub async fn create( - app: &crate::config::App, - metadata_queue: &crate::api::get_metadata_queue::response::Metadata, - user_id: &uuid::Uuid, - song_type: &String, - ) -> Result { - let payload = serde_json::json!( - { - "album": &metadata_queue.album, - "album_artist": &metadata_queue.album_artist, - "artist": &metadata_queue.artist, - "disc": metadata_queue.disc, - "disc_count": metadata_queue.disc_count, - "duration": metadata_queue.duration, - "genre": &metadata_queue.genre, - "title": &metadata_queue.title, - "track": metadata_queue.track, - "track_count": metadata_queue.track_count, - "date": metadata_queue.year.to_string(), - "audio_type": &song_type, - "user_id": &user_id, - "song_queue_id": &metadata_queue.song_queue_id, - } - ); - - let client = reqwest::Client::builder().build()?; - - let url = format!("{}/api/v2/song", app.uri); - let (key, header) = crate::api::auth_header(app).await; - - let request = client.post(url).json(&payload).header(key, header); - request.send().await - } - - pub mod response { - #[derive(Debug, serde::Deserialize, serde::Serialize)] - pub struct Response { - pub message: String, - pub data: Vec, - } - } -} - -pub mod create_coverart { - - pub async fn create( - app: &crate::config::App, - song_id: &uuid::Uuid, - coverart_queue_id: &uuid::Uuid, - ) -> Result { - let client = reqwest::Client::builder().build()?; - let url = format!("{}/api/v2/coverart", app.uri); - let payload = get_payload(song_id, coverart_queue_id); - let (key, header) = crate::api::auth_header(app).await; - let request = client.post(url).json(&payload).header(key, header); - - request.send().await - } - - fn get_payload(song_id: &uuid::Uuid, coverart_queue_id: &uuid::Uuid) -> serde_json::Value { - serde_json::json!({ - "song_id": &song_id, - "coverart_queue_id": &coverart_queue_id, - }) - } - - pub mod response { - #[derive(Debug, serde::Deserialize, serde::Serialize)] - pub struct Response { - pub message: String, - pub data: Vec, - } - } -} - -pub mod wipe_data { - pub mod song_queue { - pub async fn wipe_data( - app: &crate::config::App, - song_queue_id: &uuid::Uuid, - ) -> Result { - let client = reqwest::Client::builder().build()?; - let url = format!("{}/api/v2/song/queue/data/wipe", app.uri); - let payload = serde_json::json!({ - "song_queue_id": song_queue_id - }); - let (key, header) = crate::api::auth_header(app).await; - let request = client.patch(url).json(&payload).header(key, header); - - request.send().await - } - - pub mod response { - #[derive(Debug, serde::Deserialize, serde::Serialize)] - pub struct Response { - pub message: String, - pub data: Vec, - } - } - } - pub mod coverart_queue { - pub async fn wipe_data( - app: &crate::config::App, - coverart_queue_id: &uuid::Uuid, - ) -> Result { - let client = reqwest::Client::builder().build()?; - let url = format!("{}/api/v2/coverart/queue/data/wipe", app.uri); - let payload = serde_json::json!({ - "coverart_queue_id": coverart_queue_id - }); - let (key, header) = crate::api::auth_header(app).await; - let request = client.patch(url).json(&payload).header(key, header); - - request.send().await - } - - pub mod response { - #[derive(Debug, serde::Deserialize, serde::Serialize)] - pub struct Response { - pub message: String, - pub data: Vec, - } - } - } -} diff --git a/src/update_queued_song.rs b/src/update_queued_song.rs deleted file mode 100644 index 7a95dac..0000000 --- a/src/update_queued_song.rs +++ /dev/null @@ -1,36 +0,0 @@ -pub async fn update_queued_song( - app: &crate::config::App, - song_path: &String, - song_queue_id: &uuid::Uuid, -) -> Result { - let client = reqwest::Client::builder().build()?; - - println!("Song path: {song_path:?}"); - - // TODO: Make the filename random - let form = reqwest::multipart::Form::new().part( - "file", - reqwest::multipart::Part::bytes(std::fs::read(song_path).unwrap()) - .file_name("track01.flac"), - ); - - let url = format!("{}/api/v2/song/queue/{song_queue_id}", app.uri); - println!("Url: {url:?}"); - - let (key, header) = crate::api::auth_header(app).await; - let request = client.patch(url).multipart(form).header(key, header); - - let response = request.send().await?; - - Ok(response) -} - -pub mod response { - use serde::{Deserialize, Serialize}; - - #[derive(Debug, Deserialize, Serialize)] - pub struct Response { - pub message: String, - pub data: Vec, - } -}