From 02d9ef013ed4e5b81d679103ab6edae0ca53377d Mon Sep 17 00:00:00 2001 From: KD Date: Sat, 25 Oct 2025 21:17:40 -0400 Subject: [PATCH] tsk-199: Separate CoverArt queue endpoints (#219) * tsk-199: Created queue module for queue endpoints * tsk-199: Code formatting * tsk-199: Test fixes * tsk-199: Version bump * tsk-199: Updated readme --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 6 +- src/callers/coverart.rs | 381 --------------------------------- src/callers/mod.rs | 1 + src/callers/queue/coverart.rs | 388 ++++++++++++++++++++++++++++++++++ src/callers/queue/mod.rs | 1 + src/main.rs | 93 ++++---- src/repo/queue/coverart.rs | 8 +- 9 files changed, 452 insertions(+), 430 deletions(-) create mode 100644 src/callers/queue/coverart.rs create mode 100644 src/callers/queue/mod.rs diff --git a/Cargo.lock b/Cargo.lock index f4c243c..aec40d7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -834,7 +834,7 @@ dependencies = [ [[package]] name = "icarus" -version = "0.3.4" +version = "0.3.5" dependencies = [ "axum", "axum-extra", diff --git a/Cargo.toml b/Cargo.toml index 12f5349..c8a826a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "icarus" -version = "0.3.4" +version = "0.3.5" edition = "2024" rust-version = "1.90" diff --git a/README.md b/README.md index 9e64c45..eda5ae1 100644 --- a/README.md +++ b/README.md @@ -3,11 +3,11 @@ Web API for the Icarus project. ### Requires -`icarus_auth` +v0.6.5 -`songparser` +v0.4.8 +`icarus_auth` v0.6.x +`songparser` v0.4.x ### Compatible with -`icarus-dm` v0.8.4 +`icarus-dm` v0.8.x ## Getting Started diff --git a/src/callers/coverart.rs b/src/callers/coverart.rs index 06ba3f1..82de0ff 100644 --- a/src/callers/coverart.rs +++ b/src/callers/coverart.rs @@ -1,49 +1,4 @@ -// TODO: Separate queue and coverart endpoints -#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] -pub struct CoverArtQueue { - pub id: uuid::Uuid, - pub file_type: String, - pub song_queue_id: uuid::Uuid, -} - pub mod request { - pub mod queue { - #[derive(utoipa::ToSchema)] - pub struct Request { - /// Filename - pub file: String, - #[schema(rename = "type")] - /// File type. Should be a file and not a value - pub file_type: String, - /// Raw data of the cover art file - pub value: Vec, - } - } - - pub mod link { - #[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] - pub struct Request { - pub coverart_id: uuid::Uuid, - pub song_queue_id: uuid::Uuid, - } - } - - pub mod fetch_coverart_no_data { - #[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] - pub struct Params { - pub id: Option, - pub song_queue_id: Option, - } - } - - pub mod fetch_coverart_with_data { - #[derive(Debug, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] - pub struct Params { - pub id: Option, - pub song_queue_id: Option, - } - } - pub mod create_coverart { #[derive(Debug, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] pub struct Request { @@ -52,13 +7,6 @@ pub mod request { } } - pub mod wipe_data_from_coverart_queue { - #[derive(Debug, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] - pub struct Request { - pub coverart_queue_id: uuid::Uuid, - } - } - pub mod get_coverart { #[derive(Debug, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] pub struct Params { @@ -68,42 +16,6 @@ pub mod request { } pub mod response { - #[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] - pub struct Response { - pub message: String, - pub data: Vec, - } - - pub mod link { - #[derive(Debug, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] - pub struct Id { - pub coverart_id: uuid::Uuid, - pub song_queue_id: uuid::Uuid, - } - - #[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] - pub struct Response { - pub message: String, - pub data: Vec, - } - } - - pub mod fetch_coverart_no_data { - #[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] - pub struct Response { - pub message: String, - pub data: Vec, - } - } - - pub mod fetch_coverart_with_data { - #[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] - pub struct Response { - pub message: String, - pub data: Vec>, - } - } - pub mod create_coverart { #[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] pub struct Response { @@ -112,14 +24,6 @@ pub mod response { } } - pub mod wipe_data_from_coverart_queue { - #[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] - pub struct Response { - pub message: String, - pub data: Vec, - } - } - pub mod get_coverart { #[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] pub struct Response { @@ -129,251 +33,12 @@ pub mod response { } } -mod helper { - pub fn is_coverart_file_type_valid(file_type: &String) -> bool { - let valid_file_types = vec![ - String::from(icarus_meta::detection::coverart::constants::JPEG_TYPE), - String::from(icarus_meta::detection::coverart::constants::JPG_TYPE), - String::from(icarus_meta::detection::coverart::constants::PNG_TYPE), - ]; - - for valid_file_type in valid_file_types { - if valid_file_type == *file_type { - return true; - } - } - - false - } -} - pub mod endpoint { use axum::response::IntoResponse; use crate::repo; use crate::repo::queue as repo_queue; - /// Endpoint to queue cover art - #[utoipa::path( - post, - path = super::super::endpoints::QUEUECOVERART, - request_body( - content = super::request::queue::Request, - ), - responses( - (status = 200, description = "Successful", body = super::response::Response), - (status = 400, description = "Error queueing cover art", body = super::response::Response) - ) - )] - 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(); - let file_type = - match icarus_meta::detection::coverart::file_type_from_data(&raw_data) { - Ok(file_type) => file_type, - Err(err) => { - eprintln!("Error: {err:?}"); - response.message = err.to_string(); - return ( - axum::http::StatusCode::INTERNAL_SERVER_ERROR, - axum::Json(response), - ); - } - }; - - if !super::helper::is_coverart_file_type_valid(&file_type.file_type) { - response.message = format!("CoverArt file type not supported: {file_type:?}"); - ( - axum::http::StatusCode::INTERNAL_SERVER_ERROR, - axum::Json(response), - ) - } else { - println!( - "Received file '{}' (name = '{}', content-type = '{}', size = {}, file-type = {:?})", - file_name, - name, - content_type, - data.len(), - file_type - ); - - match repo_queue::coverart::insert(&pool, &raw_data, &file_type.file_type).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)) - } - } - } - - /// Endpoint to link queued cover art - #[utoipa::path( - post, - path = super::super::endpoints::QUEUECOVERARTLINK, - request_body( - content = super::request::link::Request, - description = "Linking queued cover art to queued song", - content_type = "application/json" - ), - responses( - (status = 200, description = "Queued cover art linked", body = super::response::link::Response), - (status = 400, description = "Linkage failed", body = super::response::link::Response) - ) - )] - pub async fn link( - axum::Extension(pool): axum::Extension, - axum::Json(payload): axum::Json, - ) -> ( - axum::http::StatusCode, - axum::Json, - ) { - let mut response = super::response::link::Response::default(); - let id = payload.coverart_id; - let song_id = payload.song_queue_id; - - match repo_queue::coverart::update(&pool, &id, &song_id).await { - Ok(_o) => { - response.data.push(super::response::link::Id { - song_queue_id: song_id, - coverart_id: id, - }); - - (axum::http::StatusCode::OK, axum::Json(response)) - } - Err(err) => { - response.message = err.to_string(); - (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) - } - } - } - - /// Endpoint to fetch cover art details - #[utoipa::path( - get, - path = super::super::endpoints::QUEUECOVERART, - params( - ("id" = uuid::Uuid, Path, description = "Queued cover art Id"), - ("song_queue_id" = uuid::Uuid, Path, description = "Queued song Id") - ), - responses( - (status = 200, description = "Queued song linked", body = super::response::fetch_coverart_no_data::Response), - (status = 400, description = "Linkage failed", body = super::response::fetch_coverart_no_data::Response) - ) - )] - pub async fn fetch_coverart_no_data( - axum::Extension(pool): axum::Extension, - axum::extract::Query(params): axum::extract::Query< - super::request::fetch_coverart_no_data::Params, - >, - ) -> ( - axum::http::StatusCode, - axum::Json, - ) { - let mut response = super::response::fetch_coverart_no_data::Response::default(); - - match params.id { - Some(id) => match repo_queue::coverart::get_coverart_queue_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 repo_queue::coverart::get_coverart_queue_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)) - } - }, - } - } - - /// Endpoint to fetch the queued cover art data - #[utoipa::path( - get, - path = super::super::endpoints::QUEUECOVERARTDATA, - params(("id" = uuid::Uuid, Path, description = "Queued cover art Id")), - responses( - (status = 200, description = "Queued cover art data", body = Vec), - (status = 400, description = "Error fetching queued cover art data", body = Vec) - ) - )] - 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) { - match repo_queue::coverart::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=\"{id}.jpg\"") - .parse() - .unwrap(), - ); - - (axum::http::StatusCode::OK, response) - } - Err(_err) => ( - axum::http::StatusCode::BAD_REQUEST, - axum::response::Response::default(), - ), - } - } - /// Endpoint to create cover art #[utoipa::path( post, @@ -455,52 +120,6 @@ pub mod endpoint { } } - /// Endpoint to wipe data from the cover art queue - #[utoipa::path( - patch, - path = super::super::endpoints::QUEUECOVERARTDATAWIPE, - request_body( - content = super::request::wipe_data_from_coverart_queue::Request, - description = "Data required to wipe the data from the cover art queue", - content_type = "application/json" - ), - responses( - (status = 200, description = "Data wiped from cover art queue", body = super::response::wipe_data_from_coverart_queue::Response), - (status = 400, description = "Error wiping the data", body = super::response::wipe_data_from_coverart_queue::Response), - (status = 404, description = "Cover art not found", body = super::response::wipe_data_from_coverart_queue::Response) - ) - )] - pub async fn wipe_data_from_coverart_queue( - axum::Extension(pool): axum::Extension, - axum::Json(payload): axum::Json, - ) -> ( - axum::http::StatusCode, - axum::Json, - ) { - let mut response = super::response::wipe_data_from_coverart_queue::Response::default(); - let coverart_queue_id = payload.coverart_queue_id; - - match repo_queue::coverart::get_coverart_queue_with_id(&pool, &coverart_queue_id).await { - Ok(coverart_queue) => { - match repo_queue::coverart::wipe_data(&pool, &coverart_queue.id).await { - Ok(id) => { - response.message = String::from("Success"); - 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)) - } - } - } - Err(err) => { - response.message = err.to_string(); - (axum::http::StatusCode::NOT_FOUND, axum::Json(response)) - } - } - } - /// Endpoint to get cover art with criteria #[utoipa::path( get, diff --git a/src/callers/mod.rs b/src/callers/mod.rs index f19362a..df3666c 100644 --- a/src/callers/mod.rs +++ b/src/callers/mod.rs @@ -1,5 +1,6 @@ pub mod coverart; pub mod metadata; +pub mod queue; pub mod song; pub mod endpoints { diff --git a/src/callers/queue/coverart.rs b/src/callers/queue/coverart.rs new file mode 100644 index 0000000..16ed258 --- /dev/null +++ b/src/callers/queue/coverart.rs @@ -0,0 +1,388 @@ +#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] +pub struct CoverArtQueue { + pub id: uuid::Uuid, + pub file_type: String, + pub song_queue_id: uuid::Uuid, +} + +pub mod request { + pub mod queue { + #[derive(utoipa::ToSchema)] + pub struct Request { + /// Filename + pub file: String, + #[schema(rename = "type")] + /// File type. Should be a file and not a value + pub file_type: String, + /// Raw data of the cover art file + pub value: Vec, + } + } + + pub mod link { + #[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] + pub struct Request { + pub coverart_id: uuid::Uuid, + pub song_queue_id: uuid::Uuid, + } + } + + pub mod fetch_coverart_no_data { + #[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] + pub struct Params { + pub id: Option, + pub song_queue_id: Option, + } + } + + pub mod fetch_coverart_with_data { + #[derive(Debug, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] + pub struct Params { + pub id: Option, + pub song_queue_id: Option, + } + } + + pub mod wipe_data_from_coverart_queue { + #[derive(Debug, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] + pub struct Request { + pub coverart_queue_id: uuid::Uuid, + } + } +} + +pub mod response { + pub mod queue { + #[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] + pub struct Response { + pub message: String, + pub data: Vec, + } + } + + pub mod link { + #[derive(Debug, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] + pub struct Id { + pub coverart_id: uuid::Uuid, + pub song_queue_id: uuid::Uuid, + } + + #[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] + pub struct Response { + pub message: String, + pub data: Vec, + } + } + + pub mod fetch_coverart_no_data { + #[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] + pub struct Response { + pub message: String, + pub data: Vec, + } + } + + pub mod fetch_coverart_with_data { + #[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] + pub struct Response { + pub message: String, + pub data: Vec>, + } + } + + pub mod wipe_data_from_coverart_queue { + #[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)] + pub struct Response { + pub message: String, + pub data: Vec, + } + } +} + +mod helper { + pub fn is_coverart_file_type_valid(file_type: &String) -> bool { + let valid_file_types = vec![ + String::from(icarus_meta::detection::coverart::constants::JPEG_TYPE), + String::from(icarus_meta::detection::coverart::constants::JPG_TYPE), + String::from(icarus_meta::detection::coverart::constants::PNG_TYPE), + ]; + + for valid_file_type in valid_file_types { + if valid_file_type == *file_type { + return true; + } + } + + false + } +} + +pub mod endpoint { + use axum::response::IntoResponse; + + use crate::repo::queue as repo; + + /// Endpoint to queue cover art + #[utoipa::path( + post, + path = super::super::super::endpoints::QUEUECOVERART, + request_body( + content = super::request::queue::Request, + ), + responses( + (status = 200, description = "Successful", body = super::response::queue::Response), + (status = 400, description = "Error queueing cover art", body = super::response::queue::Response) + ) + )] + pub async fn queue( + axum::Extension(pool): axum::Extension, + mut multipart: axum::extract::Multipart, + ) -> ( + axum::http::StatusCode, + axum::Json, + ) { + let mut response = super::response::queue::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(); + let file_type = + match icarus_meta::detection::coverart::file_type_from_data(&raw_data) { + Ok(file_type) => file_type, + Err(err) => { + eprintln!("Error: {err:?}"); + response.message = err.to_string(); + return ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response), + ); + } + }; + + if !super::helper::is_coverart_file_type_valid(&file_type.file_type) { + response.message = format!("CoverArt file type not supported: {file_type:?}"); + ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response), + ) + } else { + println!( + "Received file '{}' (name = '{}', content-type = '{}', size = {}, file-type = {:?})", + file_name, + name, + content_type, + data.len(), + file_type + ); + + match repo::coverart::insert(&pool, &raw_data, &file_type.file_type).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)) + } + } + } + + /// Endpoint to link queued cover art + #[utoipa::path( + post, + path = super::super::super::endpoints::QUEUECOVERARTLINK, + request_body( + content = super::request::link::Request, + description = "Linking queued cover art to queued song", + content_type = "application/json" + ), + responses( + (status = 200, description = "Queued cover art linked", body = super::response::link::Response), + (status = 400, description = "Linkage failed", body = super::response::link::Response) + ) + )] + pub async fn link( + axum::Extension(pool): axum::Extension, + axum::Json(payload): axum::Json, + ) -> ( + axum::http::StatusCode, + axum::Json, + ) { + let mut response = super::response::link::Response::default(); + let id = payload.coverart_id; + let song_id = payload.song_queue_id; + + match repo::coverart::update(&pool, &id, &song_id).await { + Ok(_o) => { + response.data.push(super::response::link::Id { + song_queue_id: song_id, + coverart_id: id, + }); + + (axum::http::StatusCode::OK, axum::Json(response)) + } + Err(err) => { + response.message = err.to_string(); + (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) + } + } + } + + /// Endpoint to fetch cover art details + #[utoipa::path( + get, + path = super::super::super::endpoints::QUEUECOVERART, + params( + ("id" = uuid::Uuid, Path, description = "Queued cover art Id"), + ("song_queue_id" = uuid::Uuid, Path, description = "Queued song Id") + ), + responses( + (status = 200, description = "Queued song linked", body = super::response::fetch_coverart_no_data::Response), + (status = 400, description = "Linkage failed", body = super::response::fetch_coverart_no_data::Response) + ) + )] + pub async fn fetch_coverart_no_data( + axum::Extension(pool): axum::Extension, + axum::extract::Query(params): axum::extract::Query< + super::request::fetch_coverart_no_data::Params, + >, + ) -> ( + axum::http::StatusCode, + axum::Json, + ) { + let mut response = super::response::fetch_coverart_no_data::Response::default(); + + match params.id { + Some(id) => match repo::coverart::get_coverart_queue_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 repo::coverart::get_coverart_queue_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)) + } + }, + } + } + + /// Endpoint to fetch the queued cover art data + #[utoipa::path( + get, + path = super::super::super::endpoints::QUEUECOVERARTDATA, + params(("id" = uuid::Uuid, Path, description = "Queued cover art Id")), + responses( + (status = 200, description = "Queued cover art data", body = Vec), + (status = 400, description = "Error fetching queued cover art data", body = Vec) + ) + )] + 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) { + match repo::coverart::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=\"{id}.jpg\"") + .parse() + .unwrap(), + ); + + (axum::http::StatusCode::OK, response) + } + Err(_err) => ( + axum::http::StatusCode::BAD_REQUEST, + axum::response::Response::default(), + ), + } + } + + /// Endpoint to wipe data from the cover art queue + #[utoipa::path( + patch, + path = super::super::super::endpoints::QUEUECOVERARTDATAWIPE, + request_body( + content = super::request::wipe_data_from_coverart_queue::Request, + description = "Data required to wipe the data from the cover art queue", + content_type = "application/json" + ), + responses( + (status = 200, description = "Data wiped from cover art queue", body = super::response::wipe_data_from_coverart_queue::Response), + (status = 400, description = "Error wiping the data", body = super::response::wipe_data_from_coverart_queue::Response), + (status = 404, description = "Cover art not found", body = super::response::wipe_data_from_coverart_queue::Response) + ) + )] + pub async fn wipe_data_from_coverart_queue( + axum::Extension(pool): axum::Extension, + axum::Json(payload): axum::Json, + ) -> ( + axum::http::StatusCode, + axum::Json, + ) { + let mut response = super::response::wipe_data_from_coverart_queue::Response::default(); + let coverart_queue_id = payload.coverart_queue_id; + + match repo::coverart::get_coverart_queue_with_id(&pool, &coverart_queue_id).await { + Ok(coverart_queue) => { + match repo::coverart::wipe_data(&pool, &coverart_queue.id).await { + Ok(id) => { + response.message = String::from("Success"); + 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)) + } + } + } + Err(err) => { + response.message = err.to_string(); + (axum::http::StatusCode::NOT_FOUND, axum::Json(response)) + } + } + } +} diff --git a/src/callers/queue/mod.rs b/src/callers/queue/mod.rs new file mode 100644 index 0000000..165fc86 --- /dev/null +++ b/src/callers/queue/mod.rs @@ -0,0 +1 @@ +pub mod coverart; diff --git a/src/main.rs b/src/main.rs index fdcd5ee..18a10f9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -54,9 +54,12 @@ pub mod init { use crate::callers::coverart as coverart_caller; use crate::callers::metadata as metadata_caller; + use crate::callers::queue::coverart as coverart_queue_callers; use crate::callers::song as song_caller; use coverart_caller::endpoint as coverart_endpoints; use coverart_caller::response as coverart_responses; + use coverart_queue_callers::endpoint as coverart_queue_endpoints; + use coverart_queue_callers::response as coverart_queue_responses; use metadata_caller::endpoint as metadata_endpoints; use metadata_caller::response as metadata_responses; use song_caller::endpoint as song_endpoints; @@ -114,16 +117,16 @@ pub mod init { #[openapi( paths(song_endpoints::queue_song, song_endpoints::link_user_id, song_endpoints::fetch_queue_song, song_endpoints::download_flac, song_endpoints::update_song_queue_status, song_endpoints::update_song_queue, song_endpoints::create_metadata, song_endpoints::wipe_data_from_song_queue, song_endpoints::get_songs, song_endpoints::get_all_songs, song_endpoints::stream_song, song_endpoints::download_song, - song_endpoints::delete_song, coverart_endpoints::queue, coverart_endpoints::link, coverart_endpoints::fetch_coverart_no_data, - coverart_endpoints::fetch_coverart_with_data, coverart_endpoints::create_coverart, coverart_endpoints::wipe_data_from_coverart_queue, + song_endpoints::delete_song, coverart_queue_endpoints::queue, coverart_queue_endpoints::link, coverart_queue_endpoints::fetch_coverart_no_data, + coverart_queue_endpoints::fetch_coverart_with_data, coverart_endpoints::create_coverart, coverart_queue_endpoints::wipe_data_from_coverart_queue, coverart_endpoints::get_coverart, coverart_endpoints::download_coverart, metadata_endpoints::queue_metadata, metadata_endpoints::fetch_metadata), components(schemas(song_responses::Response, song_responses::link_user_id::Response, song_responses::fetch_queue_song::Response, song_responses::update_status::Response, song_responses::update_song_queue::Response, song_responses::create_metadata::Response, song_responses::wipe_data_from_song_queue::Response, song_responses::get_songs::Response, song_responses::delete_song::Response, - coverart_responses::Response, coverart_responses::link::Response, coverart_responses::fetch_coverart_no_data::Response, - coverart_responses::fetch_coverart_with_data::Response, coverart_responses::create_coverart::Response, - coverart_responses::wipe_data_from_coverart_queue::Response, coverart_responses::get_coverart::Response, + coverart_queue_responses::queue::Response, coverart_queue_responses::link::Response, coverart_queue_responses::fetch_coverart_no_data::Response, + coverart_queue_responses::fetch_coverart_with_data::Response, coverart_responses::create_coverart::Response, + coverart_queue_responses::wipe_data_from_coverart_queue::Response, coverart_responses::get_coverart::Response, metadata_responses::queue_metadata::Response, metadata_responses::fetch_metadata::Response)), tags( (name = "Icarus API", description = "Web API to manage music") @@ -190,31 +193,32 @@ pub mod init { ) .route( crate::callers::endpoints::QUEUECOVERART, - post(crate::callers::coverart::endpoint::queue).route_layer( + post(crate::callers::queue::coverart::endpoint::queue).route_layer( axum::middleware::from_fn(crate::auth::auth::), ), ) .route( crate::callers::endpoints::QUEUECOVERARTDATA, - get(crate::callers::coverart::endpoint::fetch_coverart_with_data).route_layer( - axum::middleware::from_fn(crate::auth::auth::), - ), + get(crate::callers::queue::coverart::endpoint::fetch_coverart_with_data) + .route_layer(axum::middleware::from_fn( + crate::auth::auth::, + )), ) .route( crate::callers::endpoints::QUEUECOVERART, - get(crate::callers::coverart::endpoint::fetch_coverart_no_data).route_layer( + get(crate::callers::queue::coverart::endpoint::fetch_coverart_no_data).route_layer( axum::middleware::from_fn(crate::auth::auth::), ), ) .route( crate::callers::endpoints::QUEUECOVERARTLINK, - patch(crate::callers::coverart::endpoint::link).route_layer( + patch(crate::callers::queue::coverart::endpoint::link).route_layer( axum::middleware::from_fn(crate::auth::auth::), ), ) .route( crate::callers::endpoints::QUEUECOVERARTDATAWIPE, - patch(crate::callers::coverart::endpoint::wipe_data_from_coverart_queue) + patch(crate::callers::queue::coverart::endpoint::wipe_data_from_coverart_queue) .route_layer(axum::middleware::from_fn( crate::auth::auth::, )), @@ -743,11 +747,10 @@ mod tests { ) -> Result { match super::upload_coverart_queue_req(&app).await { Ok(response) => { - let resp = - super::get_resp_data::( - response, - ) - .await; + let resp = super::get_resp_data::< + crate::callers::queue::coverart::response::queue::Response, + >(response) + .await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); let coverart_id = resp.data[0]; assert_eq!(false, coverart_id.is_nil(), "Should not be empty"); @@ -761,7 +764,7 @@ mod tests { { Ok(response) => { let resp = super::get_resp_data::< - crate::callers::coverart::response::link::Response, + crate::callers::queue::coverart::response::link::Response, >(response) .await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); @@ -1023,7 +1026,7 @@ mod tests { match sequence_flow::queue_song_and_coverart_flow(&app).await { Ok((resp_one, song_queue_id)) => { let resp = get_resp_data::< - crate::callers::coverart::response::fetch_coverart_no_data::Response, + crate::callers::queue::coverart::response::fetch_coverart_no_data::Response, >(resp_one) .await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); @@ -1256,7 +1259,7 @@ mod tests { match sequence_flow::queue_song_and_coverart_flow(&app).await { Ok((resp_one, song_queue_id)) => { let resp = get_resp_data::< - crate::callers::coverart::response::fetch_coverart_no_data::Response, + crate::callers::queue::coverart::response::fetch_coverart_no_data::Response, >(resp_one) .await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); @@ -1396,8 +1399,10 @@ mod tests { // Send request match upload_coverart_queue_req(&app).await { Ok(response) => { - let resp = - get_resp_data::(response).await; + let resp = get_resp_data::< + crate::callers::queue::coverart::response::queue::Response, + >(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"); @@ -1431,8 +1436,10 @@ mod tests { match song_queue_req(&app).await { Ok(response) => { - let resp = - get_resp_data::(response).await; + let resp = get_resp_data::< + crate::callers::queue::coverart::response::queue::Response, + >(response) + .await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); let song_queue_id = resp.data[0]; assert_eq!(false, song_queue_id.is_nil(), "Should not be empty"); @@ -1440,9 +1447,10 @@ mod tests { // Send request match upload_coverart_queue_req(&app).await { Ok(response) => { - let resp = - get_resp_data::(response) - .await; + let resp = get_resp_data::< + crate::callers::queue::coverart::response::queue::Response, + >(response) + .await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); let coverart_id = resp.data[0]; assert_eq!(false, coverart_id.is_nil(), "Should not be empty"); @@ -1452,7 +1460,7 @@ mod tests { { Ok(response) => { let resp = get_resp_data::< - crate::callers::coverart::response::link::Response, + crate::callers::queue::coverart::response::link::Response, >(response) .await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); @@ -1505,8 +1513,10 @@ mod tests { match song_queue_req(&app).await { Ok(response) => { - let resp = - get_resp_data::(response).await; + let resp = get_resp_data::< + crate::callers::queue::coverart::response::queue::Response, + >(response) + .await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); let song_queue_id = resp.data[0]; assert_eq!(false, song_queue_id.is_nil(), "Should not be empty"); @@ -1514,7 +1524,7 @@ mod tests { match sequence_flow::queue_coverart_flow(&app, &song_queue_id).await { Ok(response) => { let resp = get_resp_data::< - crate::callers::coverart::response::fetch_coverart_no_data::Response, + crate::callers::queue::coverart::response::fetch_coverart_no_data::Response, >(response) .await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); @@ -1553,8 +1563,10 @@ mod tests { match song_queue_req(&app).await { Ok(response) => { - let resp = - get_resp_data::(response).await; + let resp = get_resp_data::< + crate::callers::queue::coverart::response::queue::Response, + >(response) + .await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); let song_queue_id = resp.data[0]; assert_eq!(false, song_queue_id.is_nil(), "Should not be empty"); @@ -1562,9 +1574,10 @@ mod tests { // Send request match upload_coverart_queue_req(&app).await { Ok(response) => { - let resp = - get_resp_data::(response) - .await; + let resp = get_resp_data::< + crate::callers::queue::coverart::response::queue::Response, + >(response) + .await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); let coverart_id = resp.data[0]; assert_eq!(false, coverart_id.is_nil(), "Should not be empty"); @@ -1574,7 +1587,7 @@ mod tests { { Ok(response) => { let resp = get_resp_data::< - crate::callers::coverart::response::link::Response, + crate::callers::queue::coverart::response::link::Response, >(response) .await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); @@ -1774,7 +1787,7 @@ mod tests { match sequence_flow::queue_coverart_flow(&app, &song_queue_id).await { Ok(response) => { let resp = get_resp_data::< - crate::callers::coverart::response::fetch_coverart_no_data::Response, + crate::callers::queue::coverart::response::fetch_coverart_no_data::Response, >(response) .await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); @@ -1975,7 +1988,7 @@ mod tests { match sequence_flow::queue_coverart_flow(&app, &song_queue_id).await { Ok(response) => { let resp = get_resp_data::< - crate::callers::coverart::response::fetch_coverart_no_data::Response, + crate::callers::queue::coverart::response::fetch_coverart_no_data::Response, >(response) .await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); @@ -2007,7 +2020,7 @@ mod tests { { Ok(response) => { let resp = get_resp_data::< - crate::callers::coverart::response::wipe_data_from_coverart_queue::Response, + crate::callers::queue::coverart::response::wipe_data_from_coverart_queue::Response, >(response) .await; assert_eq!( diff --git a/src/repo/queue/coverart.rs b/src/repo/queue/coverart.rs index ed1349b..d224562 100644 --- a/src/repo/queue/coverart.rs +++ b/src/repo/queue/coverart.rs @@ -54,7 +54,7 @@ pub async fn update( pub async fn get_coverart_queue_with_id( pool: &sqlx::PgPool, id: &uuid::Uuid, -) -> Result { +) -> Result { let result = sqlx::query( r#" SELECT id, file_type, song_queue_id FROM "coverartQueue" WHERE id = $1; @@ -68,7 +68,7 @@ pub async fn get_coverart_queue_with_id( }); match result { - Ok(row) => Ok(crate::callers::coverart::CoverArtQueue { + Ok(row) => Ok(crate::callers::queue::coverart::CoverArtQueue { id: row .try_get("id") .map_err(|_e| sqlx::Error::RowNotFound) @@ -89,7 +89,7 @@ pub async fn get_coverart_queue_with_id( pub async fn get_coverart_queue_with_song_queue_id( pool: &sqlx::PgPool, song_queue_id: &uuid::Uuid, -) -> Result { +) -> Result { let result = sqlx::query( r#" SELECT id, file_type, song_queue_id FROM "coverartQueue" WHERE song_queue_id = $1; @@ -103,7 +103,7 @@ pub async fn get_coverart_queue_with_song_queue_id( }); match result { - Ok(row) => Ok(crate::callers::coverart::CoverArtQueue { + Ok(row) => Ok(crate::callers::queue::coverart::CoverArtQueue { id: row .try_get("id") .map_err(|_e| sqlx::Error::RowNotFound)