tsk-199: Created queue module for queue endpoints
This commit is contained in:
@@ -1,49 +1,4 @@
|
||||
// TODO: Separate queue and coverart endpoints
|
||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||
pub struct CoverArtQueue {
|
||||
pub id: uuid::Uuid,
|
||||
pub file_type: String,
|
||||
pub song_queue_id: uuid::Uuid,
|
||||
}
|
||||
|
||||
pub mod request {
|
||||
pub mod queue {
|
||||
#[derive(utoipa::ToSchema)]
|
||||
pub struct Request {
|
||||
/// Filename
|
||||
pub file: String,
|
||||
#[schema(rename = "type")]
|
||||
/// File type. Should be a file and not a value
|
||||
pub file_type: String,
|
||||
/// Raw data of the cover art file
|
||||
pub value: Vec<u8>,
|
||||
}
|
||||
}
|
||||
|
||||
pub mod link {
|
||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||
pub struct Request {
|
||||
pub coverart_id: uuid::Uuid,
|
||||
pub song_queue_id: uuid::Uuid,
|
||||
}
|
||||
}
|
||||
|
||||
pub mod fetch_coverart_no_data {
|
||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||
pub struct Params {
|
||||
pub id: Option<uuid::Uuid>,
|
||||
pub song_queue_id: Option<uuid::Uuid>,
|
||||
}
|
||||
}
|
||||
|
||||
pub mod fetch_coverart_with_data {
|
||||
#[derive(Debug, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||
pub struct Params {
|
||||
pub id: Option<uuid::Uuid>,
|
||||
pub song_queue_id: Option<uuid::Uuid>,
|
||||
}
|
||||
}
|
||||
|
||||
pub mod create_coverart {
|
||||
#[derive(Debug, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||
pub struct Request {
|
||||
@@ -52,13 +7,6 @@ pub mod request {
|
||||
}
|
||||
}
|
||||
|
||||
pub mod wipe_data_from_coverart_queue {
|
||||
#[derive(Debug, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||
pub struct Request {
|
||||
pub coverart_queue_id: uuid::Uuid,
|
||||
}
|
||||
}
|
||||
|
||||
pub mod get_coverart {
|
||||
#[derive(Debug, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||
pub struct Params {
|
||||
@@ -68,42 +16,6 @@ pub mod request {
|
||||
}
|
||||
|
||||
pub mod response {
|
||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||
pub struct Response {
|
||||
pub message: String,
|
||||
pub data: Vec<uuid::Uuid>,
|
||||
}
|
||||
|
||||
pub mod link {
|
||||
#[derive(Debug, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||
pub struct Id {
|
||||
pub coverart_id: uuid::Uuid,
|
||||
pub song_queue_id: uuid::Uuid,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||
pub struct Response {
|
||||
pub message: String,
|
||||
pub data: Vec<Id>,
|
||||
}
|
||||
}
|
||||
|
||||
pub mod fetch_coverart_no_data {
|
||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||
pub struct Response {
|
||||
pub message: String,
|
||||
pub data: Vec<super::super::CoverArtQueue>,
|
||||
}
|
||||
}
|
||||
|
||||
pub mod fetch_coverart_with_data {
|
||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||
pub struct Response {
|
||||
pub message: String,
|
||||
pub data: Vec<Vec<u8>>,
|
||||
}
|
||||
}
|
||||
|
||||
pub mod create_coverart {
|
||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||
pub struct Response {
|
||||
@@ -112,14 +24,6 @@ pub mod response {
|
||||
}
|
||||
}
|
||||
|
||||
pub mod wipe_data_from_coverart_queue {
|
||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||
pub struct Response {
|
||||
pub message: String,
|
||||
pub data: Vec<uuid::Uuid>,
|
||||
}
|
||||
}
|
||||
|
||||
pub mod get_coverart {
|
||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||
pub struct Response {
|
||||
@@ -129,23 +33,6 @@ pub mod response {
|
||||
}
|
||||
}
|
||||
|
||||
mod helper {
|
||||
pub fn is_coverart_file_type_valid(file_type: &String) -> bool {
|
||||
let valid_file_types = vec![
|
||||
String::from(icarus_meta::detection::coverart::constants::JPEG_TYPE),
|
||||
String::from(icarus_meta::detection::coverart::constants::JPG_TYPE),
|
||||
String::from(icarus_meta::detection::coverart::constants::PNG_TYPE),
|
||||
];
|
||||
|
||||
for valid_file_type in valid_file_types {
|
||||
if valid_file_type == *file_type {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub mod endpoint {
|
||||
use axum::response::IntoResponse;
|
||||
@@ -153,226 +40,6 @@ pub mod endpoint {
|
||||
use crate::repo;
|
||||
use crate::repo::queue as repo_queue;
|
||||
|
||||
/// Endpoint to queue cover art
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = super::super::endpoints::QUEUECOVERART,
|
||||
request_body(
|
||||
content = super::request::queue::Request,
|
||||
),
|
||||
responses(
|
||||
(status = 200, description = "Successful", body = super::response::Response),
|
||||
(status = 400, description = "Error queueing cover art", body = super::response::Response)
|
||||
)
|
||||
)]
|
||||
pub async fn queue(
|
||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||
mut multipart: axum::extract::Multipart,
|
||||
) -> (
|
||||
axum::http::StatusCode,
|
||||
axum::Json<super::response::Response>,
|
||||
) {
|
||||
let mut response = super::response::Response::default();
|
||||
|
||||
match multipart.next_field().await {
|
||||
Ok(Some(field)) => {
|
||||
let name = field.name().unwrap().to_string();
|
||||
let file_name = field.file_name().unwrap().to_string();
|
||||
let content_type = field.content_type().unwrap().to_string();
|
||||
let data = field.bytes().await.unwrap();
|
||||
let raw_data = data.to_vec();
|
||||
let file_type =
|
||||
match icarus_meta::detection::coverart::file_type_from_data(&raw_data) {
|
||||
Ok(file_type) => file_type,
|
||||
Err(err) => {
|
||||
eprintln!("Error: {err:?}");
|
||||
response.message = err.to_string();
|
||||
return (
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(response),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
if !super::helper::is_coverart_file_type_valid(&file_type.file_type) {
|
||||
response.message = format!("CoverArt file type not supported: {file_type:?}");
|
||||
(
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(response),
|
||||
)
|
||||
} else {
|
||||
println!(
|
||||
"Received file '{}' (name = '{}', content-type = '{}', size = {}, file-type = {:?})",
|
||||
file_name,
|
||||
name,
|
||||
content_type,
|
||||
data.len(),
|
||||
file_type
|
||||
);
|
||||
|
||||
match repo_queue::coverart::insert(&pool, &raw_data, &file_type.file_type).await
|
||||
{
|
||||
Ok(id) => {
|
||||
response.message = String::from("Successful");
|
||||
response.data.push(id);
|
||||
(axum::http::StatusCode::OK, axum::Json(response))
|
||||
}
|
||||
Err(err) => {
|
||||
response.message = err.to_string();
|
||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(None) => (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)),
|
||||
Err(err) => {
|
||||
response.message = err.to_string();
|
||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Endpoint to link queued cover art
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = super::super::endpoints::QUEUECOVERARTLINK,
|
||||
request_body(
|
||||
content = super::request::link::Request,
|
||||
description = "Linking queued cover art to queued song",
|
||||
content_type = "application/json"
|
||||
),
|
||||
responses(
|
||||
(status = 200, description = "Queued cover art linked", body = super::response::link::Response),
|
||||
(status = 400, description = "Linkage failed", body = super::response::link::Response)
|
||||
)
|
||||
)]
|
||||
pub async fn link(
|
||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||
axum::Json(payload): axum::Json<super::request::link::Request>,
|
||||
) -> (
|
||||
axum::http::StatusCode,
|
||||
axum::Json<super::response::link::Response>,
|
||||
) {
|
||||
let mut response = super::response::link::Response::default();
|
||||
let id = payload.coverart_id;
|
||||
let song_id = payload.song_queue_id;
|
||||
|
||||
match repo_queue::coverart::update(&pool, &id, &song_id).await {
|
||||
Ok(_o) => {
|
||||
response.data.push(super::response::link::Id {
|
||||
song_queue_id: song_id,
|
||||
coverart_id: id,
|
||||
});
|
||||
|
||||
(axum::http::StatusCode::OK, axum::Json(response))
|
||||
}
|
||||
Err(err) => {
|
||||
response.message = err.to_string();
|
||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Endpoint to fetch cover art details
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = super::super::endpoints::QUEUECOVERART,
|
||||
params(
|
||||
("id" = uuid::Uuid, Path, description = "Queued cover art Id"),
|
||||
("song_queue_id" = uuid::Uuid, Path, description = "Queued song Id")
|
||||
),
|
||||
responses(
|
||||
(status = 200, description = "Queued song linked", body = super::response::fetch_coverart_no_data::Response),
|
||||
(status = 400, description = "Linkage failed", body = super::response::fetch_coverart_no_data::Response)
|
||||
)
|
||||
)]
|
||||
pub async fn fetch_coverart_no_data(
|
||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||
axum::extract::Query(params): axum::extract::Query<
|
||||
super::request::fetch_coverart_no_data::Params,
|
||||
>,
|
||||
) -> (
|
||||
axum::http::StatusCode,
|
||||
axum::Json<super::response::fetch_coverart_no_data::Response>,
|
||||
) {
|
||||
let mut response = super::response::fetch_coverart_no_data::Response::default();
|
||||
|
||||
match params.id {
|
||||
Some(id) => match repo_queue::coverart::get_coverart_queue_with_id(&pool, &id).await {
|
||||
Ok(cover_art_queue) => {
|
||||
response.message = String::from("Successful");
|
||||
response.data.push(cover_art_queue);
|
||||
(axum::http::StatusCode::OK, axum::Json(response))
|
||||
}
|
||||
Err(err) => {
|
||||
response.message = err.to_string();
|
||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||
}
|
||||
},
|
||||
_ => match params.song_queue_id {
|
||||
Some(song_queue_id) => {
|
||||
match repo_queue::coverart::get_coverart_queue_with_song_queue_id(
|
||||
&pool,
|
||||
&song_queue_id,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(cover_art_queue) => {
|
||||
response.message = String::from("Successful");
|
||||
response.data.push(cover_art_queue);
|
||||
(axum::http::StatusCode::OK, axum::Json(response))
|
||||
}
|
||||
Err(err) => {
|
||||
response.message = err.to_string();
|
||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {
|
||||
response.message = String::from("No valid id provided");
|
||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Endpoint to fetch the queued cover art data
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = super::super::endpoints::QUEUECOVERARTDATA,
|
||||
params(("id" = uuid::Uuid, Path, description = "Queued cover art Id")),
|
||||
responses(
|
||||
(status = 200, description = "Queued cover art data", body = Vec<u8>),
|
||||
(status = 400, description = "Error fetching queued cover art data", body = Vec<u8>)
|
||||
)
|
||||
)]
|
||||
pub async fn fetch_coverart_with_data(
|
||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||
axum::extract::Path(id): axum::extract::Path<uuid::Uuid>,
|
||||
) -> (axum::http::StatusCode, axum::response::Response) {
|
||||
match repo_queue::coverart::get_coverart_queue_data_with_id(&pool, &id).await {
|
||||
Ok(data) => {
|
||||
let bytes = axum::body::Bytes::from(data);
|
||||
let mut response = bytes.into_response();
|
||||
let headers = response.headers_mut();
|
||||
// TODO: Address this hard coding for the coverart content type
|
||||
headers.insert(axum::http::header::CONTENT_TYPE, "image".parse().unwrap());
|
||||
// TODO: Make the conent disposition more dynamic
|
||||
headers.insert(
|
||||
axum::http::header::CONTENT_DISPOSITION,
|
||||
format!("attachment; filename=\"{id}.jpg\"")
|
||||
.parse()
|
||||
.unwrap(),
|
||||
);
|
||||
|
||||
(axum::http::StatusCode::OK, response)
|
||||
}
|
||||
Err(_err) => (
|
||||
axum::http::StatusCode::BAD_REQUEST,
|
||||
axum::response::Response::default(),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// Endpoint to create cover art
|
||||
#[utoipa::path(
|
||||
@@ -455,51 +122,6 @@ pub mod endpoint {
|
||||
}
|
||||
}
|
||||
|
||||
/// Endpoint to wipe data from the cover art queue
|
||||
#[utoipa::path(
|
||||
patch,
|
||||
path = super::super::endpoints::QUEUECOVERARTDATAWIPE,
|
||||
request_body(
|
||||
content = super::request::wipe_data_from_coverart_queue::Request,
|
||||
description = "Data required to wipe the data from the cover art queue",
|
||||
content_type = "application/json"
|
||||
),
|
||||
responses(
|
||||
(status = 200, description = "Data wiped from cover art queue", body = super::response::wipe_data_from_coverart_queue::Response),
|
||||
(status = 400, description = "Error wiping the data", body = super::response::wipe_data_from_coverart_queue::Response),
|
||||
(status = 404, description = "Cover art not found", body = super::response::wipe_data_from_coverart_queue::Response)
|
||||
)
|
||||
)]
|
||||
pub async fn wipe_data_from_coverart_queue(
|
||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||
axum::Json(payload): axum::Json<super::request::wipe_data_from_coverart_queue::Request>,
|
||||
) -> (
|
||||
axum::http::StatusCode,
|
||||
axum::Json<super::response::wipe_data_from_coverart_queue::Response>,
|
||||
) {
|
||||
let mut response = super::response::wipe_data_from_coverart_queue::Response::default();
|
||||
let coverart_queue_id = payload.coverart_queue_id;
|
||||
|
||||
match repo_queue::coverart::get_coverart_queue_with_id(&pool, &coverart_queue_id).await {
|
||||
Ok(coverart_queue) => {
|
||||
match repo_queue::coverart::wipe_data(&pool, &coverart_queue.id).await {
|
||||
Ok(id) => {
|
||||
response.message = String::from("Success");
|
||||
response.data.push(id);
|
||||
(axum::http::StatusCode::OK, axum::Json(response))
|
||||
}
|
||||
Err(err) => {
|
||||
response.message = err.to_string();
|
||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
response.message = err.to_string();
|
||||
(axum::http::StatusCode::NOT_FOUND, axum::Json(response))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Endpoint to get cover art with criteria
|
||||
#[utoipa::path(
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
pub mod coverart;
|
||||
pub mod metadata;
|
||||
pub mod queue;
|
||||
pub mod song;
|
||||
|
||||
pub mod endpoints {
|
||||
|
||||
@@ -0,0 +1,395 @@
|
||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||
pub struct CoverArtQueue {
|
||||
pub id: uuid::Uuid,
|
||||
pub file_type: String,
|
||||
pub song_queue_id: uuid::Uuid,
|
||||
}
|
||||
|
||||
pub mod request {
|
||||
pub mod queue {
|
||||
#[derive(utoipa::ToSchema)]
|
||||
pub struct Request {
|
||||
/// Filename
|
||||
pub file: String,
|
||||
#[schema(rename = "type")]
|
||||
/// File type. Should be a file and not a value
|
||||
pub file_type: String,
|
||||
/// Raw data of the cover art file
|
||||
pub value: Vec<u8>,
|
||||
}
|
||||
}
|
||||
|
||||
pub mod link {
|
||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||
pub struct Request {
|
||||
pub coverart_id: uuid::Uuid,
|
||||
pub song_queue_id: uuid::Uuid,
|
||||
}
|
||||
}
|
||||
|
||||
pub mod fetch_coverart_no_data {
|
||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||
pub struct Params {
|
||||
pub id: Option<uuid::Uuid>,
|
||||
pub song_queue_id: Option<uuid::Uuid>,
|
||||
}
|
||||
}
|
||||
|
||||
pub mod fetch_coverart_with_data {
|
||||
#[derive(Debug, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||
pub struct Params {
|
||||
pub id: Option<uuid::Uuid>,
|
||||
pub song_queue_id: Option<uuid::Uuid>,
|
||||
}
|
||||
}
|
||||
|
||||
pub mod wipe_data_from_coverart_queue {
|
||||
#[derive(Debug, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||
pub struct Request {
|
||||
pub coverart_queue_id: uuid::Uuid,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub mod response {
|
||||
pub mod queue {
|
||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||
pub struct Response {
|
||||
pub message: String,
|
||||
pub data: Vec<uuid::Uuid>,
|
||||
}
|
||||
}
|
||||
|
||||
pub mod link {
|
||||
#[derive(Debug, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||
pub struct Id {
|
||||
pub coverart_id: uuid::Uuid,
|
||||
pub song_queue_id: uuid::Uuid,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||
pub struct Response {
|
||||
pub message: String,
|
||||
pub data: Vec<Id>,
|
||||
}
|
||||
}
|
||||
|
||||
pub mod fetch_coverart_no_data {
|
||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||
pub struct Response {
|
||||
pub message: String,
|
||||
pub data: Vec<super::super::CoverArtQueue>,
|
||||
}
|
||||
}
|
||||
|
||||
pub mod fetch_coverart_with_data {
|
||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||
pub struct Response {
|
||||
pub message: String,
|
||||
pub data: Vec<Vec<u8>>,
|
||||
}
|
||||
}
|
||||
|
||||
pub mod wipe_data_from_coverart_queue {
|
||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||
pub struct Response {
|
||||
pub message: String,
|
||||
pub data: Vec<uuid::Uuid>,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
mod helper {
|
||||
pub fn is_coverart_file_type_valid(file_type: &String) -> bool {
|
||||
let valid_file_types = vec![
|
||||
String::from(icarus_meta::detection::coverart::constants::JPEG_TYPE),
|
||||
String::from(icarus_meta::detection::coverart::constants::JPG_TYPE),
|
||||
String::from(icarus_meta::detection::coverart::constants::PNG_TYPE),
|
||||
];
|
||||
|
||||
for valid_file_type in valid_file_types {
|
||||
if valid_file_type == *file_type {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub mod endpoint {
|
||||
use axum::response::IntoResponse;
|
||||
|
||||
use crate::repo::queue as repo;
|
||||
|
||||
|
||||
/// Endpoint to queue cover art
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = super::super::super::endpoints::QUEUECOVERART,
|
||||
request_body(
|
||||
content = super::request::queue::Request,
|
||||
),
|
||||
responses(
|
||||
(status = 200, description = "Successful", body = super::response::queue::Response),
|
||||
(status = 400, description = "Error queueing cover art", body = super::response::queue::Response)
|
||||
)
|
||||
)]
|
||||
pub async fn queue(
|
||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||
mut multipart: axum::extract::Multipart,
|
||||
) -> (
|
||||
axum::http::StatusCode,
|
||||
axum::Json<super::response::queue::Response>,
|
||||
) {
|
||||
let mut response = super::response::queue::Response::default();
|
||||
|
||||
match multipart.next_field().await {
|
||||
Ok(Some(field)) => {
|
||||
let name = field.name().unwrap().to_string();
|
||||
let file_name = field.file_name().unwrap().to_string();
|
||||
let content_type = field.content_type().unwrap().to_string();
|
||||
let data = field.bytes().await.unwrap();
|
||||
let raw_data = data.to_vec();
|
||||
let file_type =
|
||||
match icarus_meta::detection::coverart::file_type_from_data(&raw_data) {
|
||||
Ok(file_type) => file_type,
|
||||
Err(err) => {
|
||||
eprintln!("Error: {err:?}");
|
||||
response.message = err.to_string();
|
||||
return (
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(response),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
if !super::helper::is_coverart_file_type_valid(&file_type.file_type) {
|
||||
response.message = format!("CoverArt file type not supported: {file_type:?}");
|
||||
(
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(response),
|
||||
)
|
||||
} else {
|
||||
println!(
|
||||
"Received file '{}' (name = '{}', content-type = '{}', size = {}, file-type = {:?})",
|
||||
file_name,
|
||||
name,
|
||||
content_type,
|
||||
data.len(),
|
||||
file_type
|
||||
);
|
||||
|
||||
match repo::coverart::insert(&pool, &raw_data, &file_type.file_type).await
|
||||
{
|
||||
Ok(id) => {
|
||||
response.message = String::from("Successful");
|
||||
response.data.push(id);
|
||||
(axum::http::StatusCode::OK, axum::Json(response))
|
||||
}
|
||||
Err(err) => {
|
||||
response.message = err.to_string();
|
||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(None) => (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)),
|
||||
Err(err) => {
|
||||
response.message = err.to_string();
|
||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Endpoint to link queued cover art
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = super::super::super::endpoints::QUEUECOVERARTLINK,
|
||||
request_body(
|
||||
content = super::request::link::Request,
|
||||
description = "Linking queued cover art to queued song",
|
||||
content_type = "application/json"
|
||||
),
|
||||
responses(
|
||||
(status = 200, description = "Queued cover art linked", body = super::response::link::Response),
|
||||
(status = 400, description = "Linkage failed", body = super::response::link::Response)
|
||||
)
|
||||
)]
|
||||
pub async fn link(
|
||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||
axum::Json(payload): axum::Json<super::request::link::Request>,
|
||||
) -> (
|
||||
axum::http::StatusCode,
|
||||
axum::Json<super::response::link::Response>,
|
||||
) {
|
||||
let mut response = super::response::link::Response::default();
|
||||
let id = payload.coverart_id;
|
||||
let song_id = payload.song_queue_id;
|
||||
|
||||
match repo::coverart::update(&pool, &id, &song_id).await {
|
||||
Ok(_o) => {
|
||||
response.data.push(super::response::link::Id {
|
||||
song_queue_id: song_id,
|
||||
coverart_id: id,
|
||||
});
|
||||
|
||||
(axum::http::StatusCode::OK, axum::Json(response))
|
||||
}
|
||||
Err(err) => {
|
||||
response.message = err.to_string();
|
||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Endpoint to fetch cover art details
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = super::super::super::endpoints::QUEUECOVERART,
|
||||
params(
|
||||
("id" = uuid::Uuid, Path, description = "Queued cover art Id"),
|
||||
("song_queue_id" = uuid::Uuid, Path, description = "Queued song Id")
|
||||
),
|
||||
responses(
|
||||
(status = 200, description = "Queued song linked", body = super::response::fetch_coverart_no_data::Response),
|
||||
(status = 400, description = "Linkage failed", body = super::response::fetch_coverart_no_data::Response)
|
||||
)
|
||||
)]
|
||||
pub async fn fetch_coverart_no_data(
|
||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||
axum::extract::Query(params): axum::extract::Query<
|
||||
super::request::fetch_coverart_no_data::Params,
|
||||
>,
|
||||
) -> (
|
||||
axum::http::StatusCode,
|
||||
axum::Json<super::response::fetch_coverart_no_data::Response>,
|
||||
) {
|
||||
let mut response = super::response::fetch_coverart_no_data::Response::default();
|
||||
|
||||
match params.id {
|
||||
Some(id) => match repo::coverart::get_coverart_queue_with_id(&pool, &id).await {
|
||||
Ok(cover_art_queue) => {
|
||||
response.message = String::from("Successful");
|
||||
response.data.push(cover_art_queue);
|
||||
(axum::http::StatusCode::OK, axum::Json(response))
|
||||
}
|
||||
Err(err) => {
|
||||
response.message = err.to_string();
|
||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||
}
|
||||
},
|
||||
_ => match params.song_queue_id {
|
||||
Some(song_queue_id) => {
|
||||
match repo::coverart::get_coverart_queue_with_song_queue_id(
|
||||
&pool,
|
||||
&song_queue_id,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(cover_art_queue) => {
|
||||
response.message = String::from("Successful");
|
||||
response.data.push(cover_art_queue);
|
||||
(axum::http::StatusCode::OK, axum::Json(response))
|
||||
}
|
||||
Err(err) => {
|
||||
response.message = err.to_string();
|
||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {
|
||||
response.message = String::from("No valid id provided");
|
||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Endpoint to fetch the queued cover art data
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = super::super::super::endpoints::QUEUECOVERARTDATA,
|
||||
params(("id" = uuid::Uuid, Path, description = "Queued cover art Id")),
|
||||
responses(
|
||||
(status = 200, description = "Queued cover art data", body = Vec<u8>),
|
||||
(status = 400, description = "Error fetching queued cover art data", body = Vec<u8>)
|
||||
)
|
||||
)]
|
||||
pub async fn fetch_coverart_with_data(
|
||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||
axum::extract::Path(id): axum::extract::Path<uuid::Uuid>,
|
||||
) -> (axum::http::StatusCode, axum::response::Response) {
|
||||
match repo::coverart::get_coverart_queue_data_with_id(&pool, &id).await {
|
||||
Ok(data) => {
|
||||
let bytes = axum::body::Bytes::from(data);
|
||||
let mut response = bytes.into_response();
|
||||
let headers = response.headers_mut();
|
||||
// TODO: Address this hard coding for the coverart content type
|
||||
headers.insert(axum::http::header::CONTENT_TYPE, "image".parse().unwrap());
|
||||
// TODO: Make the conent disposition more dynamic
|
||||
headers.insert(
|
||||
axum::http::header::CONTENT_DISPOSITION,
|
||||
format!("attachment; filename=\"{id}.jpg\"")
|
||||
.parse()
|
||||
.unwrap(),
|
||||
);
|
||||
|
||||
(axum::http::StatusCode::OK, response)
|
||||
}
|
||||
Err(_err) => (
|
||||
axum::http::StatusCode::BAD_REQUEST,
|
||||
axum::response::Response::default(),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// Endpoint to wipe data from the cover art queue
|
||||
#[utoipa::path(
|
||||
patch,
|
||||
path = super::super::super::endpoints::QUEUECOVERARTDATAWIPE,
|
||||
request_body(
|
||||
content = super::request::wipe_data_from_coverart_queue::Request,
|
||||
description = "Data required to wipe the data from the cover art queue",
|
||||
content_type = "application/json"
|
||||
),
|
||||
responses(
|
||||
(status = 200, description = "Data wiped from cover art queue", body = super::response::wipe_data_from_coverart_queue::Response),
|
||||
(status = 400, description = "Error wiping the data", body = super::response::wipe_data_from_coverart_queue::Response),
|
||||
(status = 404, description = "Cover art not found", body = super::response::wipe_data_from_coverart_queue::Response)
|
||||
)
|
||||
)]
|
||||
pub async fn wipe_data_from_coverart_queue(
|
||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||
axum::Json(payload): axum::Json<super::request::wipe_data_from_coverart_queue::Request>,
|
||||
) -> (
|
||||
axum::http::StatusCode,
|
||||
axum::Json<super::response::wipe_data_from_coverart_queue::Response>,
|
||||
) {
|
||||
let mut response = super::response::wipe_data_from_coverart_queue::Response::default();
|
||||
let coverart_queue_id = payload.coverart_queue_id;
|
||||
|
||||
match repo::coverart::get_coverart_queue_with_id(&pool, &coverart_queue_id).await {
|
||||
Ok(coverart_queue) => {
|
||||
match repo::coverart::wipe_data(&pool, &coverart_queue.id).await {
|
||||
Ok(id) => {
|
||||
response.message = String::from("Success");
|
||||
response.data.push(id);
|
||||
(axum::http::StatusCode::OK, axum::Json(response))
|
||||
}
|
||||
Err(err) => {
|
||||
response.message = err.to_string();
|
||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
response.message = err.to_string();
|
||||
(axum::http::StatusCode::NOT_FOUND, axum::Json(response))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
pub mod coverart;
|
||||
|
||||
+13
-10
@@ -54,6 +54,7 @@ pub mod init {
|
||||
|
||||
use crate::callers::coverart as coverart_caller;
|
||||
use crate::callers::metadata as metadata_caller;
|
||||
use crate::callers::queue::coverart as coverart_queue_callers;
|
||||
use crate::callers::song as song_caller;
|
||||
use coverart_caller::endpoint as coverart_endpoints;
|
||||
use coverart_caller::response as coverart_responses;
|
||||
@@ -61,6 +62,8 @@ pub mod init {
|
||||
use metadata_caller::response as metadata_responses;
|
||||
use song_caller::endpoint as song_endpoints;
|
||||
use song_caller::response as song_responses;
|
||||
use coverart_queue_callers::endpoint as coverart_queue_endpoints;
|
||||
use coverart_queue_callers::response as coverart_queue_responses;
|
||||
|
||||
mod cors {
|
||||
pub async fn configure_cors() -> tower_http::cors::CorsLayer {
|
||||
@@ -114,16 +117,16 @@ pub mod init {
|
||||
#[openapi(
|
||||
paths(song_endpoints::queue_song, song_endpoints::link_user_id, song_endpoints::fetch_queue_song, song_endpoints::download_flac,
|
||||
song_endpoints::update_song_queue_status, song_endpoints::update_song_queue, song_endpoints::create_metadata, song_endpoints::wipe_data_from_song_queue, song_endpoints::get_songs, song_endpoints::get_all_songs, song_endpoints::stream_song, song_endpoints::download_song,
|
||||
song_endpoints::delete_song, coverart_endpoints::queue, coverart_endpoints::link, coverart_endpoints::fetch_coverart_no_data,
|
||||
coverart_endpoints::fetch_coverart_with_data, coverart_endpoints::create_coverart, coverart_endpoints::wipe_data_from_coverart_queue,
|
||||
song_endpoints::delete_song, coverart_queue_endpoints::queue, coverart_queue_endpoints::link, coverart_queue_endpoints::fetch_coverart_no_data,
|
||||
coverart_queue_endpoints::fetch_coverart_with_data, coverart_endpoints::create_coverart, coverart_queue_endpoints::wipe_data_from_coverart_queue,
|
||||
coverart_endpoints::get_coverart, coverart_endpoints::download_coverart,
|
||||
metadata_endpoints::queue_metadata, metadata_endpoints::fetch_metadata),
|
||||
components(schemas(song_responses::Response, song_responses::link_user_id::Response, song_responses::fetch_queue_song::Response,
|
||||
song_responses::update_status::Response, song_responses::update_song_queue::Response, song_responses::create_metadata::Response,
|
||||
song_responses::wipe_data_from_song_queue::Response, song_responses::get_songs::Response, song_responses::delete_song::Response,
|
||||
coverart_responses::Response, coverart_responses::link::Response, coverart_responses::fetch_coverart_no_data::Response,
|
||||
coverart_responses::fetch_coverart_with_data::Response, coverart_responses::create_coverart::Response,
|
||||
coverart_responses::wipe_data_from_coverart_queue::Response, coverart_responses::get_coverart::Response,
|
||||
coverart_queue_responses::queue::Response, coverart_queue_responses::link::Response, coverart_queue_responses::fetch_coverart_no_data::Response,
|
||||
coverart_queue_responses::fetch_coverart_with_data::Response, coverart_responses::create_coverart::Response,
|
||||
coverart_queue_responses::wipe_data_from_coverart_queue::Response, coverart_responses::get_coverart::Response,
|
||||
metadata_responses::queue_metadata::Response, metadata_responses::fetch_metadata::Response)),
|
||||
tags(
|
||||
(name = "Icarus API", description = "Web API to manage music")
|
||||
@@ -190,31 +193,31 @@ pub mod init {
|
||||
)
|
||||
.route(
|
||||
crate::callers::endpoints::QUEUECOVERART,
|
||||
post(crate::callers::coverart::endpoint::queue).route_layer(
|
||||
post(crate::callers::queue::coverart::endpoint::queue).route_layer(
|
||||
axum::middleware::from_fn(crate::auth::auth::<axum::body::Body>),
|
||||
),
|
||||
)
|
||||
.route(
|
||||
crate::callers::endpoints::QUEUECOVERARTDATA,
|
||||
get(crate::callers::coverart::endpoint::fetch_coverart_with_data).route_layer(
|
||||
get(crate::callers::queue::coverart::endpoint::fetch_coverart_with_data).route_layer(
|
||||
axum::middleware::from_fn(crate::auth::auth::<axum::body::Body>),
|
||||
),
|
||||
)
|
||||
.route(
|
||||
crate::callers::endpoints::QUEUECOVERART,
|
||||
get(crate::callers::coverart::endpoint::fetch_coverart_no_data).route_layer(
|
||||
get(crate::callers::queue::coverart::endpoint::fetch_coverart_no_data).route_layer(
|
||||
axum::middleware::from_fn(crate::auth::auth::<axum::body::Body>),
|
||||
),
|
||||
)
|
||||
.route(
|
||||
crate::callers::endpoints::QUEUECOVERARTLINK,
|
||||
patch(crate::callers::coverart::endpoint::link).route_layer(
|
||||
patch(crate::callers::queue::coverart::endpoint::link).route_layer(
|
||||
axum::middleware::from_fn(crate::auth::auth::<axum::body::Body>),
|
||||
),
|
||||
)
|
||||
.route(
|
||||
crate::callers::endpoints::QUEUECOVERARTDATAWIPE,
|
||||
patch(crate::callers::coverart::endpoint::wipe_data_from_coverart_queue)
|
||||
patch(crate::callers::queue::coverart::endpoint::wipe_data_from_coverart_queue)
|
||||
.route_layer(axum::middleware::from_fn(
|
||||
crate::auth::auth::<axum::body::Body>,
|
||||
)),
|
||||
|
||||
@@ -54,7 +54,7 @@ pub async fn update(
|
||||
pub async fn get_coverart_queue_with_id(
|
||||
pool: &sqlx::PgPool,
|
||||
id: &uuid::Uuid,
|
||||
) -> Result<crate::callers::coverart::CoverArtQueue, sqlx::Error> {
|
||||
) -> Result<crate::callers::queue::coverart::CoverArtQueue, sqlx::Error> {
|
||||
let result = sqlx::query(
|
||||
r#"
|
||||
SELECT id, file_type, song_queue_id FROM "coverartQueue" WHERE id = $1;
|
||||
@@ -68,7 +68,7 @@ pub async fn get_coverart_queue_with_id(
|
||||
});
|
||||
|
||||
match result {
|
||||
Ok(row) => Ok(crate::callers::coverart::CoverArtQueue {
|
||||
Ok(row) => Ok(crate::callers::queue::coverart::CoverArtQueue {
|
||||
id: row
|
||||
.try_get("id")
|
||||
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||
@@ -89,7 +89,7 @@ pub async fn get_coverart_queue_with_id(
|
||||
pub async fn get_coverart_queue_with_song_queue_id(
|
||||
pool: &sqlx::PgPool,
|
||||
song_queue_id: &uuid::Uuid,
|
||||
) -> Result<crate::callers::coverart::CoverArtQueue, sqlx::Error> {
|
||||
) -> Result<crate::callers::queue::coverart::CoverArtQueue, sqlx::Error> {
|
||||
let result = sqlx::query(
|
||||
r#"
|
||||
SELECT id, file_type, song_queue_id FROM "coverartQueue" WHERE song_queue_id = $1;
|
||||
@@ -103,7 +103,7 @@ pub async fn get_coverart_queue_with_song_queue_id(
|
||||
});
|
||||
|
||||
match result {
|
||||
Ok(row) => Ok(crate::callers::coverart::CoverArtQueue {
|
||||
Ok(row) => Ok(crate::callers::queue::coverart::CoverArtQueue {
|
||||
id: row
|
||||
.try_get("id")
|
||||
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||
|
||||
Reference in New Issue
Block a user