v0.2 release #153

Merged
kdeng00 merged 11 commits from v0.2 into main 2025-08-30 13:34:06 -04:00
5 changed files with 102 additions and 2 deletions
Showing only changes of commit 703fc49f32 - Show all commits
Generated
+1 -1
View File
@@ -762,7 +762,7 @@ dependencies = [
[[package]] [[package]]
name = "icarus" name = "icarus"
version = "0.1.96" version = "0.1.97"
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.96" version = "0.1.97"
edition = "2024" edition = "2024"
rust-version = "1.88" rust-version = "1.88"
+1
View File
@@ -18,6 +18,7 @@ pub mod endpoints {
pub const CREATESONG: &str = "/api/v2/song"; pub const CREATESONG: &str = "/api/v2/song";
pub const GETSONGS: &str = "/api/v2/song"; pub const GETSONGS: &str = "/api/v2/song";
pub const STREAMSONG: &str = "/api/v2/song/stream/{id}"; pub const STREAMSONG: &str = "/api/v2/song/stream/{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";
} }
+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(),
),
}
}
} }
+64
View File
@@ -127,6 +127,10 @@ pub mod init {
crate::callers::endpoints::STREAMSONG, crate::callers::endpoints::STREAMSONG,
get(crate::callers::song::endpoint::stream_song), get(crate::callers::song::endpoint::stream_song),
) )
.route(
crate::callers::endpoints::DOWNLOADSONG,
get(crate::callers::song::endpoint::download_song),
)
} }
pub async fn app() -> axum::Router { pub async fn app() -> axum::Router {
@@ -654,6 +658,11 @@ mod tests {
serde_json::from_slice(&body).unwrap() serde_json::from_slice(&body).unwrap()
} }
pub async fn format_url_with_value(endpoint: &str, value: &uuid::Uuid) -> String {
let last = endpoint.len() - 5;
format!("{}/{value}", &endpoint[0..last])
}
// TODO: Change the name of the function to be more expressive and put into it's own module // TODO: Change the name of the function to be more expressive and put into it's own module
pub mod payload_data { pub mod payload_data {
pub async fn queue_metadata_payload_data(song_queue_id: &uuid::Uuid) -> serde_json::Value { pub async fn queue_metadata_payload_data(song_queue_id: &uuid::Uuid) -> serde_json::Value {
@@ -1997,5 +2006,60 @@ 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_song() {
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::song_id().await.unwrap();
let uri =
super::format_url_with_value(crate::callers::endpoints::DOWNLOADSONG, &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;
}
} }
} }