Crates.io | roa-multipart |
lib.rs | roa-multipart |
version | 0.5.0 |
source | src |
created_at | 2020-03-03 15:07:48.204681 |
updated_at | 2020-04-02 12:46:13.883198 |
description | multipart implementation for roa |
homepage | https://github.com/Hexilee/roa/wiki |
repository | https://github.com/Hexilee/roa |
max_upload_size | |
id | 214940 |
size | 12,280 |
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.
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(())
}