Remove config from FormData trait
This commit is contained in:
parent
c5265d286e
commit
3525bcd09c
|
@ -1,18 +1,17 @@
|
||||||
use actix_form_data::{Error, Field, Form, FormData, Multipart, Value};
|
use actix_form_data::{Error, Field, Form, FormData, Multipart, Value};
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
web::{post, resource},
|
web::{post, resource},
|
||||||
App, HttpResponse, HttpServer,
|
App, HttpRequest, HttpResponse, HttpServer,
|
||||||
};
|
};
|
||||||
use futures_util::stream::StreamExt;
|
use futures_util::stream::StreamExt;
|
||||||
|
|
||||||
struct UploadedContent(Value<()>);
|
struct UploadedContent(Value<()>);
|
||||||
|
|
||||||
impl FormData for UploadedContent {
|
impl FormData for UploadedContent {
|
||||||
type Config = ();
|
|
||||||
type Item = ();
|
type Item = ();
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn form(_: Option<&Self::Config>) -> Form<Self::Item, Self::Error> {
|
fn form(_: &HttpRequest) -> Form<Self::Item, Self::Error> {
|
||||||
Form::new()
|
Form::new()
|
||||||
.field("Hey", Field::text())
|
.field("Hey", Field::text())
|
||||||
.field(
|
.field(
|
||||||
|
|
|
@ -3,7 +3,7 @@ use actix_web::{
|
||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
middleware::Logger,
|
middleware::Logger,
|
||||||
web::{post, resource, Bytes},
|
web::{post, resource, Bytes},
|
||||||
App, HttpResponse, HttpServer, ResponseError,
|
App, HttpRequest, HttpResponse, HttpServer, ResponseError,
|
||||||
};
|
};
|
||||||
use futures_util::stream::{Stream, StreamExt, TryStreamExt};
|
use futures_util::stream::{Stream, StreamExt, TryStreamExt};
|
||||||
use std::{
|
use std::{
|
||||||
|
@ -58,12 +58,12 @@ impl ResponseError for Errors {
|
||||||
struct UploadedContent(Value<PathBuf>);
|
struct UploadedContent(Value<PathBuf>);
|
||||||
|
|
||||||
impl FormData for UploadedContent {
|
impl FormData for UploadedContent {
|
||||||
type Config = Arc<AtomicUsize>;
|
|
||||||
type Item = PathBuf;
|
type Item = PathBuf;
|
||||||
type Error = Errors;
|
type Error = Errors;
|
||||||
|
|
||||||
fn form(file_count: Option<&Self::Config>) -> Form<Self::Item, Self::Error> {
|
fn form(req: &HttpRequest) -> Form<Self::Item, Self::Error> {
|
||||||
let file_count = Arc::clone(file_count.expect("Set config"));
|
let file_count = req.app_data::<Arc<AtomicUsize>>().expect("Set config");
|
||||||
|
let file_count = Arc::clone(file_count);
|
||||||
|
|
||||||
Form::new()
|
Form::new()
|
||||||
.field("Hey", Field::text())
|
.field("Hey", Field::text())
|
||||||
|
|
|
@ -6,11 +6,10 @@ use actix_web::{dev::Payload, FromRequest, HttpRequest, ResponseError};
|
||||||
use std::{future::Future, pin::Pin};
|
use std::{future::Future, pin::Pin};
|
||||||
|
|
||||||
pub trait FormData {
|
pub trait FormData {
|
||||||
type Config: 'static;
|
|
||||||
type Item: 'static;
|
type Item: 'static;
|
||||||
type Error: ResponseError + 'static;
|
type Error: ResponseError + 'static;
|
||||||
|
|
||||||
fn form(config: Option<&Self::Config>) -> Form<Self::Item, Self::Error>;
|
fn form(req: &HttpRequest) -> Form<Self::Item, Self::Error>;
|
||||||
|
|
||||||
fn extract(value: Value<Self::Item>) -> Result<Self, Self::Error>
|
fn extract(value: Value<Self::Item>) -> Result<Self, Self::Error>
|
||||||
where
|
where
|
||||||
|
@ -28,8 +27,7 @@ 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 config = req.app_data::<T::Config>();
|
let form = T::form(req);
|
||||||
let form = T::form(config);
|
|
||||||
|
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
let uploaded = match handle_multipart(multipart, &form).await {
|
let uploaded = match handle_multipart(multipart, &form).await {
|
||||||
|
|
Loading…
Reference in a new issue