From c60f7a44aad4bb80c67fbcdeb56ea2ca72920581 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 17 Jun 2025 20:54:48 -0400 Subject: [PATCH 1/6] Fixed endpoint for getting coverart queue data --- src/callers/coverart.rs | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/callers/coverart.rs b/src/callers/coverart.rs index fce6d80..218ef49 100644 --- a/src/callers/coverart.rs +++ b/src/callers/coverart.rs @@ -326,6 +326,8 @@ pub mod cov_db { pub mod endpoint { use std::io::Write; + use axum::response::IntoResponse; + pub async fn queue( axum::Extension(pool): axum::Extension, mut multipart: axum::extract::Multipart, @@ -447,15 +449,29 @@ pub mod endpoint { pub async fn fetch_coverart_with_data( axum::Extension(pool): axum::Extension, - axum::extract::Query(params): axum::extract::Query< - super::request::fetch_coverart_with_data::Params, - >, + axum::extract::Path(id): axum::extract::Path, ) -> ( axum::http::StatusCode, - axum::Json, + axum::response::Response, ) { - let mut response = super::response::fetch_coverart_with_data::Response::default(); + match super::db::get_coverart_queue_data_with_id(&pool, &id).await { + Ok(data) => { + let bytes = axum::body::Bytes::from(data); + let mut response = bytes.into_response(); + let headers = response.headers_mut(); + // TODO: Address this hard coding for the coverart content type + headers.insert(axum::http::header::CONTENT_TYPE, "image".parse().unwrap()); + // TODO: Make the conent disposition more dynamic + headers.insert(axum::http::header::CONTENT_DISPOSITION, format!("attachment; filename=\"{}.jpg\"", id).parse().unwrap()); + (axum::http::StatusCode::OK, response) + } + Err(_err) => { + (axum::http::StatusCode::BAD_REQUEST, axum::response::Response::default()) + } + } + + /* match params.id { Some(id) => match super::db::get_coverart_queue_data_with_id(&pool, &id).await { Ok(cover_art_queue) => { @@ -491,6 +507,7 @@ pub mod endpoint { } }, } + */ } pub async fn create_coverart( -- 2.47.3 From 9e79352acb0694ba7d71c235b4bab7356c211406 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 17 Jun 2025 21:04:38 -0400 Subject: [PATCH 2/6] Modified test --- src/main.rs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1fee55c..5c8dd83 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1257,11 +1257,7 @@ mod tests { "Should not be empty" ); - let uri = format!( - "{}?id={}", - crate::callers::endpoints::QUEUECOVERARTDATA, - resp_coverart_id - ); + let uri = format!("{}/{}", crate::callers::endpoint::QUEUECOVERARTDATA, resp_coverart_id); match app .clone() @@ -1276,6 +1272,20 @@ mod tests { .await { Ok(response) => { + match resp_to_bytes(response).await { + Ok(bytes) => { + assert_eq!(false, bytes.is_empty(), "Downloaded coverart data should not be empty"); + let temp_file = tempfile::tempdir().expect("Could not create test directory"); + let test_dir = String::from(temp_file.path().to_str().unwrap()); + let new_file = format!("{}/new_image.jpeg"); + + let mut file = std::fs::File::create(&new_file).unwrap(); + file.write_all(&bytes).unwrap(); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } let resp = get_resp_data::< crate::callers::coverart::response::fetch_coverart_with_data::Response, >(response) -- 2.47.3 From e52d78093d0fddd7fe084211f4a2c62832b91814 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 17 Jun 2025 21:18:50 -0400 Subject: [PATCH 3/6] Corrected endpoint syntax --- src/callers/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/callers/mod.rs b/src/callers/mod.rs index e15f55d..dd81a85 100644 --- a/src/callers/mod.rs +++ b/src/callers/mod.rs @@ -9,7 +9,7 @@ pub mod endpoints { pub const NEXTQUEUESONG: &str = "/api/v2/song/queue/next"; pub const QUEUEMETADATA: &str = "/api/v2/song/metadata/queue"; pub const QUEUECOVERART: &str = "/api/v2/coverart/queue"; - pub const QUEUECOVERARTDATA: &str = "/api/v2/coverart/queue/data"; + pub const QUEUECOVERARTDATA: &str = "/api/v2/coverart/queue/data/{id}"; pub const QUEUECOVERARTLINK: &str = "/api/v2/coverart/queue/link"; pub const QUEUESONGDATAWIPE: &str = "/api/v2/song/queue/data/wipe"; pub const QUEUECOVERARTDATAWIPE: &str = "/api/v2/coverart/queue/data/wipe"; -- 2.47.3 From 0a79783cc9bef31c2bab68eeab1bab8120532283 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 17 Jun 2025 21:30:29 -0400 Subject: [PATCH 4/6] Fixed test --- src/main.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5c8dd83..d3bf08d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -153,6 +153,7 @@ pub async fn root() -> &'static str { mod tests { use crate::db; + use std::io::Write; use std::usize; use common_multipart_rfc7578::client::multipart::{ @@ -1257,7 +1258,14 @@ mod tests { "Should not be empty" ); - let uri = format!("{}/{}", crate::callers::endpoint::QUEUECOVERARTDATA, resp_coverart_id); + // let uri = format!("{}/{}", crate::callers::endpoints::QUEUECOVERARTDATA, resp_coverart_id); + + let raw_uri = String::from(crate::callers::endpoints::QUEUECOVERARTDATA); + let end_index = raw_uri.len() - 5; + // let mut uri: String = (&raw_uri[..end_index]).to_string(); + // uri += &id.to_string(); + let uri = format!("{}/{}", (&raw_uri[..end_index]).to_string(), resp_coverart_id); + println!("Uri: {:?}", uri); match app .clone() @@ -1277,7 +1285,7 @@ mod tests { assert_eq!(false, bytes.is_empty(), "Downloaded coverart data should not be empty"); let temp_file = tempfile::tempdir().expect("Could not create test directory"); let test_dir = String::from(temp_file.path().to_str().unwrap()); - let new_file = format!("{}/new_image.jpeg"); + let new_file = format!("{}/new_image.jpeg", test_dir); let mut file = std::fs::File::create(&new_file).unwrap(); file.write_all(&bytes).unwrap(); @@ -1286,6 +1294,8 @@ mod tests { assert!(false, "Error: {:?}", err); } } + + /* let resp = get_resp_data::< crate::callers::coverart::response::fetch_coverart_with_data::Response, >(response) @@ -1295,6 +1305,7 @@ mod tests { resp.data.is_empty(), "Should not be empty" ); + */ } Err(err) => { assert!(false, "Error: {:?}", err); -- 2.47.3 From c22f522badb939fd22720f1e345f54a0bee622ef Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 17 Jun 2025 21:33:21 -0400 Subject: [PATCH 5/6] Some cleanup --- src/callers/coverart.rs | 38 -------------------------------------- src/main.rs | 16 ---------------- 2 files changed, 54 deletions(-) diff --git a/src/callers/coverart.rs b/src/callers/coverart.rs index 218ef49..40a185b 100644 --- a/src/callers/coverart.rs +++ b/src/callers/coverart.rs @@ -470,44 +470,6 @@ pub mod endpoint { (axum::http::StatusCode::BAD_REQUEST, axum::response::Response::default()) } } - - /* - match params.id { - Some(id) => match super::db::get_coverart_queue_data_with_id(&pool, &id).await { - Ok(cover_art_queue) => { - response.message = String::from("Successful"); - response.data.push(cover_art_queue); - (axum::http::StatusCode::OK, axum::Json(response)) - } - Err(err) => { - response.message = err.to_string(); - (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) - } - }, - _ => match params.song_queue_id { - Some(song_queue_id) => match super::db::get_coverart_queue_data_with_song_queue_id( - &pool, - &song_queue_id, - ) - .await - { - Ok(cover_art_queue) => { - response.message = String::from("Successful"); - response.data.push(cover_art_queue); - (axum::http::StatusCode::OK, axum::Json(response)) - } - Err(err) => { - response.message = err.to_string(); - (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) - } - }, - None => { - response.message = String::from("No valid id provided"); - (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) - } - }, - } - */ } pub async fn create_coverart( diff --git a/src/main.rs b/src/main.rs index d3bf08d..d9f3016 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1258,12 +1258,8 @@ mod tests { "Should not be empty" ); - // let uri = format!("{}/{}", crate::callers::endpoints::QUEUECOVERARTDATA, resp_coverart_id); - let raw_uri = String::from(crate::callers::endpoints::QUEUECOVERARTDATA); let end_index = raw_uri.len() - 5; - // let mut uri: String = (&raw_uri[..end_index]).to_string(); - // uri += &id.to_string(); let uri = format!("{}/{}", (&raw_uri[..end_index]).to_string(), resp_coverart_id); println!("Uri: {:?}", uri); @@ -1294,18 +1290,6 @@ mod tests { assert!(false, "Error: {:?}", err); } } - - /* - let resp = get_resp_data::< - crate::callers::coverart::response::fetch_coverart_with_data::Response, - >(response) - .await; - assert_eq!( - false, - resp.data.is_empty(), - "Should not be empty" - ); - */ } Err(err) => { assert!(false, "Error: {:?}", err); -- 2.47.3 From 60930ca10c5732a449a7dad70d8174ccec874af2 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 17 Jun 2025 21:33:32 -0400 Subject: [PATCH 6/6] Formatting --- src/callers/coverart.rs | 19 +++++++++++-------- src/main.rs | 42 +++++++++++++++++++++++++---------------- 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/src/callers/coverart.rs b/src/callers/coverart.rs index 40a185b..79661f1 100644 --- a/src/callers/coverart.rs +++ b/src/callers/coverart.rs @@ -450,10 +450,7 @@ pub mod endpoint { pub async fn fetch_coverart_with_data( axum::Extension(pool): axum::Extension, axum::extract::Path(id): axum::extract::Path, - ) -> ( - axum::http::StatusCode, - axum::response::Response, - ) { + ) -> (axum::http::StatusCode, axum::response::Response) { match super::db::get_coverart_queue_data_with_id(&pool, &id).await { Ok(data) => { let bytes = axum::body::Bytes::from(data); @@ -462,13 +459,19 @@ pub mod endpoint { // TODO: Address this hard coding for the coverart content type headers.insert(axum::http::header::CONTENT_TYPE, "image".parse().unwrap()); // TODO: Make the conent disposition more dynamic - headers.insert(axum::http::header::CONTENT_DISPOSITION, format!("attachment; filename=\"{}.jpg\"", id).parse().unwrap()); + headers.insert( + axum::http::header::CONTENT_DISPOSITION, + format!("attachment; filename=\"{}.jpg\"", id) + .parse() + .unwrap(), + ); (axum::http::StatusCode::OK, response) } - Err(_err) => { - (axum::http::StatusCode::BAD_REQUEST, axum::response::Response::default()) - } + Err(_err) => ( + axum::http::StatusCode::BAD_REQUEST, + axum::response::Response::default(), + ), } } diff --git a/src/main.rs b/src/main.rs index d9f3016..572dd37 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1258,9 +1258,14 @@ mod tests { "Should not be empty" ); - let raw_uri = String::from(crate::callers::endpoints::QUEUECOVERARTDATA); + let raw_uri = + String::from(crate::callers::endpoints::QUEUECOVERARTDATA); let end_index = raw_uri.len() - 5; - let uri = format!("{}/{}", (&raw_uri[..end_index]).to_string(), resp_coverart_id); + let uri = format!( + "{}/{}", + (&raw_uri[..end_index]).to_string(), + resp_coverart_id + ); println!("Uri: {:?}", uri); match app @@ -1275,22 +1280,27 @@ mod tests { ) .await { - Ok(response) => { - match resp_to_bytes(response).await { - Ok(bytes) => { - assert_eq!(false, bytes.is_empty(), "Downloaded coverart data should not be empty"); - let temp_file = tempfile::tempdir().expect("Could not create test directory"); - let test_dir = String::from(temp_file.path().to_str().unwrap()); - let new_file = format!("{}/new_image.jpeg", test_dir); + Ok(response) => match resp_to_bytes(response).await { + Ok(bytes) => { + assert_eq!( + false, + bytes.is_empty(), + "Downloaded coverart data should not be empty" + ); + let temp_file = tempfile::tempdir() + .expect("Could not create test directory"); + let test_dir = + String::from(temp_file.path().to_str().unwrap()); + let new_file = format!("{}/new_image.jpeg", test_dir); - let mut file = std::fs::File::create(&new_file).unwrap(); - file.write_all(&bytes).unwrap(); - } - Err(err) => { - assert!(false, "Error: {:?}", err); - } + let mut file = + std::fs::File::create(&new_file).unwrap(); + file.write_all(&bytes).unwrap(); } - } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + }, Err(err) => { assert!(false, "Error: {:?}", err); } -- 2.47.3