From ffe293d9552c713bf31f814907ade31bdd1cdaea Mon Sep 17 00:00:00 2001 From: phoenix Date: Fri, 14 Aug 2026 09:33:57 -0400 Subject: [PATCH] Adding code for saving to bucket --- src/callers/song.rs | 39 ++++++++++++++++++++++++++++++++++++++- src/repo/song.rs | 19 ++++++++++++++++--- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/src/callers/song.rs b/src/callers/song.rs index 656ae47..e399cf4 100644 --- a/src/callers/song.rs +++ b/src/callers/song.rs @@ -137,6 +137,7 @@ pub mod endpoint { 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; let lab_config = crate::util::maze::get_config(); @@ -159,8 +160,43 @@ pub mod endpoint { } } + song.file_key = format!("processed/song/{}", song.filename); + let data_of_song = labyrinth::Data { + raw_data: song.data.clone(), + ..Default::default() + }; + + 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); + + (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:?}"); + ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response), + ) + } + } + + /* match song.save_to_filesystem() { - Ok(_) => match repo::song::insert(&pool, &song).await { + Ok(_) => + match repo::song::insert(&pool, &song).await { Ok((date_created, id)) => { song.id = id; song.date_created = Some(date_created); @@ -183,6 +219,7 @@ pub mod endpoint { ) } } + */ } Err(err) => { eprintln!("Error: {err:?}"); diff --git a/src/repo/song.rs b/src/repo/song.rs index fa70fd5..18db3c2 100644 --- a/src/repo/song.rs +++ b/src/repo/song.rs @@ -6,8 +6,8 @@ pub async fn insert( ) -> Result<(time::OffsetDateTime, uuid::Uuid), sqlx::Error> { let result = sqlx::query( r#" - INSERT INTO "song" (title, artist, album_artist, album, genre, year, track, disc, track_count, disc_count, duration, audio_type, filename, directory, user_id) - VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15) RETURNING date_created, id; + INSERT INTO "song" (title, artist, album_artist, album, genre, year, track, disc, track_count, disc_count, duration, audio_type, filename, directory, file_key, user_id) + VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16) RETURNING date_created, id; "# ) .bind(&song.title) @@ -24,6 +24,7 @@ pub async fn insert( .bind(&song.audio_type) .bind(&song.filename) .bind(&song.directory) + .bind(&song.file_key) .bind(song.user_id) .fetch_one(pool) .await @@ -133,6 +134,10 @@ pub async fn get_song( .try_get("directory") .map_err(|_e| sqlx::Error::RowNotFound) .unwrap(), + file_key: row + .try_get("file_key") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), date_created: Some(date_created_time), user_id: row .try_get("user_id") @@ -228,6 +233,10 @@ pub async fn get_all_songs(pool: &sqlx::PgPool) -> Result