From 76531cbba7cc02b761b6e14bca70455a63c8a866 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Mon, 28 Jul 2025 16:20:45 -0400 Subject: [PATCH] Finished download song endpoint --- src/callers/song.rs | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/callers/song.rs b/src/callers/song.rs index f0ab49a..2fd7526 100644 --- a/src/callers/song.rs +++ b/src/callers/song.rs @@ -774,11 +774,6 @@ pub mod endpoint { } } - pub async fn download_song() - -> (axum::http::StatusCode, axum::response::Response) { - (axum::http::StatusCode::OK, axum::response::Response::default()) - } - pub async fn update_song_queue_status( axum::Extension(pool): axum::Extension, axum::Json(payload): axum::Json, @@ -1064,4 +1059,29 @@ pub mod endpoint { )), } } + + pub async fn download_song(axum::Extension(pool): axum::Extension, + axum::extract::Path(id): axum::extract::Path) + -> (axum::http::StatusCode, axum::response::Response) { + match super::song_db::get_song(&pool, &id).await { + Ok(song) => match song.to_data() { + Ok(data) => { + let bytes = axum::body::Bytes::from(data); + let mut response = bytes.into_response(); + let headers = response.headers_mut(); + headers.insert(axum::http::header::CONTENT_TYPE, "audio/flac".parse().unwrap()); + headers.insert(axum::http::header::CONTENT_DISPOSITION, format!("attachment; filename=\"{id}.flac\"").parse().unwrap()); + + (axum::http::StatusCode::OK, response) + } + Err(_err) => { + (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::response::Response::default()) + } + } + Err(_err) => { + (axum::http::StatusCode::NOT_FOUND, axum::response::Response::default()) + } + } + } + }