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:
@@ -3,6 +3,7 @@ pub mod song;
|
||||
|
||||
pub mod endpoints {
|
||||
pub const QUEUESONG: &str = "/api/v2/song/queue";
|
||||
pub const QUEUESONGDATA: &str = "/api/v2/song/queue/{id}";
|
||||
pub const NEXTQUEUESONG: &str = "/api/v2/song/queue/next";
|
||||
pub const QUEUEMETADATA: &str = "/api/v2/song/metadata/queue";
|
||||
}
|
||||
|
||||
+55
-1
@@ -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()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user