119 lines
3.2 KiB
Rust
119 lines
3.2 KiB
Rust
|
use crate::state::Context;
|
||
|
use crate::storage_manager::StoreError;
|
||
|
use axum::http::StatusCode;
|
||
|
use axum::response::IntoResponse;
|
||
|
use axum::Json;
|
||
|
use axum_macros::FromRequest;
|
||
|
use serde::{Deserialize, Serialize};
|
||
|
use std::sync::Arc;
|
||
|
|
||
|
pub type PicContext = Arc<Context>;
|
||
|
|
||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
|
pub struct CreateAlbum {
|
||
|
pub album_name: String,
|
||
|
}
|
||
|
|
||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
|
pub struct AlbumQuery {
|
||
|
pub album_name: Option<String>,
|
||
|
}
|
||
|
|
||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
|
pub struct AddImage {
|
||
|
pub album: AlbumQuery,
|
||
|
pub tags: Vec<String>,
|
||
|
}
|
||
|
|
||
|
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||
|
pub enum ImageSort {
|
||
|
Random,
|
||
|
DateAscending,
|
||
|
DateDescending,
|
||
|
#[default]
|
||
|
None,
|
||
|
}
|
||
|
|
||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
|
pub struct ImageQuery {
|
||
|
pub album: Option<String>,
|
||
|
#[serde(default)]
|
||
|
pub tags: Vec<String>,
|
||
|
#[serde(default)]
|
||
|
pub order: ImageSort,
|
||
|
#[serde(default)]
|
||
|
pub limit: usize,
|
||
|
}
|
||
|
|
||
|
#[derive(FromRequest)]
|
||
|
#[from_request(via(axum::Json), rejection(PicOxError))]
|
||
|
pub struct Response<T>(pub T);
|
||
|
|
||
|
impl<T> IntoResponse for Response<T>
|
||
|
where
|
||
|
Json<T>: IntoResponse,
|
||
|
{
|
||
|
fn into_response(self) -> axum::response::Response {
|
||
|
Json(self.0).into_response()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[allow(dead_code)]
|
||
|
pub enum PicOxError {
|
||
|
StoreError(StoreError),
|
||
|
DbError(j_db::error::JDbError),
|
||
|
AlbumNotFound,
|
||
|
ImageNotFound,
|
||
|
TokenInvalid,
|
||
|
NoUserInHeader,
|
||
|
}
|
||
|
|
||
|
impl From<StoreError> for PicOxError {
|
||
|
fn from(value: StoreError) -> Self {
|
||
|
Self::StoreError(value)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl From<j_db::error::JDbError> for PicOxError {
|
||
|
fn from(value: j_db::error::JDbError) -> Self {
|
||
|
Self::DbError(value)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[derive(Serialize)]
|
||
|
pub struct ErrorResponse {
|
||
|
pub message: String,
|
||
|
}
|
||
|
|
||
|
impl IntoResponse for PicOxError {
|
||
|
fn into_response(self) -> axum::response::Response {
|
||
|
let (status, message) = match self {
|
||
|
PicOxError::StoreError(err) => match err {
|
||
|
StoreError::InvalidFile => (StatusCode::BAD_REQUEST, err.to_string()),
|
||
|
StoreError::OutOfStorage => (StatusCode::INSUFFICIENT_STORAGE, err.to_string()),
|
||
|
StoreError::ImageTooBig => (StatusCode::UNAUTHORIZED, err.to_string()),
|
||
|
StoreError::IOError(_) => (
|
||
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||
|
"IO Error Has Occurred!".to_string(),
|
||
|
),
|
||
|
},
|
||
|
PicOxError::DbError(err) => (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()),
|
||
|
PicOxError::AlbumNotFound => (
|
||
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||
|
"No album found!".to_string(),
|
||
|
),
|
||
|
PicOxError::ImageNotFound => (
|
||
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||
|
"Image not found".to_string(),
|
||
|
),
|
||
|
PicOxError::TokenInvalid => (StatusCode::UNAUTHORIZED, "Token is invalid".to_string()),
|
||
|
PicOxError::NoUserInHeader => (
|
||
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||
|
"User not found in header".to_string(),
|
||
|
),
|
||
|
};
|
||
|
|
||
|
(status, Response(ErrorResponse { message })).into_response()
|
||
|
}
|
||
|
}
|