diff --git a/filed/src/web/forms.rs b/filed/src/web/forms.rs index 671a08f..39df7c9 100644 --- a/filed/src/web/forms.rs +++ b/filed/src/web/forms.rs @@ -9,25 +9,34 @@ use askama::Template; use warp::{Filter, reply::Reply, reject::Rejection, filters::multipart::FormData, http::StatusCode}; use futures_util::TryStreamExt; use bytes::BufMut; +use serde::Serialize; + +use crate::files::File; use super::{state::SharedState, pages::BadActionReq, rejection::HttpReject}; +#[derive(Debug, Serialize)] +struct FormElement { + data: Vec, + mime: String +} + pub async fn upload(form: FormData, _state: SharedState) -> Result, Rejection> { - let params: HashMap = form.and_then(|mut field| async move { + let params: HashMap = form.and_then(|mut field| async move { let mut bytes: Vec = vec![]; while let Some(byte) = field.data().await { bytes.put(byte.unwrap()) } - Ok((field.name().into(), String::from_utf8_lossy(&bytes).to_string())) + Ok((field.name().into(), FormElement { data: bytes, mime: field.content_type().unwrap_or("text/plain").to_string() })) }).try_collect() .await .map_err(|err| warp::reject::custom(HttpReject::WarpError(err.into())))?; // check that required fields exist let mut all_exist = true; - let _ = vec!["delmode", "file"].iter().for_each(|x| { + let _ = vec!["delmode", "file", "filename", "password"].iter().for_each(|x| { let field = x.to_string(); if ! params.contains_key(&field) { all_exist = false; @@ -47,6 +56,29 @@ pub async fn upload(form: FormData, _state: SharedState) -> Result { + if String::from_utf8(named.data.clone()) + .map_err(|err| warp::reject::custom(HttpReject::FromUtf8Error(err)))? + .to_string() == "on" { + Some(String::from_utf8(filename.data.clone()).map_err(|err| warp::reject::custom(HttpReject::FromUtf8Error(err)))?) + } else { + None + } + }, + None => None + }, + _state.env.clone() + ).await; + Ok(Box::new(warp::reply::json(¶ms))) } diff --git a/filed/src/web/mod.rs b/filed/src/web/mod.rs index b0af05a..a804160 100644 --- a/filed/src/web/mod.rs +++ b/filed/src/web/mod.rs @@ -32,7 +32,8 @@ pub async fn serve(env: Env) { log::info!("Listening on {}", env.listen.to_string()); let state = SharedState { - redis_cli: crate::db::redis_conn(env.clone()).unwrap() + redis_cli: crate::db::redis_conn(env.clone()).unwrap(), + env: env.clone() }; warp::serve(routes(state)).run(env.listen).await; diff --git a/filed/src/web/rejection.rs b/filed/src/web/rejection.rs index e35816a..467ba15 100644 --- a/filed/src/web/rejection.rs +++ b/filed/src/web/rejection.rs @@ -1,7 +1,10 @@ +use std::string::FromUtf8Error; + #[derive(Debug)] pub enum HttpReject { WarpError(warp::Error), - AskamaError(askama::Error) + AskamaError(askama::Error), + FromUtf8Error(FromUtf8Error) } impl warp::reject::Reject for HttpReject {} \ No newline at end of file diff --git a/filed/src/web/state.rs b/filed/src/web/state.rs index 78f93f6..57bfec7 100644 --- a/filed/src/web/state.rs +++ b/filed/src/web/state.rs @@ -1,7 +1,10 @@ use redis::Client; +use crate::env::Env; + #[derive(Debug, Clone)] pub struct SharedState { - pub redis_cli: Client + pub redis_cli: Client, + pub env: Env }