8 releases
0.5.0 | Apr 2, 2020 |
---|---|
0.5.0-rc.5 | Apr 1, 2020 |
0.5.0-rc.4 | Mar 31, 2020 |
#31 in #multipart-form
22 downloads per month
82KB
1.5K
SLoC
Roa-multipart
This crate provides a wrapper for actix_multipart::Multipart
,
which may cause heavy dependencies.
It won't be used as a module of crate roa
until implementing a cleaner Multipart.
Example
use async_std::fs::File;
use async_std::io;
use async_std::path::Path;
use futures::stream::TryStreamExt;
use futures::StreamExt;
use roa::http::StatusCode;
use roa::tcp::Listener;
use roa::router::{Router, post};
use roa::{throw, App, Context};
use roa_multipart::MultipartForm;
use std::error::Error as StdError;
async fn post_file(ctx: &mut Context) -> roa::Result {
let mut form = ctx.form();
while let Some(item) = form.next().await {
let field = item?;
match field.content_disposition() {
None => throw!(StatusCode::BAD_REQUEST, "content disposition not set"),
Some(content_disposition) => match content_disposition.get_filename() {
None => continue, // ignore non-file field
Some(filename) => {
let path = Path::new("./upload");
let mut file = File::create(path.join(filename)).await?;
io::copy(&mut field.into_async_read(), &mut file).await?;
}
},
}
}
Ok(())
}
#[async_std::main]
async fn main() -> Result<(), Box<dyn StdError>> {
let router = Router::new().on("/file", post(post_file));
let (addr, server) = App::new().end(router.routes("/")?).run()?;
server.await?;
Ok(())
}
Dependencies
~31MB
~619K SLoC