From 5cf37cee6a1552a9eaa054abc93965739d30621f Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 21 Oct 2025 12:45:08 -0400 Subject: [PATCH] tsk-212: Got functionality working --- src/callers/coverart.rs | 71 +++++++++++++++++++++++++++++++---------- 1 file changed, 55 insertions(+), 16 deletions(-) diff --git a/src/callers/coverart.rs b/src/callers/coverart.rs index 09901cb..c6c60f1 100644 --- a/src/callers/coverart.rs +++ b/src/callers/coverart.rs @@ -485,6 +485,24 @@ pub mod cov_db { } } +mod helper { + pub fn is_coverart_file_type_valid(file_type: &String) -> bool { + let valid_file_types = vec![ + String::from("png"), + String::from("jpg"), + String::from("jpeg"), + ]; + + for valid_file_type in valid_file_types { + if valid_file_type == *file_type { + return true; + } + } + + return false; + } +} + pub mod endpoint { use axum::response::IntoResponse; @@ -516,24 +534,45 @@ pub mod endpoint { let content_type = field.content_type().unwrap().to_string(); let data = field.bytes().await.unwrap(); let raw_data = data.to_vec(); + let file_type = + match icarus_meta::detection::coverart::file_type_from_data(&raw_data) { + Ok(file_type) => file_type, + Err(err) => { + eprintln!("Error: {err:?}"); + response.message = err.to_string(); + return ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response), + ); + } + }; - println!( - "Received file '{}' (name = '{}', content-type = '{}', size = {})", - file_name, - name, - content_type, - data.len() - ); + if !super::helper::is_coverart_file_type_valid(&file_type) { + response.message = format!("CoverArt file type not supported: {file_type:?}"); + return ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response), + ); + } else { + println!( + "Received file '{}' (name = '{}', content-type = '{}', size = {}, file-type = {})", + file_name, + name, + content_type, + data.len(), + file_type + ); - match super::db::insert(&pool, &raw_data).await { - Ok(id) => { - response.message = String::from("Successful"); - response.data.push(id); - (axum::http::StatusCode::OK, axum::Json(response)) - } - Err(err) => { - response.message = err.to_string(); - (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) + match super::db::insert(&pool, &raw_data).await { + Ok(id) => { + response.message = String::from("Successful"); + response.data.push(id); + (axum::http::StatusCode::OK, axum::Json(response)) + } + Err(err) => { + response.message = err.to_string(); + (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) + } } } }