make the get_all api method actually work

This commit is contained in:
blek 2023-12-13 20:14:37 +10:00
parent d277fd451e
commit d27eb2dfed
Signed by: blek
GPG Key ID: 14546221E3595D0C
2 changed files with 70 additions and 28 deletions

View File

@ -2,6 +2,7 @@
use std::error::Error;
use redis::{Client, Commands, AsyncCommands, Connection};
use tokio::task::JoinSet;
use crate::env::Env;
@ -25,23 +26,18 @@ impl FileManager {
async fn find_all(self: &Self, predicate: String) -> Result<Vec<File>, Box<dyn Error>> {
let mut conn = self.conn.get_async_connection().await?;
let found: Vec<String> = conn.keys(predicate).await?;
let serialized: Vec<File> =
found.iter()
.map(|x| {
let result = serde_json::from_str(&x);
match result {
Ok(x) => Some(x),
Err(err) => {
log::error!("Error while serializing {x}: {:?}", err);
None
}
}
})
.filter(|x| x.is_some())
.map(|x| x.unwrap())
.collect();
Ok(serialized)
let keys: Vec<String> = conn.keys(predicate).await?;
let mut data: Vec<File> = vec![];
for key in keys.iter() {
let raw: String = conn.get(key.clone()).await?;
let mut parsed: File = serde_json::from_str(raw.as_str())?;
data.push(parsed);
}
Ok(data)
}
/// Filter options

View File

@ -1,10 +1,13 @@
use warp::{reply::{Reply, json}, reject::Rejection, Filter};
use std::net::IpAddr;
use crate::web::{state::SharedState, rejection::HttpReject};
use warp::{reply::{Reply, json, with_status}, reject::Rejection, Filter, http::StatusCode};
use warp_real_ip::real_ip;
use crate::web::{state::SharedState, rejection::HttpReject, api::types::{ErrorMessage, Error}};
use super::{check_api_enabled, function_disabled_err};
pub async fn get_all(state: SharedState) -> Result<Box<dyn Reply>, Rejection> {
pub async fn get_all(state: SharedState, ip: Option<IpAddr>) -> Result<Box<dyn Reply>, Rejection> {
if let Err(res) = check_api_enabled(&state) {
return Ok(Box::new(res))
}
@ -13,20 +16,63 @@ pub async fn get_all(state: SharedState) -> Result<Box<dyn Reply>, Rejection> {
return Ok(Box::new(function_disabled_err()))
}
Ok(
Box::new(
json(
&state.file_mgr.get_all(true, true)
.await
.map_err(|x| x.to_string())
.map_err(|x| HttpReject::StringError(x))?
let found =
state.file_mgr.get_all(true, true)
.await
.map_err(|x| x.to_string());
if let Err(err) = found {
return Ok(
Box::new(
with_status(
json(
&ErrorMessage {
error: Error::APIError,
details: Some(
format!("Error while getting all files: {err}")
)
}
),
StatusCode::INTERNAL_SERVER_ERROR
)
)
);
} else {
let mut found = found.unwrap();
if state.config.api.get_all_own_only {
found = found
.iter()
.filter(
|x| {
if let Some(owner) = x.uploader_ip {
if let Some(caller) = ip {
println!("{owner} {caller}");
return owner == caller
}
}
false
}
).map(|x| x.clone()).collect();
}
Ok(
Box::new(
json(
&found
)
)
)
)
}
}
pub fn get_all_f(state: SharedState) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
let proxy = state.env.proxy_addr;
warp::path!("api" / "files" / "get_all")
.map(move || state.clone())
.and(real_ip(vec![proxy]))
.and_then(get_all)
}