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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//! Save multipart files in requests into temporary files
//!
//! # Examples
//! ```rust
//! # extern crate actix_web;
//! # extern crate actix_web_multipart_file;
//! # extern crate futures;
//! # use actix_web::{FutureResponse, HttpResponse};
//! # use actix_web_multipart_file::Multiparts;
//! # use futures::Stream;
//!
//! fn handle_multipart(multiparts: Multiparts) -> FutureResponse<HttpResponse> {
//!     multiparts
//!         .and_then(|field| {
//!             // do something with field
//! #           ::futures::future::ok(())
//!         })
//! # ; unimplemented!()
//!     // ...
//! }
//!
//! ```

extern crate actix_web;
extern crate bytes;
extern crate futures;
#[macro_use]
extern crate log;
extern crate tempfile;
#[macro_use]
extern crate failure;
extern crate mime;

use actix_web::dev::AsyncResult;
use actix_web::error::PayloadError;
use actix_web::http::header::ContentDisposition;
use actix_web::http::HeaderMap;
use actix_web::multipart::Multipart;
use actix_web::multipart::MultipartItem;
use actix_web::{FromRequest, HttpMessage, HttpRequest, HttpResponse, ResponseError};
use bytes::Bytes;
use futures::future::Either;
use futures::prelude::*;
use futures::stream;
use futures::Poll;
use mime::Mime;
use std::fs::File;
use std::io::Write;
use tempfile::tempfile;

#[derive(Debug)]
pub struct Field {
    pub headers: HeaderMap,
    pub content_type: Mime,
    pub content_disposition: ContentDisposition,
    pub form_data: FormData,
}

impl Field {
    /// for compatibility with multipart's Field
    pub fn headers(&self) -> &HeaderMap {
        &self.headers
    }

    /// for compatibility with multipart's Field
    pub fn content_type(&self) -> &Mime {
        &self.content_type
    }

    /// for compatibility with multipart's Field.
    /// always return Some(data)
    pub fn content_disposition(&self) -> Option<ContentDisposition> {
        Some(self.content_disposition.clone())
    }
}

#[derive(Debug)]
pub enum FormData {
    /// form data that don't have filename
    Data {
        /// name field
        name: String,
        /// body
        value: Bytes,
    },
    /// form data thathave filename
    File {
        /// name field
        name: String,
        /// filename field
        filename: String,
        /// a temporary file that contains the body
        file: File,
    },
}

#[derive(Debug, Fail)]
pub enum Error {
    #[fail(display = "Multipart error: {}", _0)]
    Multipart(#[cause] ::actix_web::error::MultipartError),
    #[fail(display = "Given data is not a form data")]
    NotFormData,
    #[fail(display = " File handling error: {}", _0)]
    Io(#[cause] ::std::io::Error),
}

impl From<::actix_web::error::MultipartError> for Error {
    fn from(e: ::actix_web::error::MultipartError) -> Self {
        Error::Multipart(e)
    }
}

impl From<::std::io::Error> for Error {
    fn from(e: ::std::io::Error) -> Self {
        Error::Io(e)
    }
}

impl ResponseError for Error {
    fn error_response(&self) -> HttpResponse {
        match self {
            Error::Multipart(_) => HttpResponse::BadRequest().body("multipart error"),
            Error::NotFormData => HttpResponse::BadRequest().body("not a form data"),
            Error::Io(_) => HttpResponse::InternalServerError().body("file error"),
        }
    }
}

pub type BoxStream<'a, T, E> = Box<Stream<Item = T, Error = E> + 'a>;

fn save_into_tempfile<S, I, E>(input: S) -> impl Future<Item = File, Error = Error>
where
    S: Stream<Item = I, Error = E>,
    I: AsRef<[u8]>,
    Error: From<E>,
{
    tempfile()
        .into_future()
        .from_err()
        .and_then(|tempfile| {
            let file = tempfile.try_clone()?;
            Ok((tempfile, file))
        }).and_then(|(mut tempfile, mut file)| {
            input
                .from_err()
                .and_then(move |bytes| {
                    // FIXME: make it async
                    tempfile.write_all(bytes.as_ref()).map_err(|err| err.into())
                }).collect()
                .and_then(move |_| {
                    use std::io::{Seek, SeekFrom};
                    // FIXME: make it async
                    file.seek(SeekFrom::Start(0))?;
                    Ok(file)
                })
        })
}

/// save form data with filename into tempfiles
/// # Examples
/// ```rust
/// # extern crate actix_web;
/// # extern crate actix_web_multipart_file;
/// # extern crate futures;
/// # use actix_web::{HttpRequest, HttpMessage, FutureResponse, HttpResponse};
/// # use actix_web_multipart_file::save_files;
/// # use futures::Stream;
///
/// fn handle_multipart(req: HttpRequest) -> FutureResponse<HttpResponse> {
///     save_files(req.multipart())
///         .and_then(|field| {
///             // do something with field
/// #           ::futures::future::ok(())
///         })
/// # ; unimplemented!()
///     // ...
/// }
///
/// ```
pub fn save_files<S>(multipart: Multipart<S>) -> BoxStream<'static, Field, Error>
where
    S: Stream<Item = Bytes, Error = PayloadError> + 'static,
{
    let st = multipart
        .map_err(Error::from)
        .map(|item| -> BoxStream<Field, Error> {
            match item {
                MultipartItem::Nested(m) => save_files(m),
                MultipartItem::Field(field) => {
                    debug!("multipart field: {:?}", field);
                    // form data must have content disposition
                    let content_disposition = match field.content_disposition() {
                        Some(d) => d,
                        None => return Box::new(stream::once(Err(Error::NotFormData))),
                    };

                    if !content_disposition.is_form_data() {
                        return Box::new(stream::once(Err(Error::NotFormData)));
                    }

                    // currently fields that don't have names are ignored
                    let name = match content_disposition.get_name() {
                        Some(n) => n.to_string(),
                        None => return Box::new(stream::empty()),
                    };

                    let content_type = field.content_type().clone();
                    let headers = field.headers().clone();

                    let form_data = match content_disposition.get_filename() {
                        None => {
                            let fut = field
                                .from_err()
                                .concat2()
                                .map(|value| FormData::Data { name, value });
                            Either::A(fut)
                        }

                        Some(filename) => {
                            let filename = filename.to_string();
                            let fut = save_into_tempfile(field).map(move |file| FormData::File {
                                name,
                                filename,
                                file,
                            });
                            Either::B(fut)
                        }
                    };
                    let fut = form_data.map(move |form_data| Field {
                        content_type,
                        headers,
                        content_disposition,
                        form_data,
                    });
                    Box::new(fut.into_stream())
                }
            }
        }).flatten();
    Box::new(st)
}

/// An extractor that saves multipart files in requests into temporary files.
/// See the crate level documentation.
pub struct Multiparts(BoxStream<'static, Field, Error>);

impl<S> FromRequest<S> for Multiparts {
    type Config = ();
    type Result = AsyncResult<Self>;
    fn from_request(req: &HttpRequest<S>, _: &Self::Config) -> Self::Result {
        AsyncResult::ok(Multiparts(save_files(req.multipart())))
    }
}

impl Stream for Multiparts {
    type Item = Field;
    type Error = Error;
    fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
        self.0.poll()
    }
}