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
//! Multipart data extractor.

use super::{load_parts, Multiparts};
use crate::{Error, DEFAULT_FILE_LIMIT, DEFAULT_MAX_PARTS, DEFAULT_TEXT_LIMIT};
use actix_multipart::{Multipart, MultipartError};
use actix_web::dev::Payload;
use actix_web::http::StatusCode;
use actix_web::{FromRequest, HttpRequest, ResponseError};
use futures::future::LocalBoxFuture;
use futures::{FutureExt, TryFutureExt};
use std::convert::TryFrom;
use std::ops;
use std::rc::Rc;
use thiserror::Error;

/// Multipart data extractor (`multipart/form-data`).
///
/// Can be used to extract multipart data from the request body.
///
/// [MultipartFormConfig] allows you to configure extraction process.
///
/// # Example
/// First define a structure to represent the form that implements `FromMultipart` traits.
/// Then use the extractor in your route.
///
/// ```
/// # fn main() {
/// # use actix_easy_multipart_derive::FromMultipart;
/// #[derive(FromMultipart)]
/// struct Upload {
///    description: String,
///    image: MultipartFile,
/// }
/// # use actix_web::Responder;
/// # use actix_easy_multipart::MultipartFile;
/// # use actix_easy_multipart::extractor::MultipartForm;
/// use std::io::Read;
///
/// async fn route(form: MultipartForm<Upload>) -> impl Responder {
///     format!("Received image of size: {}", form.image.size)
/// }
/// # }
/// ```
pub struct MultipartForm<T>(pub T);

impl<T> MultipartForm<T> {
    pub fn into_inner(self) -> T {
        self.0
    }
}

impl<T> ops::Deref for MultipartForm<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T> ops::DerefMut for MultipartForm<T> {
    fn deref_mut(&mut self) -> &mut T {
        &mut self.0
    }
}

impl<T> FromRequest for MultipartForm<T>
where
    T: TryFrom<Multiparts, Error = Error> + 'static,
{
    type Error = actix_web::Error;
    type Future = LocalBoxFuture<'static, Result<Self, Self::Error>>;

    #[inline]
    fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
        let req2 = req.clone();
        let config = req
            .app_data::<MultipartFormConfig>()
            .cloned()
            .unwrap_or_default();

        let mp = Multipart::new(req.headers(), payload.take());
        load_parts(mp, config.text_limit, config.file_limit, config.max_parts)
            .map(move |res| match res {
                Ok(item) => {
                    let t = T::try_from(item)?;
                    Ok(MultipartForm(t))
                }
                Err(e) => Err(MultipartFormError::Multipart(e)),
            })
            .map_err(move |e| {
                if let Some(err) = config.error_handler {
                    (*err)(e, &req2)
                } else {
                    Self::Error::from(e)
                }
            })
            .boxed_local()
    }
}

/// Configure the behaviour of the [MultipartForm] extractor.
///
/// # Usage
/// Add a [MultipartFormConfig] to your actix app data.
/// ```
/// # use actix_web::web::scope;
/// # use actix_easy_multipart::extractor;
/// scope("/").app_data(
///     extractor::MultipartFormConfig::default().file_limit(25 * 1024 * 1024) // 25 MiB
/// );
/// ```
#[derive(Clone)]
pub struct MultipartFormConfig {
    text_limit: usize,
    file_limit: u64,
    max_parts: usize,
    error_handler: Option<Rc<dyn Fn(MultipartFormError, &HttpRequest) -> actix_web::Error>>,
}

impl MultipartFormConfig {
    /// Change max number bytes of text in the multipart. Defaults to [DEFAULT_TEXT_LIMIT].
    pub fn text_limit(mut self, limit: usize) -> Self {
        self.text_limit = limit;
        self
    }

    /// Change max number of bytes for all files in the multipart.
    /// Defaults to [DEFAULT_FILE_LIMIT].
    pub fn file_limit(mut self, limit: u64) -> Self {
        self.file_limit = limit;
        self
    }

    /// Change max number of parts in the form. Defaults to [DEFAULT_MAX_PARTS].
    pub fn max_parts(mut self, max: usize) -> Self {
        self.max_parts = max;
        self
    }

    /// Set custom error handler.
    pub fn error_handler<F>(mut self, f: F) -> Self
    where
        F: Fn(MultipartFormError, &HttpRequest) -> actix_web::Error + 'static,
    {
        self.error_handler = Some(Rc::new(f));
        self
    }
}

impl Default for MultipartFormConfig {
    fn default() -> Self {
        Self {
            text_limit: DEFAULT_TEXT_LIMIT,
            file_limit: DEFAULT_FILE_LIMIT,
            max_parts: DEFAULT_MAX_PARTS,
            error_handler: None,
        }
    }
}

#[derive(Error, Debug)]
pub enum MultipartFormError {
    #[error("Multipart error: {0}")]
    Multipart(MultipartError),
    #[error("Deserialization error: {0}")]
    Deserialization(
        #[from]
        #[source]
        Error,
    ),
}

impl ResponseError for MultipartFormError {
    fn status_code(&self) -> StatusCode {
        match &self {
            MultipartFormError::Multipart(m) => m.status_code(),
            MultipartFormError::Deserialization(d) => d.status_code(),
        }
    }
}