1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use crate::{
types::{Form, Value},
upload::handle_multipart,
};
use actix_web::{dev::Payload, FromRequest, HttpRequest, ResponseError};
use std::{future::Future, pin::Pin};
pub trait FormData {
type Item: 'static;
type Error: ResponseError + 'static;
fn form(req: &HttpRequest) -> Form<Self::Item, Self::Error>;
fn extract(value: Value<Self::Item>) -> Result<Self, Self::Error>
where
Self: Sized;
}
pub struct Multipart<T>(pub T);
impl<T> FromRequest for Multipart<T>
where
T: FormData,
{
type Error = actix_web::Error;
type Future = Pin<Box<dyn Future<Output = Result<Self, Self::Error>> + 'static>>;
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
let multipart = actix_multipart::Multipart::new(req.headers(), payload.take());
let form = T::form(req);
Box::pin(async move {
let uploaded = match handle_multipart(multipart, &form).await {
Ok(Ok(uploaded)) => uploaded,
Ok(Err(e)) => return Err(e.into()),
Err(e) => {
if let Some(f) = form.transform_error {
return Err((f)(e));
} else {
return Err(e.into());
}
}
};
Ok(Multipart(T::extract(uploaded)?))
})
}
}