2020-10-15 06:38:10 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::io::{self, Seek, SeekFrom, Write};
|
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
|
|
|
|
2020-10-16 06:49:22 +00:00
|
|
|
use futures_util::io::AsyncRead;
|
|
|
|
use futures_util::stream::Stream;
|
2020-09-10 08:54:38 +00:00
|
|
|
use multer::{Constraints, Multipart, SizeLimit};
|
2020-09-14 01:46:22 +00:00
|
|
|
use pin_project_lite::pin_project;
|
2020-10-15 06:38:10 +00:00
|
|
|
|
|
|
|
use crate::{BatchRequest, ParseRequestError, UploadValue};
|
2020-09-10 07:04:24 +00:00
|
|
|
|
|
|
|
/// Options for `receive_multipart`.
|
2020-09-23 17:30:03 +00:00
|
|
|
#[derive(Default, Clone, Copy)]
|
2020-09-14 19:16:41 +00:00
|
|
|
#[non_exhaustive]
|
2020-09-10 07:04:24 +00:00
|
|
|
pub struct MultipartOptions {
|
2020-09-14 19:16:41 +00:00
|
|
|
/// The maximum file size.
|
|
|
|
pub max_file_size: Option<usize>,
|
|
|
|
/// The maximum number of files.
|
|
|
|
pub max_num_files: Option<usize>,
|
2020-09-11 23:43:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MultipartOptions {
|
|
|
|
/// Set maximum file size.
|
|
|
|
pub fn max_file_size(self, size: usize) -> Self {
|
|
|
|
MultipartOptions {
|
|
|
|
max_file_size: Some(size),
|
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
2020-09-10 07:04:24 +00:00
|
|
|
|
2020-09-11 23:43:50 +00:00
|
|
|
/// Set maximum number of files.
|
|
|
|
pub fn max_num_files(self, n: usize) -> Self {
|
|
|
|
MultipartOptions {
|
|
|
|
max_num_files: Some(n),
|
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
2020-09-10 07:04:24 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 18:43:03 +00:00
|
|
|
pub(super) async fn receive_batch_multipart(
|
2021-06-16 09:20:41 +00:00
|
|
|
body: impl AsyncRead + Send,
|
2020-09-10 07:04:24 +00:00
|
|
|
boundary: impl Into<String>,
|
|
|
|
opts: MultipartOptions,
|
2020-09-17 18:43:03 +00:00
|
|
|
) -> Result<BatchRequest, ParseRequestError> {
|
2021-06-07 06:07:53 +00:00
|
|
|
let mut multipart = Multipart::with_constraints(
|
2020-09-13 10:49:07 +00:00
|
|
|
ReaderStream::new(body),
|
2020-09-10 07:04:24 +00:00
|
|
|
boundary,
|
|
|
|
Constraints::new().size_limit({
|
|
|
|
let mut limit = SizeLimit::new();
|
|
|
|
if let (Some(max_file_size), Some(max_num_files)) =
|
2021-02-13 18:50:17 +00:00
|
|
|
(opts.max_file_size, opts.max_num_files)
|
2020-09-10 07:04:24 +00:00
|
|
|
{
|
|
|
|
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(
|
2020-09-17 18:43:03 +00:00
|
|
|
serde_json::from_str::<BatchRequest>(&request_str)
|
2020-09-12 16:07:46 +00:00
|
|
|
.map_err(ParseRequestError::InvalidRequest)?,
|
2020-09-10 08:54:38 +00:00
|
|
|
);
|
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-10-23 10:49:00 +00:00
|
|
|
let content_type = field.content_type().map(ToString::to_string);
|
2020-09-10 08:54:38 +00:00
|
|
|
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-17 18:43:03 +00:00
|
|
|
let mut request: BatchRequest = 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) {
|
2020-10-10 02:32:43 +00:00
|
|
|
let upload = UploadValue {
|
|
|
|
filename,
|
|
|
|
content_type,
|
|
|
|
content: file,
|
|
|
|
};
|
|
|
|
|
2020-09-10 07:04:24 +00:00
|
|
|
for var_path in var_paths {
|
2020-09-17 18:43:03 +00:00
|
|
|
match &mut request {
|
|
|
|
BatchRequest::Single(request) => {
|
2020-10-10 02:32:43 +00:00
|
|
|
request.set_upload(&var_path, upload.try_clone()?);
|
2020-09-17 18:43:03 +00:00
|
|
|
}
|
|
|
|
BatchRequest::Batch(requests) => {
|
|
|
|
let mut s = var_path.splitn(2, '.');
|
|
|
|
let idx = s.next().and_then(|idx| idx.parse::<usize>().ok());
|
|
|
|
let path = s.next();
|
|
|
|
|
|
|
|
if let (Some(idx), Some(path)) = (idx, path) {
|
|
|
|
if let Some(request) = requests.get_mut(idx) {
|
2020-10-10 02:32:43 +00:00
|
|
|
request.set_upload(path, upload.try_clone()?);
|
2020-09-17 18:43:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-10 07:04:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-09-13 10:49:07 +00:00
|
|
|
pin_project! {
|
2020-09-17 08:39:55 +00:00
|
|
|
pub(crate) struct ReaderStream<T> {
|
2020-09-13 10:49:07 +00:00
|
|
|
buf: [u8; 2048],
|
|
|
|
#[pin]
|
|
|
|
reader: T,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> ReaderStream<T> {
|
2020-09-17 08:39:55 +00:00
|
|
|
pub(crate) fn new(reader: T) -> Self {
|
2020-09-13 10:49:07 +00:00
|
|
|
Self {
|
|
|
|
buf: [0; 2048],
|
|
|
|
reader,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: AsyncRead> Stream for ReaderStream<T> {
|
2020-11-30 05:43:17 +00:00
|
|
|
type Item = io::Result<Vec<u8>>;
|
2020-09-13 10:49:07 +00:00
|
|
|
|
|
|
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
|
|
|
|
let this = self.project();
|
|
|
|
|
2020-09-10 07:04:24 +00:00
|
|
|
Poll::Ready(
|
2020-10-16 06:49:22 +00:00
|
|
|
match futures_util::ready!(this.reader.poll_read(cx, this.buf)?) {
|
2020-09-10 07:04:24 +00:00
|
|
|
0 => None,
|
2020-11-30 05:43:17 +00:00
|
|
|
size => Some(Ok(this.buf[..size].to_vec())),
|
2020-09-14 01:46:22 +00:00
|
|
|
},
|
2020-09-10 07:04:24 +00:00
|
|
|
)
|
2020-09-13 10:49:07 +00:00
|
|
|
}
|
2020-09-10 07:04:24 +00:00
|
|
|
}
|