implement the /api/files/delete method

This commit is contained in:
blek 2023-12-09 23:22:33 +10:00
parent 103c7c4c8d
commit 5944d73d6c
Signed by: blek
GPG Key ID: 14546221E3595D0C
4 changed files with 66 additions and 5 deletions

View File

@ -1,7 +1,7 @@
use warp::{reply::Reply, reject::Rejection, Filter}; use warp::{reply::Reply, reject::Rejection, Filter};
use serde::{Serialize, Deserialize}; 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; use super::state::SharedState;
@ -24,5 +24,6 @@ pub fn get_routes(state: SharedState) -> impl Filter<Extract = impl Reply, Error
warp::path!("api") warp::path!("api")
.and(warp::path::end()) .and(warp::path::end())
.map(api_root) .map(api_root)
.or(get_all_f(state)) .or(get_all_f(state.clone()))
.or(delete_f(state))
} }

View File

@ -23,4 +23,5 @@ fn function_disabled_err() -> WithStatus<Json> {
) )
} }
pub mod get_all; pub mod get_all;
pub mod delete;

View File

@ -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<Box<dyn Reply>, 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<Extract = impl Reply, Error = Rejection> + Clone {
warp::path!("api" / "files" / "delete")
.map(move || state.clone())
.and(warp::body::json())
.and_then(delete)
}

View File

@ -3,7 +3,8 @@ use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Error { pub enum Error {
APIDisabled, APIDisabled,
APIFunctionDisabled APIFunctionDisabled,
APIError
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
@ -17,7 +18,8 @@ impl ErrorMessage {
ErrorMessage { ErrorMessage {
details: match error { details: match error {
Error::APIDisabled => Some("API is disabled by the administrator. Please contact them for further details".into()), 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, error,
} }