From ddb0bf27f848849780dde9a212533c8ae3a7359e Mon Sep 17 00:00:00 2001 From: KD Date: Sun, 18 May 2025 18:22:39 -0400 Subject: [PATCH] Queue coverart (#127) * Added new module * Added new endpoint * Formatting * Migration changes * Fix routing issue * Fixed migration * Added functionality and test * Test change * Cleanup * Code formatting --- migrations/20250420185217_init_migration.sql | 7 ++ src/callers/coverart.rs | 83 ++++++++++++++++++++ src/callers/mod.rs | 2 + src/main.rs | 57 ++++++++++++++ 4 files changed, 149 insertions(+) create mode 100644 src/callers/coverart.rs diff --git a/migrations/20250420185217_init_migration.sql b/migrations/20250420185217_init_migration.sql index fd94f5f..aca722f 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 NULL +); + -- Create an index for better query performance CREATE INDEX metadata_queue_data_metadata ON "metadataQueue" USING gin (metadata); diff --git a/src/callers/coverart.rs b/src/callers/coverart.rs new file mode 100644 index 0000000..15de43a --- /dev/null +++ b/src/callers/coverart.rs @@ -0,0 +1,83 @@ +pub mod response { + #[derive(Debug, Default, serde::Deserialize, serde::Serialize)] + pub struct Response { + 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), + } + } +} + +pub mod endpoint { + pub async fn queue( + axum::Extension(pool): axum::Extension, + mut multipart: axum::extract::Multipart, + ) -> ( + axum::http::StatusCode, + axum::Json, + ) { + let mut response = super::response::Response::default(); + + 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 = {})", + 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)), + Err(err) => { + response.message = err.to_string(); + (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) + } + } + } +} 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..d7ff00c 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::QUEUECOVERART, + post(crate::callers::coverart::endpoint::queue), + ) } pub async fn app() -> axum::Router { @@ -591,4 +595,57 @@ 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"); + let id = resp.data[0]; + assert_eq!(false, id.is_nil(), "Should not be empty"); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + }; + + let _ = db_mgr::drop_database(&tm_pool, &db_name).await; + } }