2022-09-07 23:38:10 +00:00
|
|
|
use actix_form_data::{Error, Field, Form, FormData, Multipart, Value};
|
2019-05-08 23:11:26 +00:00
|
|
|
use actix_web::{
|
2020-06-06 02:40:44 +00:00
|
|
|
web::{post, resource},
|
2021-09-14 01:45:09 +00:00
|
|
|
App, HttpResponse, HttpServer,
|
2019-05-08 23:11:26 +00:00
|
|
|
};
|
2021-09-06 00:37:13 +00:00
|
|
|
use futures_util::stream::StreamExt;
|
2018-04-30 20:22:48 +00:00
|
|
|
|
2022-09-07 23:38:10 +00:00
|
|
|
struct UploadedContent(Value<()>);
|
|
|
|
|
|
|
|
impl FormData for UploadedContent {
|
|
|
|
type Config = ();
|
|
|
|
type Item = ();
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn form(_: Option<&Self::Config>) -> Form<Self::Item, Self::Error> {
|
|
|
|
Form::new()
|
|
|
|
.field("Hey", Field::text())
|
|
|
|
.field(
|
|
|
|
"Hi",
|
|
|
|
Field::map()
|
|
|
|
.field("One", Field::int())
|
|
|
|
.field("Two", Field::float())
|
|
|
|
.finalize(),
|
|
|
|
)
|
|
|
|
.field(
|
|
|
|
"files",
|
|
|
|
Field::array(Field::file(|_, _, mut stream| async move {
|
|
|
|
while let Some(res) = stream.next().await {
|
|
|
|
res?;
|
|
|
|
}
|
|
|
|
Ok(()) as Result<(), Error>
|
|
|
|
})),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extract(value: Value<Self::Item>) -> Result<Self, Self::Error>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
|
|
|
Ok(UploadedContent(value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn upload(Multipart(UploadedContent(value)): Multipart<UploadedContent>) -> HttpResponse {
|
|
|
|
println!("Uploaded Content: {:#?}", value);
|
2020-06-06 02:40:44 +00:00
|
|
|
HttpResponse::Created().finish()
|
2018-04-30 20:22:48 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 16:59:31 +00:00
|
|
|
#[actix_rt::main]
|
|
|
|
async fn main() -> Result<(), anyhow::Error> {
|
2022-09-07 23:38:10 +00:00
|
|
|
HttpServer::new(move || App::new().service(resource("/upload").route(post().to(upload))))
|
|
|
|
.bind("127.0.0.1:8080")?
|
|
|
|
.run()
|
|
|
|
.await?;
|
2019-05-08 23:11:26 +00:00
|
|
|
|
|
|
|
Ok(())
|
2018-04-30 20:22:48 +00:00
|
|
|
}
|