diff --git a/src/callers/coverart.rs b/src/callers/coverart.rs index fce6d80..79661f1 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,49 +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::http::StatusCode, - axum::Json, - ) { - let mut response = super::response::fetch_coverart_with_data::Response::default(); + axum::extract::Path(id): axum::extract::Path, + ) -> (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); + 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(), + ); - 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)) - } - }, + (axum::http::StatusCode::OK, response) + } + Err(_err) => ( + axum::http::StatusCode::BAD_REQUEST, + axum::response::Response::default(), + ), } } 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"; diff --git a/src/main.rs b/src/main.rs index 1fee55c..572dd37 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,11 +1258,15 @@ mod tests { "Should not be empty" ); + let raw_uri = + String::from(crate::callers::endpoints::QUEUECOVERARTDATA); + let end_index = raw_uri.len() - 5; let uri = format!( - "{}?id={}", - crate::callers::endpoints::QUEUECOVERARTDATA, + "{}/{}", + (&raw_uri[..end_index]).to_string(), resp_coverart_id ); + println!("Uri: {:?}", uri); match app .clone() @@ -1275,17 +1280,27 @@ mod tests { ) .await { - Ok(response) => { - 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" - ); - } + 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); + } + }, Err(err) => { assert!(false, "Error: {:?}", err); }