use crate::{Constraints, Multipart};
use hyper::{header, Request};
pub trait RequestMultipartExt {
fn into_multipart(self) -> routerify::Result<Multipart<'static>>;
fn into_multipart_with_constraints(self, constraints: Constraints) -> routerify::Result<Multipart<'static>>;
}
impl RequestMultipartExt for Request<hyper::Body> {
fn into_multipart(self) -> routerify::Result<Multipart<'static>> {
let boundary = self
.headers()
.get(header::CONTENT_TYPE)
.and_then(|val| val.to_str().ok())
.and_then(|val| multer::parse_boundary(val).ok());
if let Some(boundary) = boundary {
Ok(Multipart::new(self.into_body(), boundary))
} else {
Err(Box::new(routerify::Error::new(
"Content-Type is not multipart/form-data",
)))
}
}
fn into_multipart_with_constraints(self, constraints: Constraints) -> routerify::Result<Multipart<'static>> {
let boundary = self
.headers()
.get(header::CONTENT_TYPE)
.and_then(|val| val.to_str().ok())
.and_then(|val| multer::parse_boundary(val).ok());
if let Some(boundary) = boundary {
Ok(Multipart::with_constraints(self.into_body(), boundary, constraints))
} else {
Err(Box::new(routerify::Error::new(
"Content-Type is not multipart/form-data",
)))
}
}
}