Code Monkey home page Code Monkey logo

actix-easy-multipart's Introduction

Actix Easy Multipart

Build status crates.io docs.rs

Typed multipart form extractor for actix-web.

โš  Deprecation notice

This functionality has been now been merged into the official actix-multipart crate (as of version 0.6.0).

Please use the actix-multipart module instead, read the documentation here.

This repository will no longer be maintained.

Example

use actix_web::Responder;
use actix_easy_multipart::tempfile::Tempfile;
use actix_easy_multipart::text::Text;
use actix_easy_multipart::MultipartForm;

#[derive(MultipartForm)]
struct Upload {
    description: Option<Text<String>>,
    timestamp: Text<i64>,
    #[multipart(rename="image_set[]")]
    image_set: Vec<Tempfile>,
}

async fn route(form: MultipartForm<Upload>) -> impl Responder {
    format!("Received 5 images: {}", form.image_set.len())
}

Features

  • Receiving optional fields, using Option.
  • Receiving lists of fields, using Vec<T>.
  • Deserialize integers, floats, enums from plain text fields using Text<T>.
  • Deserialize complex data from JSON uploads, using Json<T>.
  • Receive file uploads into temporary files on disk, using Tempfile.
  • User customisable asynchronous field readers, for example you may want to stream form data to an object storage service, just implement the FieldReader trait.

Versions and Compatibility

actix-easy-multipart actix-web tokio
0.x 2.x 0.2
1.x 3.x 0.2
2.x 4.x 1
3.x 4.x 1

See Also

actix-easy-multipart's People

Contributors

jacob-pro avatar spineethling avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

actix-easy-multipart's Issues

Upload files larger than 256kb

When I use this library and I try to upload a file larger than the default 256kb file limit, I get the following error:

Multipart error: Payload reached size limit.

This is the code I use for the endpoint:

#[derive(FromMultipart, ToSchema)]
struct Payload {
    other: String,
    #[schema(value_type = String, format = Binary)]
    payload: File,
}

#[utoipa::path(
    request_body(content = Payload, content_type = "multipart/form-data"),
    responses(
        (status = 201, description = "Payload successfully uploaded"),
    )
)]
#[post("/payload/upload")]
async fn task_upload(metadata: MultipartForm<Payload>) -> impl Responder {
    println!("{}, {:?}", metadata.other, metadata.payload.filename);

    HttpResponse::Ok()
}

To fix this, I've tried to change the default size for both actix and this library:

HttpServer::new(move || {
    App::new()
        .app_data(state.clone())
        .app_data(actix_web::web::PayloadConfig::new(500 * 1024 * 1024))
        .app_data(MultipartFormConfig::default().file_limit(500 * 1024 * 1024))
        .service(
            SwaggerUi::new("/swagger-ui/{_:.*}").url("/api-doc/openapi.json", openapi.clone()),
        )
        .service(task_upload)
})
.bind(("0.0.0.0", 8080))?
.run()
.await

Nothing seems to help. When I use Multipart and stream directly from actix, the file limit error does not kick in.
I'm not sure if the issue happens in this library, or if it is actix as this is unfortunately a part of the framework that lacks documentation.

Example of form with several fields and a binary file

As of now, the example shows how to handle a multipart form that has just one text field:
#[derive(MultipartForm)] struct Upload { text: Text<String>, }
Could you please update the example or create a new one showing how to handle a form that has several text fields and a binary file? This is the typical example of a user using a form to submit some information alongside a binary file.
@jacob-pro, this is valuable work, should be pulled into the official actix web multipart package.

persist unavailable?

code

#[derive(FromMultipart)]
pub struct Upload {
    name: String,
    file: File,
}

#[post("/upload")]
pub async fn batch_upload_photo(
    form: MultipartForm<Upload>,
) -> anyhow::Result<impl Responder, ServiceError> {
    let new_file = form.file.file.persist("./upload/a.png");

    println!("file {:?}", &form.file.file);
}

error

cannot move out of dereference of `MultipartForm<Upload>`

image

An example to store file in DB

Assuming you are working with a PostgreSQL database, any example of dropping the uploaded file in a media folder at the project's root and storing a reference to the file in DB?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.