bfile/filed/src/web/forms.rs

34 lines
977 B
Rust
Raw Normal View History

2023-09-30 11:26:47 +02:00
/*
forms.rs - All the forms
*/
use std::collections::HashMap;
2023-10-01 02:14:35 +02:00
use warp::{Filter, reply::Reply, reject::Rejection, filters::multipart::FormData};
2023-09-30 11:26:47 +02:00
use futures_util::TryStreamExt;
use bytes::BufMut;
2023-10-01 02:14:35 +02:00
use super::state::SharedState;
pub async fn upload(form: FormData, _state: SharedState) -> Result<Box<dyn Reply>, Rejection> {
2023-09-30 11:26:47 +02:00
let params: HashMap<String, String> = form.and_then(|mut field| async move {
let mut bytes: Vec<u8> = vec![];
while let Some(byte) = field.data().await {
bytes.put(byte.unwrap())
}
Ok((field.name().into(), String::from_utf8_lossy(&bytes).to_string()))
}).try_collect().await.unwrap();
Ok(Box::new(warp::reply::json(&params)))
}
2023-10-01 02:14:35 +02:00
pub fn get_routes(state: SharedState) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
2023-09-30 11:26:47 +02:00
warp::post().and(
2023-10-01 02:14:35 +02:00
warp::multipart::form()
.and(warp::any().map(move || state.clone()))
.and_then(upload)
2023-09-30 11:26:47 +02:00
)
}