1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
mod extractor;
mod load;
#[cfg(test)]
mod test;
pub use extractor::*;
pub use load::*;
use actix_web::http::StatusCode;
use actix_web::ResponseError;
use err_derive::Error;
use std::ffi::OsStr;
use std::path::Path;
use std::str::FromStr;
use tempfile::NamedTempFile;
pub type Multiparts = Vec<MultipartField>;
#[derive(Debug)]
pub struct MultipartFile {
pub file: NamedTempFile,
pub size: u64,
pub name: String,
pub filename: Option<String>,
pub mime: mime::Mime,
}
impl MultipartFile {
pub fn get_extension(&self) -> Option<&str> {
self.filename
.as_ref()
.and_then(|f| Path::new(f.as_str()).extension().and_then(OsStr::to_str))
}
}
#[derive(Debug)]
pub struct MultipartText {
pub name: String,
pub text: String,
}
#[derive(Debug)]
pub enum MultipartField {
File(MultipartFile),
Text(MultipartText),
}
#[derive(Debug, Error)]
pub enum GetError {
#[error(display = "Field '{}' not found", _0)]
NotFound(String),
#[error(display = "Field '{}' couldn't be converted into {}", _0, _1)]
TypeError(String, String),
#[error(display = "Duplicate values found for field '{}'", _0)]
DuplicateField(String),
}
impl ResponseError for GetError {
fn status_code(&self) -> StatusCode {
StatusCode::BAD_REQUEST
}
}
pub trait MultipartType
where
Self: std::marker::Sized,
{
fn get(form: &mut Multiparts, field_name: &str) -> Result<Self, GetError>;
}
pub trait MultipartTypeSpecial
where
Self: std::marker::Sized,
{
fn get(form: &mut Multiparts, field_name: &str) -> Result<Self, GetError>;
}
impl<T: FromStr> MultipartType for T {
fn get(form: &mut Multiparts, field_name: &str) -> Result<Self, GetError> {
let mut matches = Vec::<T>::get(form, field_name)?;
match matches.len() {
0 => Err(GetError::NotFound(field_name.into())),
1 => Ok(matches.pop().unwrap()),
_ => Err(GetError::DuplicateField(field_name.into())),
}
}
}
impl<T: FromStr> MultipartTypeSpecial for Option<T> {
fn get(form: &mut Multiparts, field_name: &str) -> Result<Self, GetError> {
let mut matches = Vec::<T>::get(form, field_name)?;
match matches.len() {
0 => Ok(None),
1 => Ok(Some(matches.pop().unwrap())),
_ => Err(GetError::DuplicateField(field_name.into())),
}
}
}
impl<T: FromStr> MultipartTypeSpecial for Vec<T> {
fn get(form: &mut Multiparts, field_name: &str) -> Result<Self, GetError> {
let mut matches = Vec::new();
for i in form {
match i {
MultipartField::File(_) => {}
MultipartField::Text(x) => {
if x.name == field_name {
let y: T = x.text.parse().map_err(|_| {
GetError::TypeError(
field_name.into(),
std::any::type_name::<T>().into(),
)
})?;
matches.push(y);
}
}
}
}
Ok(matches)
}
}
impl MultipartType for MultipartFile {
fn get(form: &mut Multiparts, field_name: &str) -> Result<Self, GetError> {
let mut matches = Vec::<MultipartFile>::get(form, field_name)?;
match matches.len() {
0 => Err(GetError::NotFound(field_name.into())),
1 => Ok(matches.pop().unwrap()),
_ => Err(GetError::DuplicateField(field_name.into())),
}
}
}
impl MultipartTypeSpecial for Option<MultipartFile> {
fn get(form: &mut Multiparts, field_name: &str) -> Result<Self, GetError> {
let mut matches = Vec::<MultipartFile>::get(form, field_name)?;
match matches.len() {
0 => Ok(None),
1 => Ok(Some(matches.pop().unwrap())),
_ => Err(GetError::DuplicateField(field_name.into())),
}
}
}
impl MultipartTypeSpecial for Vec<MultipartFile> {
fn get(form: &mut Multiparts, field_name: &str) -> Result<Self, GetError> {
let mut indexes = Vec::new();
for (idx, item) in form.iter().enumerate() {
match item {
MultipartField::Text(_) => {}
MultipartField::File(x) => {
if x.name == field_name {
indexes.push(idx)
}
}
}
}
Ok(indexes
.iter()
.rev()
.map(|idx| match form.remove(*idx) {
MultipartField::File(x) => x,
MultipartField::Text(_) => panic!(),
})
.collect())
}
}