Bot integration fixes

main
Joey Hines 2024-02-11 16:37:46 -07:00
parent 136405d19b
commit 1b1f6a3eee
Signed by: joeyahines
GPG Key ID: 995E531F7A569DDB
4 changed files with 51 additions and 18 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "pic_ox" name = "pic_ox"
version = "0.1.0" version = "0.1.1"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -50,9 +50,18 @@ async fn get_album(
Ok(Response(album)) Ok(Response(album))
} }
async fn query_images( async fn get_image(
image_query: Query<ImageQuery>, image_id: Path<u64>,
State(context): State<PicContext>, State(context): State<PicContext>,
) -> Result<Response<Image>, PicOxError> {
let image = context.db.get::<Image>(*image_id)?;
Ok(Response(image))
}
async fn query_images(
State(context): State<PicContext>,
Json(image_query): Json<ImageQuery>,
) -> Result<Response<Vec<Image>>, PicOxError> { ) -> Result<Response<Vec<Image>>, PicOxError> {
let album_id = if let Some(album) = &image_query.album { let album_id = if let Some(album) = &image_query.album {
Some( Some(
@ -62,6 +71,7 @@ async fn query_images(
album_name: Some(album.to_string()), album_name: Some(album.to_string()),
}, },
) )
.first()
.ok_or(PicOxError::AlbumNotFound)? .ok_or(PicOxError::AlbumNotFound)?
.id() .id()
.unwrap(), .unwrap(),
@ -150,8 +160,22 @@ async fn add_image(
let user = headers.get("user").unwrap().to_str().unwrap(); let user = headers.get("user").unwrap().to_str().unwrap();
let user: u64 = user.parse().unwrap(); let user: u64 = user.parse().unwrap();
let mut album = let album = Album::find_album_by_query(&context.db, metadata.clone().unwrap().album)
Album::find_album_by_query(&context.db, metadata.clone().unwrap().album).unwrap(); .first()
.cloned();
let mut album = if let Some(album) = album {
album
} else {
let name = metadata
.clone()
.unwrap()
.album
.album_name
.ok_or(PicOxError::AlbumNotFound)?;
let album = Album::new(&name, Vec::new(), user);
context.db.insert::<Album>(album)?
};
let mut store_manager = context.store_manager.write().await; let mut store_manager = context.store_manager.write().await;
let img = store_manager let img = store_manager
@ -181,7 +205,7 @@ async fn add_image(
async fn query_album( async fn query_album(
album_query: Query<AlbumQuery>, album_query: Query<AlbumQuery>,
State(context): State<PicContext>, State(context): State<PicContext>,
) -> Result<Response<Option<Album>>, PicOxError> { ) -> Result<Response<Vec<Album>>, PicOxError> {
let resp = Album::find_album_by_query(&context.db, album_query.0); let resp = Album::find_album_by_query(&context.db, album_query.0);
Ok(Response(resp)) Ok(Response(resp))
} }
@ -233,6 +257,7 @@ pub async fn run_picox(db: Database, config: PicOxConfig) {
.route("/api/album/:id", get(get_album)) .route("/api/album/:id", get(get_album))
.route("/api/album/", get(query_album)) .route("/api/album/", get(query_album))
.route("/api/image/", get(query_images)) .route("/api/image/", get(query_images))
.route("/api/image/:id", get(get_image))
.with_state(context); .with_state(context);
let listener = tokio::net::TcpListener::bind(&config.host).await.unwrap(); let listener = tokio::net::TcpListener::bind(&config.host).await.unwrap();

View File

@ -4,6 +4,7 @@ use axum::http::StatusCode;
use axum::response::IntoResponse; use axum::response::IntoResponse;
use axum::Json; use axum::Json;
use axum_macros::FromRequest; use axum_macros::FromRequest;
use log::error;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::sync::Arc; use std::sync::Arc;
@ -92,10 +93,13 @@ impl IntoResponse for PicOxError {
StoreError::InvalidFile => (StatusCode::BAD_REQUEST, err.to_string()), StoreError::InvalidFile => (StatusCode::BAD_REQUEST, err.to_string()),
StoreError::OutOfStorage => (StatusCode::INSUFFICIENT_STORAGE, err.to_string()), StoreError::OutOfStorage => (StatusCode::INSUFFICIENT_STORAGE, err.to_string()),
StoreError::ImageTooBig => (StatusCode::UNAUTHORIZED, err.to_string()), StoreError::ImageTooBig => (StatusCode::UNAUTHORIZED, err.to_string()),
StoreError::IOError(_) => ( StoreError::IOError(e) => {
error!("IO error occurred handling request {}", e);
(
StatusCode::INTERNAL_SERVER_ERROR, StatusCode::INTERNAL_SERVER_ERROR,
"IO Error Has Occurred!".to_string(), "IO Error Has Occurred!".to_string(),
), )
}
}, },
PicOxError::DbError(err) => (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()), PicOxError::DbError(err) => (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()),
PicOxError::AlbumNotFound => ( PicOxError::AlbumNotFound => (

View File

@ -29,15 +29,19 @@ impl Album {
} }
} }
pub fn find_album_by_query(db: &Database, album_query: AlbumQuery) -> Option<Self> { pub fn find_album_by_query(db: &Database, album_query: AlbumQuery) -> Vec<Self> {
db.filter(|_, album: &Album| {
if let Some(album_name) = &album_query.album_name { if let Some(album_name) = &album_query.album_name {
db.filter(|_, album: &Album| album.album_name == *album_name) if album_name != &album.album_name && !album.aliases.contains(album_name) {
.unwrap() return false;
.next()
} else {
None
} }
} }
true
})
.unwrap()
.collect()
}
} }
impl JdbModel for Album { impl JdbModel for Album {