actually save the files when submitted
This commit is contained in:
parent
381b8b04b7
commit
9b3bfb6407
|
@ -9,25 +9,34 @@ use askama::Template;
|
||||||
use warp::{Filter, reply::Reply, reject::Rejection, filters::multipart::FormData, http::StatusCode};
|
use warp::{Filter, reply::Reply, reject::Rejection, filters::multipart::FormData, http::StatusCode};
|
||||||
use futures_util::TryStreamExt;
|
use futures_util::TryStreamExt;
|
||||||
use bytes::BufMut;
|
use bytes::BufMut;
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
use crate::files::File;
|
||||||
|
|
||||||
use super::{state::SharedState, pages::BadActionReq, rejection::HttpReject};
|
use super::{state::SharedState, pages::BadActionReq, rejection::HttpReject};
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize)]
|
||||||
|
struct FormElement {
|
||||||
|
data: Vec<u8>,
|
||||||
|
mime: String
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn upload(form: FormData, _state: SharedState) -> Result<Box<dyn Reply>, Rejection> {
|
pub async fn upload(form: FormData, _state: SharedState) -> Result<Box<dyn Reply>, Rejection> {
|
||||||
|
|
||||||
let params: HashMap<String, String> = form.and_then(|mut field| async move {
|
let params: HashMap<String, FormElement> = form.and_then(|mut field| async move {
|
||||||
let mut bytes: Vec<u8> = vec![];
|
let mut bytes: Vec<u8> = vec![];
|
||||||
while let Some(byte) = field.data().await {
|
while let Some(byte) = field.data().await {
|
||||||
bytes.put(byte.unwrap())
|
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()
|
}).try_collect()
|
||||||
.await
|
.await
|
||||||
.map_err(|err| warp::reject::custom(HttpReject::WarpError(err.into())))?;
|
.map_err(|err| warp::reject::custom(HttpReject::WarpError(err.into())))?;
|
||||||
|
|
||||||
// check that required fields exist
|
// check that required fields exist
|
||||||
let mut all_exist = true;
|
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();
|
let field = x.to_string();
|
||||||
if ! params.contains_key(&field) {
|
if ! params.contains_key(&field) {
|
||||||
all_exist = false;
|
all_exist = false;
|
||||||
|
@ -47,6 +56,29 @@ pub async fn upload(form: FormData, _state: SharedState) -> Result<Box<dyn Reply
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let data = params.get("file").unwrap();
|
||||||
|
let delmode = params.get("delmode").unwrap();
|
||||||
|
let named = params.get("named");
|
||||||
|
let filename = params.get("filename").unwrap();
|
||||||
|
|
||||||
|
let file = File::create(
|
||||||
|
data.data.clone(),
|
||||||
|
data.mime.clone(),
|
||||||
|
match named {
|
||||||
|
Some(named) => {
|
||||||
|
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)))
|
Ok(Box::new(warp::reply::json(¶ms)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,7 +32,8 @@ pub async fn serve(env: Env) {
|
||||||
log::info!("Listening on {}", env.listen.to_string());
|
log::info!("Listening on {}", env.listen.to_string());
|
||||||
|
|
||||||
let state = SharedState {
|
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;
|
warp::serve(routes(state)).run(env.listen).await;
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
|
use std::string::FromUtf8Error;
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum HttpReject {
|
pub enum HttpReject {
|
||||||
WarpError(warp::Error),
|
WarpError(warp::Error),
|
||||||
AskamaError(askama::Error)
|
AskamaError(askama::Error),
|
||||||
|
FromUtf8Error(FromUtf8Error)
|
||||||
}
|
}
|
||||||
impl warp::reject::Reject for HttpReject {}
|
impl warp::reject::Reject for HttpReject {}
|
|
@ -1,7 +1,10 @@
|
||||||
|
|
||||||
use redis::Client;
|
use redis::Client;
|
||||||
|
|
||||||
|
use crate::env::Env;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct SharedState {
|
pub struct SharedState {
|
||||||
pub redis_cli: Client
|
pub redis_cli: Client,
|
||||||
|
pub env: Env
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue