From d8f0c08cee071bbf7615933190294ceac2941a5d Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Mon, 28 Jul 2025 16:52:53 -0400 Subject: [PATCH] Added download coverart endpoint --- src/callers/coverart.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/callers/coverart.rs b/src/callers/coverart.rs index 7a3398a..b987daa 100644 --- a/src/callers/coverart.rs +++ b/src/callers/coverart.rs @@ -646,4 +646,38 @@ pub mod endpoint { } } } + + pub async fn download_coverart(axum::Extension(pool): axum::Extension, + axum::extract::Path(id): axum::extract::Path) -> + (axum::http::StatusCode, axum::response::Response) { + + match super::cov_db::get_coverart(&pool, &id).await { + Ok(coverart) => match coverart.to_data() { + Ok(data) => { + let bytes = axum::body::Bytes::from(data); + let mut response = bytes.into_response(); + let headers = response.headers_mut(); + // TODO: Address hard coding + headers.insert( + axum::http::header::CONTENT_TYPE, + "audio/jpg".parse().unwrap(), + ); + headers.insert( + axum::http::header::CONTENT_DISPOSITION, + format!("attachment; filename=\"{id}.jpg\"") + .parse() + .unwrap(), + ); + + (axum::http::StatusCode::OK, response) + } + Err(_err) => { + (axum::http::StatusCode::NOT_FOUND, axum::response::Response::default()) + } + } + Err(_err) => { + (axum::http::StatusCode::NOT_FOUND, axum::response::Response::default()) + } + } + } }