diff --git a/src/callers/song.rs b/src/callers/song.rs index e399cf4..a4333aa 100644 --- a/src/callers/song.rs +++ b/src/callers/song.rs @@ -167,23 +167,21 @@ pub mod endpoint { }; match lr.upload(&song.file_key, &data_of_song).await { - Ok(_resp) => { - match repo::song::insert(&pool, &song).await { - Ok((date_created, id)) => { - song.id = id; - song.date_created = Some(date_created); - response.message = String::from("Successful"); - response.data.push(song); + Ok(_resp) => match repo::song::insert(&pool, &song).await { + Ok((date_created, id)) => { + song.id = id; + song.date_created = Some(date_created); + response.message = String::from("Successful"); + response.data.push(song); - (axum::http::StatusCode::OK, axum::Json(response)) - } - Err(err) => { - response.message = - format!("{:?} song {:?}", err.to_string(), song); - (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) - } + (axum::http::StatusCode::OK, axum::Json(response)) } - } + Err(err) => { + response.message = + format!("{:?} song {:?}", err.to_string(), song); + (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) + } + }, Err(err) => { eprintln!("Error: {err:?}"); ( @@ -195,7 +193,7 @@ pub mod endpoint { /* match song.save_to_filesystem() { - Ok(_) => + Ok(_) => match repo::song::insert(&pool, &song).await { Ok((date_created, id)) => { song.id = id; @@ -327,45 +325,88 @@ pub mod endpoint { axum::extract::Path(id): axum::extract::Path, ) -> impl IntoResponse { match repo::song::get_song(&pool, &id).await { - Ok(song) => { - let song_path = song.song_path().unwrap(); - let path = std::path::Path::new(&song_path); + Ok(mut song) => { + let lab_config = crate::util::maze::get_config(); + let lr = labyrinth::Labyrinth { config: lab_config }; - if !path.starts_with(&song.directory) || !path.exists() { - return Err((axum::http::StatusCode::NOT_FOUND, "File not found")); - } + match lr.download(&song.file_key).await { + Ok(data) => { + song.filename = + simodels::song::generate_filename(simodels::types::MusicType::FlacExtension, true) + .unwrap(); + // Will no longer be needed with s3 + song.directory = sienvy::environment::get_root_directory().value; + song.data = data; - let file = match tokio::fs::File::open(&path).await { - Ok(file) => file, - Err(_) => return Err((axum::http::StatusCode::NOT_FOUND, "File not found")), - }; + match song.save_to_filesystem() { + Ok(_) => { + let song_path = song.song_path().unwrap(); + let path = std::path::Path::new(&song_path); - let file_size = match file.metadata().await { - Ok(meta) => meta.len(), - Err(_) => { - return Err(( - axum::http::StatusCode::INTERNAL_SERVER_ERROR, - "Could not read file", - )); + if !path.starts_with(&song.directory) || !path.exists() { + return Err((axum::http::StatusCode::NOT_FOUND, "File not found")); + } + + let file = match tokio::fs::File::open(&path).await { + Ok(file) => file, + Err(_) => return Err((axum::http::StatusCode::NOT_FOUND, "File not found")), + }; + + + let file_size = match file.metadata().await { + Ok(meta) => meta.len(), + Err(_) => { + return Err(( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + "Could not read file", + )); + } + }; + + let mime = mime_guess::from_path(path).first_or_octet_stream(); + let stream = tokio_util::io::ReaderStream::new(file); + + let rep = axum::response::Response::builder() + .header("content-type", mime.to_string()) + .header("accept-ranges", "bytes") + .header("content-length", file_size.to_string()) + .body(axum::body::Body::from_stream(stream)) + .unwrap(); + match song.remove_from_filesystem() { + Ok(_) => { + Ok(rep) + } + Err(err) => { + eprintln!("Error: {err:?}"); + Err(( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + "Could not read file", + )) + } + } + } + Err(err) => { + eprintln!("Error: {err:?}"); + Err(( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + "Could not find file", + )) + } + } } - }; - - let mime = mime_guess::from_path(path).first_or_octet_stream(); - let stream = tokio_util::io::ReaderStream::new(file); - - let rep = axum::response::Response::builder() - .header("content-type", mime.to_string()) - .header("accept-ranges", "bytes") - .header("content-length", file_size.to_string()) - .body(axum::body::Body::from_stream(stream)) - .unwrap(); - - Ok(rep) + Err(err) => { + eprintln!("Error: {err:?}"); + Err(( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + "Could not find file", + )) + } + } } Err(_err) => Err(( axum::http::StatusCode::INTERNAL_SERVER_ERROR, "Could not find file", - )), + )) } } diff --git a/src/repo/song.rs b/src/repo/song.rs index 18db3c2..aa6a8b5 100644 --- a/src/repo/song.rs +++ b/src/repo/song.rs @@ -233,10 +233,10 @@ pub async fn get_all_songs(pool: &sqlx::PgPool) -> Result