S3 song download #260
+88
-47
@@ -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<uuid::Uuid>,
|
||||
) -> 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",
|
||||
)),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+4
-4
@@ -233,10 +233,10 @@ pub async fn get_all_songs(pool: &sqlx::PgPool) -> Result<Vec<simodels::song::So
|
||||
.try_get("directory")
|
||||
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||
.unwrap(),
|
||||
file_key: row
|
||||
.try_get("file_key")
|
||||
.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")
|
||||
|
||||
Reference in New Issue
Block a user