S3 song download #260
+25
-12
@@ -331,8 +331,10 @@ pub mod endpoint {
|
|||||||
|
|
||||||
match lr.download(&song.file_key).await {
|
match lr.download(&song.file_key).await {
|
||||||
Ok(data) => {
|
Ok(data) => {
|
||||||
song.filename =
|
song.filename = simodels::song::generate_filename(
|
||||||
simodels::song::generate_filename(simodels::types::MusicType::FlacExtension, true)
|
simodels::types::MusicType::FlacExtension,
|
||||||
|
true,
|
||||||
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
// Will no longer be needed with s3
|
// Will no longer be needed with s3
|
||||||
song.directory = sienvy::environment::get_root_directory().value;
|
song.directory = sienvy::environment::get_root_directory().value;
|
||||||
@@ -344,15 +346,22 @@ pub mod endpoint {
|
|||||||
let path = std::path::Path::new(&song_path);
|
let path = std::path::Path::new(&song_path);
|
||||||
|
|
||||||
if !path.starts_with(&song.directory) || !path.exists() {
|
if !path.starts_with(&song.directory) || !path.exists() {
|
||||||
return Err((axum::http::StatusCode::NOT_FOUND, "File not found"));
|
return Err((
|
||||||
|
axum::http::StatusCode::NOT_FOUND,
|
||||||
|
"File not found",
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let file = match tokio::fs::File::open(&path).await {
|
let file = match tokio::fs::File::open(&path).await {
|
||||||
Ok(file) => file,
|
Ok(file) => file,
|
||||||
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(_) => {
|
||||||
@@ -373,9 +382,7 @@ pub mod endpoint {
|
|||||||
.body(axum::body::Body::from_stream(stream))
|
.body(axum::body::Body::from_stream(stream))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
match song.remove_from_filesystem() {
|
match song.remove_from_filesystem() {
|
||||||
Ok(_) => {
|
Ok(_) => Ok(rep),
|
||||||
Ok(rep)
|
|
||||||
}
|
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("Error: {err:?}");
|
eprintln!("Error: {err:?}");
|
||||||
Err((
|
Err((
|
||||||
@@ -406,7 +413,7 @@ pub mod endpoint {
|
|||||||
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",
|
||||||
))
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -425,8 +432,11 @@ pub mod endpoint {
|
|||||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
axum::extract::Path(id): axum::extract::Path<uuid::Uuid>,
|
axum::extract::Path(id): axum::extract::Path<uuid::Uuid>,
|
||||||
) -> (axum::http::StatusCode, axum::response::Response) {
|
) -> (axum::http::StatusCode, axum::response::Response) {
|
||||||
|
let lab_config = crate::util::maze::get_config();
|
||||||
|
let lr = labyrinth::Labyrinth { config: lab_config };
|
||||||
|
|
||||||
match repo::song::get_song(&pool, &id).await {
|
match repo::song::get_song(&pool, &id).await {
|
||||||
Ok(song) => match simodels::song::io::to_data(&song) {
|
Ok(song) => match lr.download(&song.file_key).await {
|
||||||
Ok(data) => {
|
Ok(data) => {
|
||||||
let bytes = axum::body::Bytes::from(data);
|
let bytes = axum::body::Bytes::from(data);
|
||||||
let mut response = bytes.into_response();
|
let mut response = bytes.into_response();
|
||||||
@@ -475,6 +485,8 @@ pub mod endpoint {
|
|||||||
axum::Json<super::response::delete_song::Response>,
|
axum::Json<super::response::delete_song::Response>,
|
||||||
) {
|
) {
|
||||||
let mut response = super::response::delete_song::Response::default();
|
let mut response = super::response::delete_song::Response::default();
|
||||||
|
let lab_config = crate::util::maze::get_config();
|
||||||
|
let lr = labyrinth::Labyrinth { config: lab_config };
|
||||||
|
|
||||||
match repo::song::get_song(&pool, &id).await {
|
match repo::song::get_song(&pool, &id).await {
|
||||||
Ok(song) => {
|
Ok(song) => {
|
||||||
@@ -498,7 +510,8 @@ pub mod endpoint {
|
|||||||
match repo::coverart::delete_coverart(&pool, &coverart.id).await
|
match repo::coverart::delete_coverart(&pool, &coverart.id).await
|
||||||
{
|
{
|
||||||
Ok(deleted_coverart) => {
|
Ok(deleted_coverart) => {
|
||||||
match song.remove_from_filesystem() {
|
// match song.remove_from_filesystem() {
|
||||||
|
match lr.delete(&song.file_key).await {
|
||||||
Ok(_) => match coverart.remove_from_filesystem() {
|
Ok(_) => match coverart.remove_from_filesystem() {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
response.message = String::from(
|
response.message = String::from(
|
||||||
@@ -516,7 +529,7 @@ pub mod endpoint {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
response.message = err.to_string();
|
eprintln!("Error: {err:?}");
|
||||||
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response))
|
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user