Added error checking for file creation in create metadata endpoint (#154)

* Added error checking for file creation in create metadata endpoint

* Code formatting

* Fixed create metadata endoint

* Added missing message in the response

* Cleanup

* Warning fixes

* Code formatting
This commit was merged in pull request #154.
This commit is contained in:
KD
2025-07-14 19:42:07 -04:00
committed by GitHub
parent fd9df41f51
commit 5a2639cb59
+30 -2
View File
@@ -782,9 +782,22 @@ pub mod endpoint {
Ok(data) => { Ok(data) => {
song.data = data; song.data = data;
let dir = std::path::Path::new(&song.directory); let dir = std::path::Path::new(&song.directory);
if !dir.exists() {
println!("Creating directory");
match std::fs::create_dir_all(dir) {
Ok(_) => {
println!("Successfully created directory");
}
Err(err) => {
eprintln!("Error: Unable to create the directory {err:?}");
}
}
}
let save_path = dir.join(&song.filename); let save_path = dir.join(&song.filename);
let mut file = std::fs::File::create(&save_path).unwrap(); match std::fs::File::create(&save_path) {
Ok(mut file) => {
file.write_all(&song.data).unwrap(); file.write_all(&song.data).unwrap();
match song.song_path() { match song.song_path() {
@@ -792,12 +805,14 @@ pub mod endpoint {
Ok((date_created, id)) => { Ok((date_created, id)) => {
song.id = id; song.id = id;
song.date_created = date_created; song.date_created = date_created;
response.message = String::from("Successful");
response.data.push(song); response.data.push(song);
(axum::http::StatusCode::OK, axum::Json(response)) (axum::http::StatusCode::OK, axum::Json(response))
} }
Err(err) => { Err(err) => {
response.message = format!("{:?} song {:?}", err.to_string(), song); response.message =
format!("{:?} song {:?}", err.to_string(), song);
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) (axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
} }
}, },
@@ -807,6 +822,19 @@ pub mod endpoint {
} }
} }
} }
Err(err) => {
let song_path = song.song_path();
response.message = format!(
"{err:?} Song directory: {} Filename: {} Save Path: {:?} Song Path: {:?}",
song.directory, song.filename, save_path, song_path
);
(
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response),
)
}
}
}
Err(err) => { Err(err) => {
response.message = err.to_string(); response.message = err.to_string();
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) (axum::http::StatusCode::BAD_REQUEST, axum::Json(response))