From 3f22a4f6b0116bd2c6c7c853392bc71cf18f17dd Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Wed, 30 Apr 2025 21:41:10 -0400 Subject: [PATCH 01/10] Added new module --- src/callers/coverart.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/callers/coverart.rs diff --git a/src/callers/coverart.rs b/src/callers/coverart.rs new file mode 100644 index 0000000..a123adc --- /dev/null +++ b/src/callers/coverart.rs @@ -0,0 +1,40 @@ + +pub mod response { + #[derive(Debug, Default, serde::Deserialize, serde::Serialize)] + pub struct Response { + pub rando: i32, + } +} + + +pub mod endpoint { + // TODO: Implement this + pub async fn queue( + axum::Extension(pool): axum::Extension, + mut multipart: axum::extract::Multipart, + ) -> (axum::http::StatusCode, axum::Json) { + + let response = super::response::Response::default(); + + while let Some(field) = multipart.next_field().await.unwrap() { + let name = field.name().unwrap().to_string(); + let file_name = field.file_name().unwrap().to_string(); + let content_type = field.content_type().unwrap().to_string(); + let data = field.bytes().await.unwrap(); + + println!( + "Received file '{}' (name = '{}', content-type = '{}', size = {})", + file_name, + name, + content_type, + data.len() + ); + + // Save the file to disk + // let mut file = std::fs::File::create(&file_name).unwrap(); + // file.write_all(&data).unwrap(); + } + + (axum::http::StatusCode::OK, axum::Json(response)) + } +} -- 2.47.3 From db1f0358670d15ccce1490164c8d311df589e53c Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Wed, 30 Apr 2025 21:42:20 -0400 Subject: [PATCH 02/10] Added new endpoint --- src/callers/mod.rs | 2 ++ src/main.rs | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/src/callers/mod.rs b/src/callers/mod.rs index aac314c..55d2907 100644 --- a/src/callers/mod.rs +++ b/src/callers/mod.rs @@ -1,3 +1,4 @@ +pub mod coverart; pub mod metadata; pub mod song; @@ -6,4 +7,5 @@ pub mod endpoints { pub const QUEUESONGDATA: &str = "/api/v2/song/queue/{id}"; 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"; } diff --git a/src/main.rs b/src/main.rs index 560d5fc..7508658 100644 --- a/src/main.rs +++ b/src/main.rs @@ -85,6 +85,10 @@ pub mod init { crate::callers::endpoints::QUEUEMETADATA, get(crate::callers::metadata::endpoint::fetch_metadata), ) + .route( + crate::callers::endpoints::QUEUEMETADATA, + post(crate::callers::coverart::endpoint::queue), + ) } pub async fn app() -> axum::Router { -- 2.47.3 From 031b1b611ac6e1b5292624e1f50a37c96f10c02f Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Wed, 30 Apr 2025 21:42:33 -0400 Subject: [PATCH 03/10] Formatting --- src/callers/coverart.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/callers/coverart.rs b/src/callers/coverart.rs index a123adc..d70af9b 100644 --- a/src/callers/coverart.rs +++ b/src/callers/coverart.rs @@ -1,4 +1,3 @@ - pub mod response { #[derive(Debug, Default, serde::Deserialize, serde::Serialize)] pub struct Response { @@ -6,14 +5,15 @@ pub mod response { } } - pub mod endpoint { // TODO: Implement this pub async fn queue( axum::Extension(pool): axum::Extension, mut multipart: axum::extract::Multipart, - ) -> (axum::http::StatusCode, axum::Json) { - + ) -> ( + axum::http::StatusCode, + axum::Json, + ) { let response = super::response::Response::default(); while let Some(field) = multipart.next_field().await.unwrap() { -- 2.47.3 From ecc200517840055f9409e7dc85e9878bec61bee0 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Thu, 1 May 2025 21:17:06 -0400 Subject: [PATCH 04/10] Migration changes --- migrations/20250420185217_init_migration.sql | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/migrations/20250420185217_init_migration.sql b/migrations/20250420185217_init_migration.sql index fd94f5f..a7d5d52 100644 --- a/migrations/20250420185217_init_migration.sql +++ b/migrations/20250420185217_init_migration.sql @@ -17,5 +17,12 @@ CREATE TABLE IF NOT EXISTS "metadataQueue" ( song_queue_id UUID NOT NULL ); +-- Table to store queued coverart +CREATE TABLE IF NOT EXISTS "coverartQueue" { + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), + data BYTEA NOT NULL, + song_queue_id UUID NOT NULL +}; + -- Create an index for better query performance CREATE INDEX metadata_queue_data_metadata ON "metadataQueue" USING gin (metadata); -- 2.47.3 From 7e42a6a44e40a8c391caec1947f94615505df1ab Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Thu, 1 May 2025 21:17:15 -0400 Subject: [PATCH 05/10] Fix routing issue --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 7508658..9a70e89 100644 --- a/src/main.rs +++ b/src/main.rs @@ -86,7 +86,7 @@ pub mod init { get(crate::callers::metadata::endpoint::fetch_metadata), ) .route( - crate::callers::endpoints::QUEUEMETADATA, + crate::callers::endpoints::QUEUECOVERART, post(crate::callers::coverart::endpoint::queue), ) } -- 2.47.3 From e25870be1c588d6594943f976cfe0c6a55d483e2 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Thu, 1 May 2025 21:54:11 -0400 Subject: [PATCH 06/10] Fixed migration --- migrations/20250420185217_init_migration.sql | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/migrations/20250420185217_init_migration.sql b/migrations/20250420185217_init_migration.sql index a7d5d52..aca722f 100644 --- a/migrations/20250420185217_init_migration.sql +++ b/migrations/20250420185217_init_migration.sql @@ -18,11 +18,11 @@ CREATE TABLE IF NOT EXISTS "metadataQueue" ( ); -- Table to store queued coverart -CREATE TABLE IF NOT EXISTS "coverartQueue" { +CREATE TABLE IF NOT EXISTS "coverartQueue" ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), data BYTEA NOT NULL, - song_queue_id UUID NOT NULL -}; + song_queue_id UUID NULL +); -- Create an index for better query performance CREATE INDEX metadata_queue_data_metadata ON "metadataQueue" USING gin (metadata); -- 2.47.3 From ca525dcab3117c8f4d762dca6bc0395e44e96000 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Thu, 1 May 2025 21:54:28 -0400 Subject: [PATCH 07/10] Added functionality and test --- src/callers/coverart.rs | 64 +++++++++++++++++++++++++++++++++++++---- src/main.rs | 48 +++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 5 deletions(-) diff --git a/src/callers/coverart.rs b/src/callers/coverart.rs index d70af9b..937be63 100644 --- a/src/callers/coverart.rs +++ b/src/callers/coverart.rs @@ -1,7 +1,40 @@ pub mod response { #[derive(Debug, Default, serde::Deserialize, serde::Serialize)] pub struct Response { - pub rando: i32, + pub message: String, + pub data: Vec, + } +} + +mod db { + use sqlx::Row; + + pub async fn insert( + pool: &sqlx::PgPool, + data: &Vec, + ) -> Result { + let result = sqlx::query( + r#" + INSERT INTO "coverartQueue" (data) VALUES($1) RETURNING id; + "#, + ) + .bind(data) + .fetch_one(pool) + .await + .map_err(|e| { + eprintln!("Error inserting: {}", e); + }); + + match result { + Ok(row) => { + let id: uuid::Uuid = row + .try_get("id") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(); + Ok(id) + } + Err(_err) => Err(sqlx::Error::RowNotFound), + } } } @@ -14,13 +47,16 @@ pub mod endpoint { axum::http::StatusCode, axum::Json, ) { - let response = super::response::Response::default(); + let mut response = super::response::Response::default(); - while let Some(field) = multipart.next_field().await.unwrap() { + // while let Some(field) = multipart.next_field().await.unwrap() { + match multipart.next_field().await { + Ok(Some(field)) => { let name = field.name().unwrap().to_string(); let file_name = field.file_name().unwrap().to_string(); let content_type = field.content_type().unwrap().to_string(); let data = field.bytes().await.unwrap(); + let raw_data = data.to_vec(); println!( "Received file '{}' (name = '{}', content-type = '{}', size = {})", @@ -30,11 +66,29 @@ pub mod endpoint { data.len() ); + match super::db::insert(&pool, &raw_data).await { + Ok(id) => { + response.message = String::from("Successful"); + response.data.push(id); + (axum::http::StatusCode::OK, axum::Json(response)) + } + Err(err) => { + response.message = err.to_string(); + (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) + } + } + // Save the file to disk // let mut file = std::fs::File::create(&file_name).unwrap(); // file.write_all(&data).unwrap(); + } + Ok(None) => { + (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) + } + Err(err) => { + response.message = err.to_string(); + (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) + } } - - (axum::http::StatusCode::OK, axum::Json(response)) } } diff --git a/src/main.rs b/src/main.rs index 9a70e89..3ffe04e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -595,4 +595,52 @@ mod tests { let _ = db_mgr::drop_database(&tm_pool, &db_name).await; } + + #[tokio::test] + async fn test_song_coverart_queue() { + let tm_pool = db_mgr::get_pool().await.unwrap(); + let db_name = db_mgr::generate_db_name().await; + + match db_mgr::create_database(&tm_pool, &db_name).await { + Ok(_) => { + println!("Success"); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + + let pool = db_mgr::connect_to_db(&db_name).await.unwrap(); + db::migrations(&pool).await; + + let app = init::app(pool).await; + let mut form = MultipartForm::default(); + let _ = form.add_file("jpg", "tests/Machine_gun/160809_machinegun.jpg"); + + // Create request + let content_type = form.content_type(); + let body = MultipartBody::from(form); + + // Send request + match app.clone().oneshot( + axum::http::Request::builder() + .method(axum::http::Method::POST) + .uri(crate::callers::endpoints::QUEUECOVERART) + .header(axum::http::header::CONTENT_TYPE, content_type) + .body(axum::body::Body::from_stream(body)) + .unwrap() + ).await { + Ok(response) => { + let resp = + get_resp_data::(response).await; + assert_eq!(false, resp.data.is_empty(), "Should not be empty"); + assert_eq!(false, resp.data[0].is_nil(), "Should not be empty"); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + }; + + let _ = db_mgr::drop_database(&tm_pool, &db_name).await; + } } -- 2.47.3 From a222427ee38e755ee26051a74dd0cc656eb23bd4 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Thu, 1 May 2025 21:56:15 -0400 Subject: [PATCH 08/10] Test change --- src/main.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 3ffe04e..a83d1e5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -634,7 +634,8 @@ mod tests { let resp = get_resp_data::(response).await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); - assert_eq!(false, resp.data[0].is_nil(), "Should not be empty"); + let id = resp.data[0]; + assert_eq!(false, id.is_nil(), "Should not be empty"); } Err(err) => { assert!(false, "Error: {:?}", err); -- 2.47.3 From 33b19dc4c69152acca47e486b36d20c267b34f05 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Thu, 1 May 2025 21:57:12 -0400 Subject: [PATCH 09/10] Cleanup --- src/callers/coverart.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/callers/coverart.rs b/src/callers/coverart.rs index 937be63..083bbd7 100644 --- a/src/callers/coverart.rs +++ b/src/callers/coverart.rs @@ -39,7 +39,6 @@ mod db { } pub mod endpoint { - // TODO: Implement this pub async fn queue( axum::Extension(pool): axum::Extension, mut multipart: axum::extract::Multipart, @@ -49,7 +48,6 @@ pub mod endpoint { ) { let mut response = super::response::Response::default(); - // while let Some(field) = multipart.next_field().await.unwrap() { match multipart.next_field().await { Ok(Some(field)) => { let name = field.name().unwrap().to_string(); @@ -77,10 +75,6 @@ pub mod endpoint { (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) } } - - // Save the file to disk - // let mut file = std::fs::File::create(&file_name).unwrap(); - // file.write_all(&data).unwrap(); } Ok(None) => { (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) -- 2.47.3 From 0c58a6e8375ae9f2bc14895e00334bd4b51a6c61 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Thu, 1 May 2025 21:57:27 -0400 Subject: [PATCH 10/10] Code formatting --- src/callers/coverart.rs | 59 +++++++++++++++++++---------------------- src/main.rs | 20 ++++++++------ 2 files changed, 39 insertions(+), 40 deletions(-) diff --git a/src/callers/coverart.rs b/src/callers/coverart.rs index 083bbd7..15de43a 100644 --- a/src/callers/coverart.rs +++ b/src/callers/coverart.rs @@ -9,10 +9,7 @@ pub mod response { mod db { use sqlx::Row; - pub async fn insert( - pool: &sqlx::PgPool, - data: &Vec, - ) -> Result { + pub async fn insert(pool: &sqlx::PgPool, data: &Vec) -> Result { let result = sqlx::query( r#" INSERT INTO "coverartQueue" (data) VALUES($1) RETURNING id; @@ -50,38 +47,36 @@ pub mod endpoint { match multipart.next_field().await { Ok(Some(field)) => { - let name = field.name().unwrap().to_string(); - let file_name = field.file_name().unwrap().to_string(); - let content_type = field.content_type().unwrap().to_string(); - let data = field.bytes().await.unwrap(); - let raw_data = data.to_vec(); + let name = field.name().unwrap().to_string(); + let file_name = field.file_name().unwrap().to_string(); + let content_type = field.content_type().unwrap().to_string(); + let data = field.bytes().await.unwrap(); + let raw_data = data.to_vec(); - println!( - "Received file '{}' (name = '{}', content-type = '{}', size = {})", - file_name, - name, - content_type, - data.len() - ); + println!( + "Received file '{}' (name = '{}', content-type = '{}', size = {})", + file_name, + name, + content_type, + data.len() + ); - match super::db::insert(&pool, &raw_data).await { - Ok(id) => { - response.message = String::from("Successful"); - response.data.push(id); - (axum::http::StatusCode::OK, axum::Json(response)) - } - Err(err) => { - response.message = err.to_string(); - (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) - } - } - } - Ok(None) => { - (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) + match super::db::insert(&pool, &raw_data).await { + Ok(id) => { + response.message = String::from("Successful"); + response.data.push(id); + (axum::http::StatusCode::OK, axum::Json(response)) + } + Err(err) => { + response.message = err.to_string(); + (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) + } + } } + Ok(None) => (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)), Err(err) => { - response.message = err.to_string(); - (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) + response.message = err.to_string(); + (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) } } } diff --git a/src/main.rs b/src/main.rs index a83d1e5..d7ff00c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -622,14 +622,18 @@ mod tests { let body = MultipartBody::from(form); // Send request - match app.clone().oneshot( - axum::http::Request::builder() - .method(axum::http::Method::POST) - .uri(crate::callers::endpoints::QUEUECOVERART) - .header(axum::http::header::CONTENT_TYPE, content_type) - .body(axum::body::Body::from_stream(body)) - .unwrap() - ).await { + match app + .clone() + .oneshot( + axum::http::Request::builder() + .method(axum::http::Method::POST) + .uri(crate::callers::endpoints::QUEUECOVERART) + .header(axum::http::header::CONTENT_TYPE, content_type) + .body(axum::body::Body::from_stream(body)) + .unwrap(), + ) + .await + { Ok(response) => { let resp = get_resp_data::(response).await; -- 2.47.3