diff --git a/Cargo.lock b/Cargo.lock index e81dcfb..ee7173a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -834,7 +834,7 @@ dependencies = [ [[package]] name = "icarus" -version = "0.3.9" +version = "0.3.10" dependencies = [ "axum", "axum-extra", diff --git a/Cargo.toml b/Cargo.toml index dcab15f..55321cd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "icarus" -version = "0.3.9" +version = "0.3.10" edition = "2024" rust-version = "1.90" diff --git a/src/callers/queue/song.rs b/src/callers/queue/song.rs index 73c09c7..1dcf38d 100644 --- a/src/callers/queue/song.rs +++ b/src/callers/queue/song.rs @@ -96,6 +96,19 @@ pub mod response { } } +pub async fn is_song_valid(data: &[u8]) -> Result { + 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 { use axum::response::IntoResponse; @@ -139,22 +152,38 @@ pub mod endpoint { ); let raw_data: Vec = data.to_vec(); - let queue_repo = repo::song::insert( - &pool, - &raw_data, - &file_name, - &crate::repo::queue::song::status::PENDING.to_string(), - ) - .await - .unwrap(); - results.push(queue_repo); + match super::is_song_valid(&raw_data).await { + Ok(valid) => { + if valid { + let queue_repo = repo::song::insert( + &pool, + &raw_data, + &file_name, + &crate::repo::queue::song::status::PENDING.to_string(), + ) + .await + .unwrap(); + 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.message = if response.data.is_empty() { String::from("Error") } else { - String::from("Success") + String::from(super::super::super::response::SUCCESSFUL) }; (axum::http::StatusCode::OK, axum::Json(response)) @@ -234,7 +263,6 @@ pub mod endpoint { } } - // TODO: Rename /// Endpoint to download the queued song #[utoipa::path( get, @@ -383,15 +411,35 @@ pub mod endpoint { ); let raw_data: Vec = data.to_vec(); - match repo::song::update(&pool, &raw_data, &id).await { - Ok(_) => { - response.message = String::from("Successful"); - response.data.push(id); - (axum::http::StatusCode::OK, axum::Json(response)) + match super::is_song_valid(&raw_data).await { + Ok(valid) => { + if valid { + match repo::song::update(&pool, &raw_data, &id).await { + Ok(_) => { + response.message = + String::from(super::super::super::response::SUCCESSFUL); + response.data.push(id); + (axum::http::StatusCode::OK, axum::Json(response)) + } + Err(err) => { + 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)) + } } Err(err) => { response.message = err.to_string(); - (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) + ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response), + ) } } } else {