From 5944d73d6ccc6a625727bfb0a3d9316887f82d09 Mon Sep 17 00:00:00 2001 From: blek Date: Sat, 9 Dec 2023 23:22:33 +1000 Subject: [PATCH] implement the /api/files/delete method --- filed/src/web/api.rs | 5 +-- filed/src/web/api/files.rs | 3 +- filed/src/web/api/files/delete.rs | 57 +++++++++++++++++++++++++++++++ filed/src/web/api/types.rs | 6 ++-- 4 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 filed/src/web/api/files/delete.rs diff --git a/filed/src/web/api.rs b/filed/src/web/api.rs index d07602b..6b958c5 100644 --- a/filed/src/web/api.rs +++ b/filed/src/web/api.rs @@ -1,7 +1,7 @@ use warp::{reply::Reply, reject::Rejection, Filter}; use serde::{Serialize, Deserialize}; -use self::files::get_all::get_all_f; +use self::files::{get_all::get_all_f, delete::delete_f}; use super::state::SharedState; @@ -24,5 +24,6 @@ pub fn get_routes(state: SharedState) -> impl Filter WithStatus { ) } -pub mod get_all; \ No newline at end of file +pub mod get_all; +pub mod delete; \ No newline at end of file diff --git a/filed/src/web/api/files/delete.rs b/filed/src/web/api/files/delete.rs new file mode 100644 index 0000000..b5d5603 --- /dev/null +++ b/filed/src/web/api/files/delete.rs @@ -0,0 +1,57 @@ +use std::collections::HashMap; + +use warp::{reply::{Reply, json}, reject::Rejection, Filter, http::StatusCode}; +use serde::{Serialize, Deserialize}; + +use crate::web::{state::SharedState, rejection::HttpReject, api::types::{ErrorMessage, Error}}; + +use super::{function_disabled_err, check_api_enabled}; + +#[derive(Serialize, Deserialize, Clone, Debug)] +pub struct DeleteFunctionPayload { + pub fid: String +} + +pub async fn delete(state: SharedState, body: DeleteFunctionPayload) -> Result, Rejection> { + if let Err(res) = check_api_enabled(&state) { + return Ok(Box::new(res)); + } + + if state.config.api.delete { + return Ok(Box::new(function_disabled_err())) + } + + let id = body.fid; + let mut file = state.file_mgr.find_by_hash(id.clone()) + .map_err(|x| HttpReject::StringError(x.to_string()))?; + + if let None = file { + file = state.file_mgr.find_by_name(id) + .map_err(|x| HttpReject::StringError(x.to_string()))?; + } + + if let None = file { + return Ok( + Box::new( + warp::reply::with_status( + json( + &ErrorMessage { + error: Error::APIError, + details: Some("No file with that ID was found.".into()) + } + ), + StatusCode::NOT_FOUND + ) + ) + ) + } + + Ok(Box::new(json(&HashMap::<(), ()>::new()))) +} + +pub fn delete_f(state: SharedState) -> impl Filter + Clone { + warp::path!("api" / "files" / "delete") + .map(move || state.clone()) + .and(warp::body::json()) + .and_then(delete) +} \ No newline at end of file diff --git a/filed/src/web/api/types.rs b/filed/src/web/api/types.rs index 717394f..53a86eb 100644 --- a/filed/src/web/api/types.rs +++ b/filed/src/web/api/types.rs @@ -3,7 +3,8 @@ use serde::{Serialize, Deserialize}; #[derive(Debug, Clone, Serialize, Deserialize)] pub enum Error { APIDisabled, - APIFunctionDisabled + APIFunctionDisabled, + APIError } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -17,7 +18,8 @@ impl ErrorMessage { ErrorMessage { details: match error { Error::APIDisabled => Some("API is disabled by the administrator. Please contact them for further details".into()), - Error::APIFunctionDisabled => Some("This API function is disabled by the administrator. Please contact them for further details.".into()) + Error::APIFunctionDisabled => Some("This API function is disabled by the administrator. Please contact them for further details.".into()), + Error::APIError => Some("An error has occured while executing the API request".into()) }, error, }