Make form construction fallible
This commit is contained in:
parent
5f38d493c6
commit
1c88a70021
|
@ -11,8 +11,8 @@ impl FormData for UploadedContent {
|
||||||
type Item = ();
|
type Item = ();
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn form(_: &HttpRequest) -> Form<Self::Item, Self::Error> {
|
fn form(_: &HttpRequest) -> Result<Form<Self::Item, Self::Error>, Self::Error> {
|
||||||
Form::new()
|
Ok(Form::new()
|
||||||
.field("Hey", Field::text())
|
.field("Hey", Field::text())
|
||||||
.field(
|
.field(
|
||||||
"Hi",
|
"Hi",
|
||||||
|
@ -29,7 +29,7 @@ impl FormData for UploadedContent {
|
||||||
}
|
}
|
||||||
Ok(()) as Result<(), Error>
|
Ok(()) as Result<(), Error>
|
||||||
})),
|
})),
|
||||||
)
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extract(value: Value<Self::Item>) -> Result<Self, Self::Error>
|
fn extract(value: Value<Self::Item>) -> Result<Self, Self::Error>
|
||||||
|
|
|
@ -61,11 +61,11 @@ impl FormData for UploadedContent {
|
||||||
type Item = PathBuf;
|
type Item = PathBuf;
|
||||||
type Error = Errors;
|
type Error = Errors;
|
||||||
|
|
||||||
fn form(req: &HttpRequest) -> Form<Self::Item, Self::Error> {
|
fn form(req: &HttpRequest) -> Result<Form<Self::Item, Self::Error>, Self::Error> {
|
||||||
let file_count = req.app_data::<Arc<AtomicUsize>>().expect("Set config");
|
let file_count = req.app_data::<Arc<AtomicUsize>>().expect("Set config");
|
||||||
let file_count = Arc::clone(file_count);
|
let file_count = Arc::clone(file_count);
|
||||||
|
|
||||||
Form::new()
|
Ok(Form::new()
|
||||||
.field("Hey", Field::text())
|
.field("Hey", Field::text())
|
||||||
.field(
|
.field(
|
||||||
"Hi",
|
"Hi",
|
||||||
|
@ -85,7 +85,7 @@ impl FormData for UploadedContent {
|
||||||
.map_err(Errors::from)
|
.map_err(Errors::from)
|
||||||
}
|
}
|
||||||
})),
|
})),
|
||||||
)
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extract(value: Value<Self::Item>) -> Result<Self, Self::Error>
|
fn extract(value: Value<Self::Item>) -> Result<Self, Self::Error>
|
||||||
|
|
|
@ -9,7 +9,7 @@ pub trait FormData {
|
||||||
type Item: 'static;
|
type Item: 'static;
|
||||||
type Error: ResponseError + 'static;
|
type Error: ResponseError + 'static;
|
||||||
|
|
||||||
fn form(req: &HttpRequest) -> Form<Self::Item, Self::Error>;
|
fn form(req: &HttpRequest) -> Result<Form<Self::Item, Self::Error>, Self::Error>;
|
||||||
|
|
||||||
fn extract(value: Value<Self::Item>) -> Result<Self, Self::Error>
|
fn extract(value: Value<Self::Item>) -> Result<Self, Self::Error>
|
||||||
where
|
where
|
||||||
|
@ -27,9 +27,11 @@ where
|
||||||
|
|
||||||
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
|
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
|
||||||
let multipart = actix_multipart::Multipart::new(req.headers(), payload.take());
|
let multipart = actix_multipart::Multipart::new(req.headers(), payload.take());
|
||||||
let form = Rc::new(T::form(req));
|
let res = T::form(req);
|
||||||
|
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
|
let form = Rc::new(res?);
|
||||||
|
|
||||||
let uploaded = match handle_multipart(multipart, Rc::clone(&form)).await {
|
let uploaded = match handle_multipart(multipart, Rc::clone(&form)).await {
|
||||||
Ok(Ok(uploaded)) => uploaded,
|
Ok(Ok(uploaded)) => uploaded,
|
||||||
Ok(Err(e)) => return Err(e.into()),
|
Ok(Err(e)) => return Err(e.into()),
|
||||||
|
|
|
@ -38,8 +38,8 @@
|
||||||
//! type Item = ();
|
//! type Item = ();
|
||||||
//! type Error = Error;
|
//! type Error = Error;
|
||||||
//!
|
//!
|
||||||
//! fn form(_: &HttpRequest) -> Form<Self::Item, Self::Error> {
|
//! fn form(_: &HttpRequest) -> Result<Form<Self::Item, Self::Error>, Self::Error> {
|
||||||
//! Form::new()
|
//! Ok(Form::new()
|
||||||
//! .field("Hey", Field::text())
|
//! .field("Hey", Field::text())
|
||||||
//! .field(
|
//! .field(
|
||||||
//! "Hi",
|
//! "Hi",
|
||||||
|
@ -56,7 +56,7 @@
|
||||||
//! }
|
//! }
|
||||||
//! Ok(()) as Result<(), Error>
|
//! Ok(()) as Result<(), Error>
|
||||||
//! })),
|
//! })),
|
||||||
//! )
|
//! ))
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! fn extract(value: Value<Self::Item>) -> Result<Self, Self::Error>
|
//! fn extract(value: Value<Self::Item>) -> Result<Self, Self::Error>
|
||||||
|
|
|
@ -281,6 +281,8 @@ where
|
||||||
|
|
||||||
if !stream_error {
|
if !stream_error {
|
||||||
while let Some(res) = m.next().await {
|
while let Some(res) = m.next().await {
|
||||||
|
tracing::trace!("draining multipart field");
|
||||||
|
|
||||||
if let Ok(field) = res {
|
if let Ok(field) = res {
|
||||||
let mut stream = field.into_streamer();
|
let mut stream = field.into_streamer();
|
||||||
while stream.next().await.is_some() {
|
while stream.next().await.is_some() {
|
||||||
|
|
Loading…
Reference in a new issue