Fixed endpoint for getting coverart queue data #143

Merged
kdeng00 merged 6 commits from coverart_queue_data_endpoint_fix into v0.2 2025-06-17 21:40:49 -04:00
3 changed files with 53 additions and 56 deletions
+24 -42
View File
@@ -326,6 +326,8 @@ pub mod cov_db {
pub mod endpoint { pub mod endpoint {
use std::io::Write; use std::io::Write;
use axum::response::IntoResponse;
pub async fn queue( pub async fn queue(
axum::Extension(pool): axum::Extension<sqlx::PgPool>, axum::Extension(pool): axum::Extension<sqlx::PgPool>,
mut multipart: axum::extract::Multipart, mut multipart: axum::extract::Multipart,
@@ -447,49 +449,29 @@ pub mod endpoint {
pub async fn fetch_coverart_with_data( pub async fn fetch_coverart_with_data(
axum::Extension(pool): axum::Extension<sqlx::PgPool>, axum::Extension(pool): axum::Extension<sqlx::PgPool>,
axum::extract::Query(params): axum::extract::Query< axum::extract::Path(id): axum::extract::Path<uuid::Uuid>,
super::request::fetch_coverart_with_data::Params, ) -> (axum::http::StatusCode, axum::response::Response) {
>, match super::db::get_coverart_queue_data_with_id(&pool, &id).await {
) -> ( Ok(data) => {
axum::http::StatusCode, let bytes = axum::body::Bytes::from(data);
axum::Json<super::response::fetch_coverart_with_data::Response>, let mut response = bytes.into_response();
) { let headers = response.headers_mut();
let mut response = super::response::fetch_coverart_with_data::Response::default(); // 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(),
);
match params.id { (axum::http::StatusCode::OK, response)
Some(id) => match super::db::get_coverart_queue_data_with_id(&pool, &id).await { }
Ok(cover_art_queue) => { Err(_err) => (
response.message = String::from("Successful"); axum::http::StatusCode::BAD_REQUEST,
response.data.push(cover_art_queue); axum::response::Response::default(),
(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))
}
},
} }
} }
+1 -1
View File
@@ -9,7 +9,7 @@ pub mod endpoints {
pub const NEXTQUEUESONG: &str = "/api/v2/song/queue/next"; pub const NEXTQUEUESONG: &str = "/api/v2/song/queue/next";
pub const QUEUEMETADATA: &str = "/api/v2/song/metadata/queue"; pub const QUEUEMETADATA: &str = "/api/v2/song/metadata/queue";
pub const QUEUECOVERART: &str = "/api/v2/coverart/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 QUEUECOVERARTLINK: &str = "/api/v2/coverart/queue/link";
pub const QUEUESONGDATAWIPE: &str = "/api/v2/song/queue/data/wipe"; pub const QUEUESONGDATAWIPE: &str = "/api/v2/song/queue/data/wipe";
pub const QUEUECOVERARTDATAWIPE: &str = "/api/v2/coverart/queue/data/wipe"; pub const QUEUECOVERARTDATAWIPE: &str = "/api/v2/coverart/queue/data/wipe";
+28 -13
View File
@@ -153,6 +153,7 @@ pub async fn root() -> &'static str {
mod tests { mod tests {
use crate::db; use crate::db;
use std::io::Write;
use std::usize; use std::usize;
use common_multipart_rfc7578::client::multipart::{ use common_multipart_rfc7578::client::multipart::{
@@ -1257,11 +1258,15 @@ mod tests {
"Should not be empty" "Should not be empty"
); );
let raw_uri =
String::from(crate::callers::endpoints::QUEUECOVERARTDATA);
let end_index = raw_uri.len() - 5;
let uri = format!( let uri = format!(
"{}?id={}", "{}/{}",
crate::callers::endpoints::QUEUECOVERARTDATA, (&raw_uri[..end_index]).to_string(),
resp_coverart_id resp_coverart_id
); );
println!("Uri: {:?}", uri);
match app match app
.clone() .clone()
@@ -1275,17 +1280,27 @@ mod tests {
) )
.await .await
{ {
Ok(response) => { Ok(response) => match resp_to_bytes(response).await {
let resp = get_resp_data::< Ok(bytes) => {
crate::callers::coverart::response::fetch_coverart_with_data::Response, assert_eq!(
>(response) false,
.await; bytes.is_empty(),
assert_eq!( "Downloaded coverart data should not be empty"
false, );
resp.data.is_empty(), let temp_file = tempfile::tempdir()
"Should not be empty" .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);
}
},
Err(err) => { Err(err) => {
assert!(false, "Error: {:?}", err); assert!(false, "Error: {:?}", err);
} }