Fixed endpoint for getting coverart queue data (#143)

* Fixed endpoint for getting coverart queue data

* Modified test

* Corrected endpoint syntax

* Fixed test

* Some cleanup

* Formatting
This commit was merged in pull request #143.
This commit is contained in:
KD
2025-06-17 21:40:49 -04:00
committed by GitHub
parent 6f2e8d6e17
commit fa792be9cc
3 changed files with 53 additions and 56 deletions
+24 -42
View File
@@ -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<sqlx::PgPool>,
mut multipart: axum::extract::Multipart,
@@ -447,49 +449,29 @@ pub mod endpoint {
pub async fn fetch_coverart_with_data(
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
axum::extract::Query(params): axum::extract::Query<
super::request::fetch_coverart_with_data::Params,
>,
) -> (
axum::http::StatusCode,
axum::Json<super::response::fetch_coverart_with_data::Response>,
) {
let mut response = super::response::fetch_coverart_with_data::Response::default();
axum::extract::Path(id): axum::extract::Path<uuid::Uuid>,
) -> (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(),
),
}
}
+1 -1
View File
@@ -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";
+28 -13
View File
@@ -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);
}