rocket_multipart_form_data/
lib.rs

1/*!
2# Multipart Form Data for Rocket Framework
3
4This crate provides a multipart parser for the Rocket framework.
5
6## Example
7
8```rust
9#[macro_use] extern crate rocket;
10
11use rocket::Data;
12use rocket::http::ContentType;
13
14use rocket_multipart_form_data::{mime, MultipartFormDataOptions, MultipartFormData, MultipartFormDataField, Repetition};
15
16#[post("/", data = "<data>")]
17async fn index(content_type: &ContentType, data: Data<'_>) -> &'static str {
18    let mut options = MultipartFormDataOptions::with_multipart_form_data_fields(
19        vec! [
20            MultipartFormDataField::file("photo").content_type_by_string(Some(mime::IMAGE_STAR)).unwrap(),
21            MultipartFormDataField::raw("fingerprint").size_limit(4096),
22            MultipartFormDataField::text("name"),
23            MultipartFormDataField::text("email").repetition(Repetition::fixed(3)),
24            MultipartFormDataField::text("email"),
25        ]
26    );
27
28    let mut multipart_form_data = MultipartFormData::parse(content_type, data, options).await.unwrap();
29
30    let photo = multipart_form_data.files.get("photo"); // Use the get method to preserve file fields from moving out of the MultipartFormData instance in order to delete them automatically when the MultipartFormData instance is being dropped
31    let fingerprint = multipart_form_data.raw.remove("fingerprint"); // Use the remove method to move raw fields out of the MultipartFormData instance (recommended)
32    let name = multipart_form_data.texts.remove("name"); // Use the remove method to move text fields out of the MultipartFormData instance (recommended)
33    let email = multipart_form_data.texts.remove("email");
34
35    if let Some(file_fields) = photo {
36        let file_field = &file_fields[0]; // Because we only put one "photo" field to the allowed_fields, the max length of this file_fields is 1.
37
38        let _content_type = &file_field.content_type;
39        let _file_name = &file_field.file_name;
40        let _path = &file_field.path;
41
42        // You can now deal with the uploaded file.
43    }
44
45    if let Some(mut raw_fields) = fingerprint {
46        let raw_field = raw_fields.remove(0); // Because we only put one "fingerprint" field to the allowed_fields, the max length of this raw_fields is 1.
47
48        let _content_type = raw_field.content_type;
49        let _file_name = raw_field.file_name;
50        let _raw = raw_field.raw;
51
52        // You can now deal with the raw data.
53    }
54
55    if let Some(mut text_fields) = name {
56        let text_field = text_fields.remove(0); // Because we only put one "text" field to the allowed_fields, the max length of this text_fields is 1.
57
58        let _content_type = text_field.content_type;
59        let _file_name = text_field.file_name;
60        let _text = text_field.text;
61
62        // You can now deal with the text data.
63    }
64
65    if let Some(text_fields) = email {
66        for text_field in text_fields { // We put "email" field to the allowed_fields for two times and let the first time repeat for 3 times, so the max length of this text_fields is 4.
67            let _content_type = text_field.content_type;
68            let _file_name = text_field.file_name;
69            let _text = text_field.text;
70
71            // You can now deal with the text data.
72        }
73    }
74
75    "ok"
76}
77```
78
79Also see `examples`.
80 */
81
82pub extern crate mime;
83pub extern crate multer;
84
85mod fields;
86mod multipart_form_data;
87mod multipart_form_data_errors;
88mod multipart_form_data_field;
89mod multipart_form_data_options;
90mod multipart_form_data_type;
91mod repetition;
92
93pub use fields::*;
94pub use multipart_form_data::*;
95pub use multipart_form_data_errors::*;
96pub use multipart_form_data_field::*;
97pub use multipart_form_data_options::*;
98pub use multipart_form_data_type::*;
99pub use repetition::*;