tsk-213: Identify audio file type in initial step when queueing (#225)

* tsk-213: Adding song check when queueing songs

* tsk-213: Added check for updating queued song

* tsk-213: Removed todo

* tsk-213: Code cleanup

* tsk-213: Warning fix

* tsk-213: Version bump

* tsk-213: Minor status code changes
This commit was merged in pull request #225.
This commit is contained in:
KD
2025-10-29 14:58:34 -04:00
committed by GitHub
parent 1871ebe40b
commit 27ef175828
3 changed files with 67 additions and 19 deletions
Generated
+1 -1
View File
@@ -834,7 +834,7 @@ dependencies = [
[[package]] [[package]]
name = "icarus" name = "icarus"
version = "0.3.9" version = "0.3.10"
dependencies = [ dependencies = [
"axum", "axum",
"axum-extra", "axum-extra",
+1 -1
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "icarus" name = "icarus"
version = "0.3.9" version = "0.3.10"
edition = "2024" edition = "2024"
rust-version = "1.90" rust-version = "1.90"
+51 -3
View File
@@ -96,6 +96,19 @@ pub mod response {
} }
} }
pub async fn is_song_valid(data: &[u8]) -> Result<bool, std::io::Error> {
match icarus_meta::detection::song::file_type_from_data(data) {
Ok(file_type) => {
if file_type.file_type == icarus_meta::detection::song::constants::FLAC_TYPE {
Ok(true)
} else {
Ok(false)
}
}
Err(err) => Err(err),
}
}
pub mod endpoint { pub mod endpoint {
use axum::response::IntoResponse; use axum::response::IntoResponse;
@@ -139,6 +152,9 @@ pub mod endpoint {
); );
let raw_data: Vec<u8> = data.to_vec(); let raw_data: Vec<u8> = data.to_vec();
match super::is_song_valid(&raw_data).await {
Ok(valid) => {
if valid {
let queue_repo = repo::song::insert( let queue_repo = repo::song::insert(
&pool, &pool,
&raw_data, &raw_data,
@@ -148,13 +164,26 @@ pub mod endpoint {
.await .await
.unwrap(); .unwrap();
results.push(queue_repo); results.push(queue_repo);
} else {
response.message = String::from("Invalid song type");
return (axum::http::StatusCode::BAD_REQUEST, axum::Json(response));
}
}
Err(err) => {
response.message = err.to_string();
return (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response),
);
}
}
} }
response.data = results; response.data = results;
response.message = if response.data.is_empty() { response.message = if response.data.is_empty() {
String::from("Error") String::from("Error")
} else { } else {
String::from("Success") String::from(super::super::super::response::SUCCESSFUL)
}; };
(axum::http::StatusCode::OK, axum::Json(response)) (axum::http::StatusCode::OK, axum::Json(response))
@@ -234,7 +263,6 @@ pub mod endpoint {
} }
} }
// TODO: Rename
/// Endpoint to download the queued song /// Endpoint to download the queued song
#[utoipa::path( #[utoipa::path(
get, get,
@@ -383,17 +411,37 @@ pub mod endpoint {
); );
let raw_data: Vec<u8> = data.to_vec(); let raw_data: Vec<u8> = data.to_vec();
match super::is_song_valid(&raw_data).await {
Ok(valid) => {
if valid {
match repo::song::update(&pool, &raw_data, &id).await { match repo::song::update(&pool, &raw_data, &id).await {
Ok(_) => { Ok(_) => {
response.message = String::from("Successful"); response.message =
String::from(super::super::super::response::SUCCESSFUL);
response.data.push(id); response.data.push(id);
(axum::http::StatusCode::OK, axum::Json(response)) (axum::http::StatusCode::OK, axum::Json(response))
} }
Err(err) => { Err(err) => {
response.message = err.to_string(); response.message = err.to_string();
(
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response),
)
}
}
} else {
response.message = String::from("Invalid song type");
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) (axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
} }
} }
Err(err) => {
response.message = err.to_string();
(
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response),
)
}
}
} else { } else {
response.message = String::from("No data provided"); response.message = String::from("No data provided");
(axum::http::StatusCode::NOT_FOUND, axum::Json(response)) (axum::http::StatusCode::NOT_FOUND, axum::Json(response))