From d27eb2dfedcd52eeb6b5578983b3fa41ebd50cda Mon Sep 17 00:00:00 2001 From: blek Date: Wed, 13 Dec 2023 20:14:37 +1000 Subject: [PATCH] make the get_all api method actually work --- filed/src/files/lookup.rs | 30 ++++++------- filed/src/web/api/files/get_all.rs | 68 +++++++++++++++++++++++++----- 2 files changed, 70 insertions(+), 28 deletions(-) diff --git a/filed/src/files/lookup.rs b/filed/src/files/lookup.rs index 3e67233..736980c 100644 --- a/filed/src/files/lookup.rs +++ b/filed/src/files/lookup.rs @@ -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, Box> { let mut conn = self.conn.get_async_connection().await?; - let found: Vec = conn.keys(predicate).await?; - let serialized: Vec = - 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 = conn.keys(predicate).await?; + + let mut data: Vec = 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 diff --git a/filed/src/web/api/files/get_all.rs b/filed/src/web/api/files/get_all.rs index 59735cc..dbf58b7 100644 --- a/filed/src/web/api/files/get_all.rs +++ b/filed/src/web/api/files/get_all.rs @@ -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, Rejection> { +pub async fn get_all(state: SharedState, ip: Option) -> Result, 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, 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 + 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) } \ No newline at end of file