From 1871ebe40bef9e9c48cc7e944e162c8e966d998c Mon Sep 17 00:00:00 2001 From: KD Date: Mon, 27 Oct 2025 14:16:04 -0400 Subject: [PATCH] tsk-203: Addressing hard coding when downloading coverart (#223) * tsk-203: Addressing hard coding when downloading coverart * tsk-203: Code formatting * tsk-203: Version bump * tsk-203: Make downloaded coverart file random * tsk-203: Code formatting --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/callers/coverart.rs | 39 ++++++++++++++++++++++++++++++++++++--- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a7fc01e..e81dcfb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -834,7 +834,7 @@ dependencies = [ [[package]] name = "icarus" -version = "0.3.8" +version = "0.3.9" dependencies = [ "axum", "axum-extra", diff --git a/Cargo.toml b/Cargo.toml index b70249a..dcab15f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "icarus" -version = "0.3.8" +version = "0.3.9" edition = "2024" rust-version = "1.90" diff --git a/src/callers/coverart.rs b/src/callers/coverart.rs index 82de0ff..fec5fed 100644 --- a/src/callers/coverart.rs +++ b/src/callers/coverart.rs @@ -179,17 +179,50 @@ pub mod endpoint { match repo::coverart::get_coverart(&pool, &id).await { Ok(coverart) => match icarus_models::coverart::io::to_data(&coverart) { Ok(data) => { + let (file_type, img_type) = + match icarus_meta::detection::coverart::file_type_from_data(&data) { + Ok(file_type) => { + if file_type.file_type + == icarus_meta::detection::coverart::constants::JPEG_TYPE + { + ( + file_type, + icarus_models::types::CoverArtTypes::JpegExtension, + ) + } else if file_type.file_type + == icarus_meta::detection::coverart::constants::JPG_TYPE + { + (file_type, icarus_models::types::CoverArtTypes::JpgExtension) + } else if file_type.file_type + == icarus_meta::detection::coverart::constants::PNG_TYPE + { + (file_type, icarus_models::types::CoverArtTypes::PngExtension) + } else { + return ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::response::Response::default(), + ); + } + } + Err(err) => { + eprintln!("Error: {err:?}"); + return ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::response::Response::default(), + ); + } + }; let bytes = axum::body::Bytes::from(data); let mut response = bytes.into_response(); let headers = response.headers_mut(); - // TODO: Address hard coding + let filename = icarus_models::coverart::generate_filename(img_type, true); headers.insert( axum::http::header::CONTENT_TYPE, - "audio/jpg".parse().unwrap(), + file_type.mime.parse().unwrap(), ); headers.insert( axum::http::header::CONTENT_DISPOSITION, - format!("attachment; filename=\"{id}.jpg\"") + format!("attachment; filename=\"{filename}\"") .parse() .unwrap(), );