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()) + } + } + } }