Bot integration fixes
parent
136405d19b
commit
1b1f6a3eee
|
@ -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
|
||||||
|
|
|
@ -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();
|
||||||
|
|
|
@ -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) => {
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
error!("IO error occurred handling request {}", e);
|
||||||
"IO Error Has Occurred!".to_string(),
|
(
|
||||||
),
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
"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 => (
|
||||||
|
|
|
@ -29,14 +29,18 @@ 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> {
|
||||||
if let Some(album_name) = &album_query.album_name {
|
db.filter(|_, album: &Album| {
|
||||||
db.filter(|_, album: &Album| album.album_name == *album_name)
|
if let Some(album_name) = &album_query.album_name {
|
||||||
.unwrap()
|
if album_name != &album.album_name && !album.aliases.contains(album_name) {
|
||||||
.next()
|
return false;
|
||||||
} else {
|
}
|
||||||
None
|
}
|
||||||
}
|
|
||||||
|
true
|
||||||
|
})
|
||||||
|
.unwrap()
|
||||||
|
.collect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue