2020-09-10 08:54:38 +00:00
|
|
|
use crate::http::GQLRequest;
|
|
|
|
use crate::{ParseRequestError, Request};
|
|
|
|
use bytes::Bytes;
|
2020-09-10 07:04:24 +00:00
|
|
|
use futures::io::AsyncRead;
|
2020-09-10 08:54:38 +00:00
|
|
|
use futures::stream::{self, Stream};
|
|
|
|
use multer::{Constraints, Multipart, SizeLimit};
|
2020-09-10 07:04:24 +00:00
|
|
|
use std::collections::HashMap;
|
2020-09-10 08:54:38 +00:00
|
|
|
use std::io::{self, Seek, SeekFrom, Write};
|
2020-09-10 07:04:24 +00:00
|
|
|
use std::pin::Pin;
|
2020-09-10 08:54:38 +00:00
|
|
|
use std::task::Poll;
|
2020-09-10 07:04:24 +00:00
|
|
|
|
|
|
|
/// Options for `receive_multipart`.
|
|
|
|
#[derive(Default, Clone)]
|
|
|
|
#[non_exhaustive]
|
|
|
|
pub struct MultipartOptions {
|
|
|
|
/// Maximum file size.
|
|
|
|
pub max_file_size: Option<usize>,
|
|
|
|
|
|
|
|
/// Maximum number of files.
|
|
|
|
pub max_num_files: Option<usize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Receive a multipart request.
|
|
|
|
pub async fn receive_multipart(
|
2020-09-11 07:54:56 +00:00
|
|
|
body: impl AsyncRead + Send + 'static,
|
2020-09-10 07:04:24 +00:00
|
|
|
boundary: impl Into<String>,
|
|
|
|
opts: MultipartOptions,
|
2020-09-10 08:54:38 +00:00
|
|
|
) -> Result<Request, ParseRequestError> {
|
2020-09-10 07:04:24 +00:00
|
|
|
let mut multipart = Multipart::new_with_constraints(
|
|
|
|
reader_stream(body),
|
|
|
|
boundary,
|
|
|
|
Constraints::new().size_limit({
|
|
|
|
let mut limit = SizeLimit::new();
|
|
|
|
if let (Some(max_file_size), Some(max_num_files)) =
|
|
|
|
(opts.max_file_size, opts.max_file_size)
|
|
|
|
{
|
|
|
|
limit = limit.whole_stream((max_file_size * max_num_files) as u64);
|
|
|
|
}
|
|
|
|
if let Some(max_file_size) = opts.max_file_size {
|
|
|
|
limit = limit.per_field(max_file_size as u64);
|
|
|
|
}
|
|
|
|
limit
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2020-09-10 08:54:38 +00:00
|
|
|
let mut request = None;
|
2020-09-10 07:04:24 +00:00
|
|
|
let mut map = None;
|
|
|
|
let mut files = Vec::new();
|
|
|
|
|
|
|
|
while let Some(mut field) = multipart.next_field().await? {
|
|
|
|
match field.name() {
|
|
|
|
Some("operations") => {
|
|
|
|
let request_str = field.text().await?;
|
2020-09-10 08:54:38 +00:00
|
|
|
request = Some(
|
|
|
|
serde_json::from_str::<GQLRequest>(&request_str)
|
|
|
|
.map_err(ParseRequestError::InvalidRequest)?
|
|
|
|
.into(),
|
|
|
|
);
|
2020-09-10 07:04:24 +00:00
|
|
|
}
|
|
|
|
Some("map") => {
|
|
|
|
let map_str = field.text().await?;
|
|
|
|
map = Some(
|
|
|
|
serde_json::from_str::<HashMap<String, Vec<String>>>(&map_str)
|
|
|
|
.map_err(ParseRequestError::InvalidFilesMap)?,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
if let Some(name) = field.name().map(ToString::to_string) {
|
|
|
|
if let Some(filename) = field.file_name().map(ToString::to_string) {
|
2020-09-10 08:54:38 +00:00
|
|
|
let content_type = field.content_type().map(|mime| mime.to_string());
|
|
|
|
let mut file = tempfile::tempfile().map_err(ParseRequestError::Io)?;
|
2020-09-10 07:04:24 +00:00
|
|
|
while let Some(chunk) = field.chunk().await.unwrap() {
|
|
|
|
file.write(&chunk).map_err(ParseRequestError::Io)?;
|
|
|
|
}
|
|
|
|
file.seek(SeekFrom::Start(0))?;
|
|
|
|
files.push((name, filename, content_type, file));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-10 08:54:38 +00:00
|
|
|
let mut request: Request = request.ok_or(ParseRequestError::MissingOperatorsPart)?;
|
2020-09-10 07:04:24 +00:00
|
|
|
let map = map.as_mut().ok_or(ParseRequestError::MissingMapPart)?;
|
|
|
|
|
|
|
|
for (name, filename, content_type, file) in files {
|
|
|
|
if let Some(var_paths) = map.remove(&name) {
|
|
|
|
for var_path in var_paths {
|
2020-09-10 08:54:38 +00:00
|
|
|
request.set_upload(
|
2020-09-10 07:04:24 +00:00
|
|
|
&var_path,
|
|
|
|
filename.clone(),
|
|
|
|
content_type.clone(),
|
|
|
|
file.try_clone().unwrap(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !map.is_empty() {
|
|
|
|
return Err(ParseRequestError::MissingFiles);
|
|
|
|
}
|
|
|
|
|
2020-09-10 08:54:38 +00:00
|
|
|
Ok(request)
|
2020-09-10 07:04:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn reader_stream(
|
2020-09-11 07:54:56 +00:00
|
|
|
reader: impl AsyncRead + Send + 'static,
|
|
|
|
) -> impl Stream<Item = io::Result<Bytes>> + Send + 'static {
|
2020-09-10 07:04:24 +00:00
|
|
|
let mut buf = [0u8; 2048];
|
2020-09-11 07:54:56 +00:00
|
|
|
let mut reader = Box::pin(reader);
|
2020-09-10 07:04:24 +00:00
|
|
|
stream::poll_fn(move |cx| {
|
|
|
|
Poll::Ready(
|
|
|
|
match futures::ready!(Pin::new(&mut reader).poll_read(cx, &mut buf)?) {
|
|
|
|
0 => None,
|
|
|
|
size => Some(Ok(Bytes::copy_from_slice(&buf[..size]))),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|