roa-multipart

Crates.ioroa-multipart
lib.rsroa-multipart
version0.5.0
sourcesrc
created_at2020-03-03 15:07:48.204681
updated_at2020-04-02 12:46:13.883198
descriptionmultipart implementation for roa
homepagehttps://github.com/Hexilee/roa/wiki
repositoryhttps://github.com/Hexilee/roa
max_upload_size
id214940
size12,280
xixi (Hexilee)

documentation

https://docs.rs/roa-multipart

README

Stable Test codecov Rust Docs Crate version Download Version License: MIT

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(())
}
Commit count: 722

cargo fmt