use crate::extract::FromRequest;
use http::StatusCode;
pub struct Multipart(multer::Multipart<'static>);
impl Multipart {
pub async fn next_field(&mut self) -> Result<Option<multer::Field<'static>>, multer::Error> {
self.0.next_field().await
}
}
impl FromRequest for Multipart {
type Error = (StatusCode, String);
async fn from_request(
parts: &http::request::Parts,
body: bytes::Bytes,
) -> Result<Self, Self::Error> {
let boundary = parts
.headers
.get(http::header::CONTENT_TYPE)
.and_then(|ct| ct.to_str().ok())
.and_then(|ct| multer::parse_boundary(ct).ok())
.ok_or_else(|| {
(
StatusCode::BAD_REQUEST,
"missing or invalid multipart boundary".to_string(),
)
})?;
let stream = futures::stream::once(async move { Ok::<_, std::io::Error>(body) });
let multipart = multer::Multipart::new(stream, boundary);
Ok(Multipart(multipart))
}
}