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 options = MultipartFormDataOptions {
19 max_data_bytes: 10 * 1024 * 1024,
20 allowed_fields: vec![
21 MultipartFormDataField::file("photo").content_type_by_string(Some(mime::IMAGE_STAR)).unwrap(),
22 MultipartFormDataField::raw("fingerprint").size_limit(4096),
23 MultipartFormDataField::text("name"),
24 MultipartFormDataField::text("email").repetition(Repetition::fixed(3)),
25 MultipartFormDataField::text("email"),
26 ],
27 ..MultipartFormDataOptions::default()
28 };
29
30 let mut multipart_form_data = MultipartFormData::parse(content_type, data, options).await.unwrap();
31
32 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
33 let fingerprint = multipart_form_data.raw.remove("fingerprint"); // Use the remove method to move raw fields out of the MultipartFormData instance (recommended)
34 let name = multipart_form_data.texts.remove("name"); // Use the remove method to move text fields out of the MultipartFormData instance (recommended)
35 let email = multipart_form_data.texts.remove("email");
36
37 if let Some(file_fields) = photo {
38 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.
39
40 let _content_type = &file_field.content_type;
41 let _file_name = &file_field.file_name;
42 let _path = &file_field.path;
43
44 // You can now deal with the uploaded file.
45 }
46
47 if let Some(mut raw_fields) = fingerprint {
48 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.
49
50 let _content_type = raw_field.content_type;
51 let _file_name = raw_field.file_name;
52 let _raw = raw_field.raw;
53
54 // You can now deal with the raw data.
55 }
56
57 if let Some(mut text_fields) = name {
58 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.
59
60 let _content_type = text_field.content_type;
61 let _file_name = text_field.file_name;
62 let _text = text_field.text;
63
64 // You can now deal with the text data.
65 }
66
67 if let Some(text_fields) = email {
68 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.
69 let _content_type = text_field.content_type;
70 let _file_name = text_field.file_name;
71 let _text = text_field.text;
72
73 // You can now deal with the text data.
74 }
75 }
76
77 "ok"
78}
79```
80
81Also see `examples`.
82 */
83
84#![warn(missing_docs)]
85
86pub use mime;
87pub use multer;
88
89mod fields;
90mod multipart_form_data;
91mod multipart_form_data_errors;
92mod multipart_form_data_field;
93mod multipart_form_data_options;
94mod multipart_form_data_type;
95mod repetition;
96
97pub use fields::*;
98pub use multipart_form_data::*;
99pub use multipart_form_data_errors::*;
100pub use multipart_form_data_field::*;
101pub use multipart_form_data_options::*;
102pub use multipart_form_data_type::*;
103pub use repetition::*;