Fetch next queued song (#125)

* Added code

* Got it working

* Added test and refactored test code

* Removed comment

* Cleanup

* Code formatting
This commit was merged in pull request #125.
This commit is contained in:
KD
2025-04-26 18:38:17 -04:00
committed by GitHub
parent fd41d25c4c
commit 8755276b48
3 changed files with 165 additions and 12 deletions
+55 -1
View File
@@ -122,10 +122,36 @@ mod song_queue {
Err(_err) => Err(sqlx::Error::RowNotFound),
}
}
pub async fn get_data(pool: &sqlx::PgPool, id: &uuid::Uuid) -> Result<Vec<u8>, sqlx::Error> {
let result = sqlx::query(
r#"
SELECT data FROM "songQueue"
WHERE id = $1;
"#,
)
.bind(id)
.fetch_one(pool)
.await
.map_err(|e| {
eprintln!("Error inserting: {}", e);
});
match result {
Ok(row) => {
let data = row
.try_get("data")
.map_err(|_e| sqlx::Error::RowNotFound)
.unwrap();
Ok(data)
}
Err(_err) => Err(sqlx::Error::RowNotFound),
}
}
}
pub mod endpoint {
use axum::{Json, http::StatusCode};
use axum::{Json, http::StatusCode, response::IntoResponse};
use std::io::Write;
use crate::callers::song::song_queue;
@@ -197,4 +223,32 @@ pub mod endpoint {
}
}
}
pub async fn download_flac(
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
axum::extract::Path(id): axum::extract::Path<uuid::Uuid>,
) -> (StatusCode, axum::response::Response) {
println!("Id: {:?}", id);
match song_queue::get_data(&pool, &id).await {
Ok(data) => {
let by = axum::body::Bytes::from(data);
let mut response = by.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=\"{}.flac\"", id)
.parse()
.unwrap(),
);
(StatusCode::OK, response)
}
Err(_err) => (StatusCode::BAD_REQUEST, axum::response::Response::default()),
}
}
}