2019-04-03 21:28:58 +02:00
|
|
|
//! Multipart payload support
|
2019-04-13 23:50:54 +02:00
|
|
|
use actix_web::{dev::Payload, Error, FromRequest, HttpRequest};
|
2019-04-03 21:28:58 +02:00
|
|
|
|
|
|
|
use crate::server::Multipart;
|
|
|
|
|
|
|
|
/// Get request's payload as multipart stream
|
|
|
|
///
|
|
|
|
/// Content-type: multipart/form-data;
|
|
|
|
///
|
|
|
|
/// ## Server example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use futures::{Future, Stream};
|
|
|
|
/// # use futures::future::{ok, result, Either};
|
|
|
|
/// use actix_web::{web, HttpResponse, Error};
|
|
|
|
/// use actix_multipart as mp;
|
|
|
|
///
|
|
|
|
/// fn index(payload: mp::Multipart) -> impl Future<Item = HttpResponse, Error = Error> {
|
|
|
|
/// payload.from_err() // <- get multipart stream for current request
|
2019-04-13 19:11:07 +02:00
|
|
|
/// .and_then(|field| { // <- iterate over multipart items
|
|
|
|
/// // Field in turn is stream of *Bytes* object
|
|
|
|
/// field.from_err()
|
|
|
|
/// .fold((), |_, chunk| {
|
|
|
|
/// println!("-- CHUNK: \n{:?}", std::str::from_utf8(&chunk));
|
|
|
|
/// Ok::<_, Error>(())
|
|
|
|
/// })
|
2019-04-03 21:28:58 +02:00
|
|
|
/// })
|
|
|
|
/// .fold((), |_, _| Ok::<_, Error>(()))
|
|
|
|
/// .map(|_| HttpResponse::Ok().into())
|
|
|
|
/// }
|
|
|
|
/// # fn main() {}
|
|
|
|
/// ```
|
2019-04-13 23:50:54 +02:00
|
|
|
impl FromRequest for Multipart {
|
2019-04-03 21:28:58 +02:00
|
|
|
type Error = Error;
|
|
|
|
type Future = Result<Multipart, Error>;
|
|
|
|
|
|
|
|
#[inline]
|
2019-04-13 23:50:54 +02:00
|
|
|
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
|
2019-04-07 23:43:07 +02:00
|
|
|
Ok(Multipart::new(req.headers(), payload.take()))
|
2019-04-03 21:28:58 +02:00
|
|
|
}
|
|
|
|
}
|