[][src]Crate 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_core::http::StatusCode;
use roa_tcp::Listener;
use roa_router::Router;
use roa_core::{throw, App};
use roa_multipart::Multipart;
use std::error::Error as StdError;

let mut app = App::new(());
let mut router = Router::<()>::new();
router.post("/file", |mut ctx| async move {
    let mut form = Multipart::new(&mut ctx);
    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(())
});
let (addr, server) = app.run()?;
// server.await
Ok(())

Structs

Field

A wrapper for actix multipart field.

Multipart

A wrapper for actix multipart.

WrapError

A wrapper for actix multipart field.