S3 song download #260

Merged
phoenix merged 15 commits from s3-song_download into main 2026-08-14 16:41:57 -04:00
2 changed files with 92 additions and 51 deletions
Showing only changes of commit d20bc830f3 - Show all commits
+88 -47
View File
@@ -167,23 +167,21 @@ pub mod endpoint {
}; };
match lr.upload(&song.file_key, &data_of_song).await { match lr.upload(&song.file_key, &data_of_song).await {
Ok(_resp) => { Ok(_resp) => match repo::song::insert(&pool, &song).await {
match repo::song::insert(&pool, &song).await { Ok((date_created, id)) => {
Ok((date_created, id)) => { song.id = id;
song.id = id; song.date_created = Some(date_created);
song.date_created = Some(date_created); response.message = String::from("Successful");
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) => {
response.message =
format!("{:?} song {:?}", err.to_string(), song);
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
}
} }
} Err(err) => {
response.message =
format!("{:?} song {:?}", err.to_string(), song);
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
}
},
Err(err) => { Err(err) => {
eprintln!("Error: {err:?}"); eprintln!("Error: {err:?}");
( (
@@ -195,7 +193,7 @@ pub mod endpoint {
/* /*
match song.save_to_filesystem() { match song.save_to_filesystem() {
Ok(_) => Ok(_) =>
match repo::song::insert(&pool, &song).await { match repo::song::insert(&pool, &song).await {
Ok((date_created, id)) => { Ok((date_created, id)) => {
song.id = id; song.id = id;
@@ -327,45 +325,88 @@ pub mod endpoint {
axum::extract::Path(id): axum::extract::Path<uuid::Uuid>, axum::extract::Path(id): axum::extract::Path<uuid::Uuid>,
) -> impl IntoResponse { ) -> impl IntoResponse {
match repo::song::get_song(&pool, &id).await { match repo::song::get_song(&pool, &id).await {
Ok(song) => { Ok(mut song) => {
let song_path = song.song_path().unwrap(); let lab_config = crate::util::maze::get_config();
let path = std::path::Path::new(&song_path); let lr = labyrinth::Labyrinth { config: lab_config };
if !path.starts_with(&song.directory) || !path.exists() { match lr.download(&song.file_key).await {
return Err((axum::http::StatusCode::NOT_FOUND, "File not found")); 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 { match song.save_to_filesystem() {
Ok(file) => file, Ok(_) => {
Err(_) => return Err((axum::http::StatusCode::NOT_FOUND, "File not found")), let song_path = song.song_path().unwrap();
}; let path = std::path::Path::new(&song_path);
let file_size = match file.metadata().await { if !path.starts_with(&song.directory) || !path.exists() {
Ok(meta) => meta.len(), return Err((axum::http::StatusCode::NOT_FOUND, "File not found"));
Err(_) => { }
return Err((
axum::http::StatusCode::INTERNAL_SERVER_ERROR, let file = match tokio::fs::File::open(&path).await {
"Could not read file", 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",
))
}
}
} }
}; Err(err) => {
eprintln!("Error: {err:?}");
let mime = mime_guess::from_path(path).first_or_octet_stream(); Err((
let stream = tokio_util::io::ReaderStream::new(file); axum::http::StatusCode::INTERNAL_SERVER_ERROR,
"Could not find 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) => Err(( Err(_err) => Err((
axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::http::StatusCode::INTERNAL_SERVER_ERROR,
"Could not find file", "Could not find file",
)), ))
} }
} }
+4 -4
View File
@@ -233,10 +233,10 @@ pub async fn get_all_songs(pool: &sqlx::PgPool) -> Result<Vec<simodels::song::So
.try_get("directory") .try_get("directory")
.map_err(|_e| sqlx::Error::RowNotFound) .map_err(|_e| sqlx::Error::RowNotFound)
.unwrap(), .unwrap(),
file_key: row file_key: row
.try_get("file_key") .try_get("file_key")
.map_err(|_e| sqlx::Error::RowNotFound) .map_err(|_e| sqlx::Error::RowNotFound)
.unwrap(), .unwrap(),
date_created: Some(date_created_time), date_created: Some(date_created_time),
user_id: row user_id: row
.try_get("user_id") .try_get("user_id")