Downloading song endpoint (#165)

* Added initial code for downloading song endpoint

* Finished download song endpoint

* Making download endpoint available

* Added test

* Code formatting

* Version bump
This commit was merged in pull request #165.
This commit is contained in:
KD
2025-07-28 16:40:14 -04:00
committed by GitHub
parent e98e055de0
commit 703fc49f32
5 changed files with 102 additions and 2 deletions
+35
View File
@@ -1059,4 +1059,39 @@ pub mod endpoint {
)),
}
}
pub async fn download_song(
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
axum::extract::Path(id): axum::extract::Path<uuid::Uuid>,
) -> (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(),
),
}
}
}