S3 song download #260
+48
-7
@@ -167,8 +167,7 @@ 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);
|
||||||
@@ -182,8 +181,7 @@ pub mod endpoint {
|
|||||||
format!("{:?} song {:?}", err.to_string(), song);
|
format!("{:?} song {:?}", err.to_string(), song);
|
||||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("Error: {err:?}");
|
eprintln!("Error: {err:?}");
|
||||||
(
|
(
|
||||||
@@ -327,7 +325,21 @@ 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 lab_config = crate::util::maze::get_config();
|
||||||
|
let lr = labyrinth::Labyrinth { config: lab_config };
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
match song.save_to_filesystem() {
|
||||||
|
Ok(_) => {
|
||||||
let song_path = song.song_path().unwrap();
|
let song_path = song.song_path().unwrap();
|
||||||
let path = std::path::Path::new(&song_path);
|
let path = std::path::Path::new(&song_path);
|
||||||
|
|
||||||
@@ -340,6 +352,7 @@ pub mod endpoint {
|
|||||||
Err(_) => return Err((axum::http::StatusCode::NOT_FOUND, "File not found")),
|
Err(_) => return Err((axum::http::StatusCode::NOT_FOUND, "File not found")),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
let file_size = match file.metadata().await {
|
let file_size = match file.metadata().await {
|
||||||
Ok(meta) => meta.len(),
|
Ok(meta) => meta.len(),
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
@@ -359,13 +372,41 @@ pub mod endpoint {
|
|||||||
.header("content-length", file_size.to_string())
|
.header("content-length", file_size.to_string())
|
||||||
.body(axum::body::Body::from_stream(stream))
|
.body(axum::body::Body::from_stream(stream))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
match song.remove_from_filesystem() {
|
||||||
|
Ok(_) => {
|
||||||
Ok(rep)
|
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:?}");
|
||||||
|
Err((
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
"Could not find file",
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
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",
|
||||||
)),
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user