Download coverart endpoint #166

Merged
kdeng00 merged 5 commits from download_coverart into v0.2 2025-07-28 17:05:42 -04:00
5 changed files with 99 additions and 2 deletions
Generated
+1 -1
View File
@@ -762,7 +762,7 @@ dependencies = [
[[package]] [[package]]
name = "icarus" name = "icarus"
version = "0.1.97" version = "0.1.98"
dependencies = [ dependencies = [
"axum", "axum",
"common-multipart-rfc7578", "common-multipart-rfc7578",
+1 -1
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "icarus" name = "icarus"
version = "0.1.97" version = "0.1.98"
edition = "2024" edition = "2024"
rust-version = "1.88" rust-version = "1.88"
+36
View File
@@ -646,4 +646,40 @@ pub mod endpoint {
} }
} }
} }
pub async fn download_coverart(
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
axum::extract::Path(id): axum::extract::Path<uuid::Uuid>,
) -> (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(),
),
}
}
} }
+1
View File
@@ -21,6 +21,7 @@ pub mod endpoints {
pub const DOWNLOADSONG: &str = "/api/v2/song/download/{id}"; pub const DOWNLOADSONG: &str = "/api/v2/song/download/{id}";
pub const CREATECOVERART: &str = "/api/v2/coverart"; pub const CREATECOVERART: &str = "/api/v2/coverart";
pub const GETCOVERART: &str = "/api/v2/coverart"; pub const GETCOVERART: &str = "/api/v2/coverart";
pub const DOWNLOADCOVERART: &str = "/api/v2/coverart/download/{id}";
} }
pub mod response { pub mod response {
+60
View File
@@ -123,6 +123,10 @@ pub mod init {
crate::callers::endpoints::GETCOVERART, crate::callers::endpoints::GETCOVERART,
get(crate::callers::coverart::endpoint::get_coverart), get(crate::callers::coverart::endpoint::get_coverart),
) )
.route(
crate::callers::endpoints::DOWNLOADCOVERART,
get(crate::callers::coverart::endpoint::download_coverart),
)
.route( .route(
crate::callers::endpoints::STREAMSONG, crate::callers::endpoints::STREAMSONG,
get(crate::callers::song::endpoint::stream_song), get(crate::callers::song::endpoint::stream_song),
@@ -2061,5 +2065,61 @@ mod tests {
let _ = super::db_mgr::drop_database(&tm_pool, &db_name).await; let _ = super::db_mgr::drop_database(&tm_pool, &db_name).await;
} }
#[tokio::test]
async fn test_download_coverart() {
let tm_pool = super::db_mgr::get_pool().await.unwrap();
let db_name = super::db_mgr::generate_db_name().await;
match super::db_mgr::create_database(&tm_pool, &db_name).await {
Ok(_) => {
println!("Success");
}
Err(err) => {
assert!(false, "Error: {:?}", err);
}
}
let pool = super::db_mgr::connect_to_db(&db_name).await.unwrap();
super::db_mgr::migrations(&pool).await;
let app = super::init::app(pool).await;
let id = test_data::coverart_id().await.unwrap();
let uri =
super::format_url_with_value(crate::callers::endpoints::DOWNLOADCOVERART, &id)
.await;
match app
.clone()
.oneshot(
axum::http::Request::builder()
.method(axum::http::Method::GET)
.uri(&uri)
.body(axum::body::Body::empty())
.unwrap(),
)
.await
{
Ok(response) => {
let e = response.into_body();
let mut data = e.into_data_stream();
while let Some(chunk) = data.next().await {
match chunk {
Ok(_data) => {}
Err(err) => {
assert!(false, "Error: {err:?}");
}
}
}
}
Err(err) => {
assert!(false, "Error: {err:?}");
}
}
let _ = super::db_mgr::drop_database(&tm_pool, &db_name).await;
}
} }
} }