19 releases

0.8.1 Mar 1, 2022
0.7.0 Oct 22, 2021
0.6.1 May 17, 2021
0.6.0 Sep 19, 2020
0.2.1 Oct 12, 2018

#629 in HTTP server

Download history 1/week @ 2023-12-11 1/week @ 2023-12-18 1/week @ 2023-12-25 1/week @ 2024-01-08 23/week @ 2024-01-22 42/week @ 2024-01-29 5/week @ 2024-02-12 90/week @ 2024-02-19 25/week @ 2024-02-26 19/week @ 2024-03-04 74/week @ 2024-03-11 29/week @ 2024-03-18 38/week @ 2024-03-25

162 downloads per month

MIT license

43KB
799 lines

awmp

Docs Crates.io

A convenience library for working with multipart/form-data in actix-web 1.x, 2.x, 3.x, or 4.x.

This library uses actix-multipart internally, and is not a replacement for actix-multipart. It saves multipart file data to tempfiles and collects text data, handling all blocking I/O operations.

Provides some configuration options in PartsConfig:

  • text_limit: Any text field data larger than this number of bytes will be saved as a tempfile
  • file_limit: Any file field data larger than this number of bytes will be discarded/ignored
  • file_fields: Treat fields with these names as file fields
  • text_fields: Treat fields with these names as text fields
  • temp_dir: Use this folder as the tmp directory, rather than tempfile's default

Usage

This crate supports both major versions of actix-web, 1.x, 2.x, 3.x, and 4.x. It supports 4.x by default.

To use with actix-web 1.x, add the following to your Cargo.toml:

awmp = { version = "0.8", default-features = false, features = ["v1"] }

Example

use actix_web::{web, App, Error, FromRequest, HttpResponse, HttpServer};

async fn upload(mut parts: awmp::Parts) -> Result<actix_web::HttpResponse, actix_web::Error> {
    let qs = parts.texts.to_query_string();

    let file_parts = parts
        .files
        .take("file")
        .pop()
        .and_then(|f| f.persist("/tmp").ok())
        .map(|f| format!("File uploaded to: {}", f.display()))
        .unwrap_or_default();

    let body = [format!("Text parts: {}", &qs), file_parts].join(", ");

    Ok(actix_web::HttpResponse::Ok().body(body))
}

#[actix_rt::main]
async fn main() -> Result<(), std::io::Error> {
    actix_web::HttpServer::new(move || {
        actix_web::App::new()
            .data(awmp::PartsConfig::default().with_file_limit(100000))
            .route("/", actix_web::web::post().to(upload))
    })
    .bind("0.0.0.0:3000")?
    .run()
    .await
}

Current version: 0.8.1

License: MIT

Dependencies

~6–23MB
~354K SLoC