make the get_all api method actually work

This commit is contained in:
blek 2023-12-13 20:14:37 +10:00 committed by blek! Git
parent 46fbbee459
commit 3cb1fd978f
2 changed files with 70 additions and 28 deletions

View File

@ -2,6 +2,7 @@
use std::error::Error; use std::error::Error;
use redis::{Client, Commands, AsyncCommands, Connection}; use redis::{Client, Commands, AsyncCommands, Connection};
use tokio::task::JoinSet;
use crate::env::Env; use crate::env::Env;
@ -25,23 +26,18 @@ impl FileManager {
async fn find_all(self: &Self, predicate: String) -> Result<Vec<File>, Box<dyn Error>> { async fn find_all(self: &Self, predicate: String) -> Result<Vec<File>, Box<dyn Error>> {
let mut conn = self.conn.get_async_connection().await?; let mut conn = self.conn.get_async_connection().await?;
let found: Vec<String> = conn.keys(predicate).await?; let keys: Vec<String> = conn.keys(predicate).await?;
let serialized: Vec<File> =
found.iter() let mut data: Vec<File> = vec![];
.map(|x| {
let result = serde_json::from_str(&x); for key in keys.iter() {
match result { let raw: String = conn.get(key.clone()).await?;
Ok(x) => Some(x),
Err(err) => { let mut parsed: File = serde_json::from_str(raw.as_str())?;
log::error!("Error while serializing {x}: {:?}", err); data.push(parsed);
None
} }
}
}) Ok(data)
.filter(|x| x.is_some())
.map(|x| x.unwrap())
.collect();
Ok(serialized)
} }
/// Filter options /// 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}; 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) { if let Err(res) = check_api_enabled(&state) {
return Ok(Box::new(res)) 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())) return Ok(Box::new(function_disabled_err()))
} }
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( Ok(
Box::new( Box::new(
json( json(
&state.file_mgr.get_all(true, true) &found
.await
.map_err(|x| x.to_string())
.map_err(|x| HttpReject::StringError(x))?
) )
) )
) )
} }
}
pub fn get_all_f(state: SharedState) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone { 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") warp::path!("api" / "files" / "get_all")
.map(move || state.clone()) .map(move || state.clone())
.and(real_ip(vec![proxy]))
.and_then(get_all) .and_then(get_all)
} }