2020-07-09 17:27:24 +00:00
|
|
|
use crate::file_kind::FileKind;
|
|
|
|
use actix_multipart::{Field, Multipart};
|
|
|
|
use actix_web::{error, http::header::DispositionParam};
|
|
|
|
use async_std::{fs, fs::File, path::Path, prelude::*};
|
|
|
|
use chrono::{prelude::*, Duration};
|
|
|
|
use futures::{StreamExt, TryStreamExt};
|
2020-07-08 19:26:46 +00:00
|
|
|
|
2021-04-04 01:38:29 +00:00
|
|
|
pub(crate) struct UploadConfig {
|
|
|
|
pub original_name: String,
|
|
|
|
pub valid_till: DateTime<Local>,
|
|
|
|
pub kind: FileKind,
|
|
|
|
pub delete_on_download: bool,
|
|
|
|
}
|
|
|
|
|
2020-07-09 17:27:24 +00:00
|
|
|
pub(crate) async fn parse_multipart(
|
|
|
|
mut payload: Multipart,
|
|
|
|
file_id: &str,
|
|
|
|
filename: &Path,
|
2021-03-09 22:36:24 +00:00
|
|
|
max_size: Option<u64>,
|
2021-04-04 01:38:29 +00:00
|
|
|
) -> Result<UploadConfig, error::Error> {
|
2020-07-09 17:27:24 +00:00
|
|
|
let mut original_name: Option<String> = None;
|
2021-02-13 15:47:04 +00:00
|
|
|
let mut keep_for: Option<String> = None;
|
2020-07-09 17:27:24 +00:00
|
|
|
let mut kind: Option<FileKind> = None;
|
2021-04-04 01:38:29 +00:00
|
|
|
let mut delete_on_download = false;
|
2020-07-09 17:27:24 +00:00
|
|
|
|
|
|
|
while let Ok(Some(field)) = payload.try_next().await {
|
|
|
|
let name = get_field_name(&field)?;
|
|
|
|
let name = name.as_str();
|
|
|
|
match name {
|
2021-02-13 15:47:04 +00:00
|
|
|
"keep_for" => {
|
|
|
|
keep_for = Some(parse_string(name, field).await?);
|
2020-07-09 17:27:24 +00:00
|
|
|
}
|
2021-02-13 15:47:04 +00:00
|
|
|
"file" => {
|
2020-07-09 17:27:24 +00:00
|
|
|
let file_original_name = get_original_filename(&field);
|
|
|
|
if file_original_name == None || file_original_name.as_deref() == Some("") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
original_name = file_original_name;
|
|
|
|
kind = Some(FileKind::BINARY);
|
|
|
|
let mut file = fs::File::create(&filename)
|
|
|
|
.await
|
|
|
|
.map_err(|_| error::ErrorInternalServerError("could not create file"))?;
|
2021-03-09 22:36:24 +00:00
|
|
|
write_to_file(&mut file, field, max_size).await?;
|
2020-07-09 17:27:24 +00:00
|
|
|
}
|
2021-02-13 15:47:04 +00:00
|
|
|
"text" => {
|
2020-07-09 17:27:24 +00:00
|
|
|
if original_name.is_some() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
original_name = Some(format!("{}.txt", file_id));
|
|
|
|
kind = Some(FileKind::TEXT);
|
|
|
|
let mut file = fs::File::create(&filename)
|
|
|
|
.await
|
|
|
|
.map_err(|_| error::ErrorInternalServerError("could not create file"))?;
|
2021-03-09 22:36:24 +00:00
|
|
|
write_to_file(&mut file, field, max_size).await?;
|
2020-07-09 17:27:24 +00:00
|
|
|
}
|
2021-04-04 01:38:29 +00:00
|
|
|
"delete_on_download" => {
|
|
|
|
delete_on_download = dbg!(parse_string(name, field).await?) != "false";
|
|
|
|
}
|
2020-07-09 17:27:24 +00:00
|
|
|
_ => {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-04-04 01:38:29 +00:00
|
|
|
let original_name = original_name.ok_or_else(|| error::ErrorBadRequest("no content found"))?;
|
|
|
|
let kind = kind.ok_or_else(|| error::ErrorBadRequest("no content found"))?;
|
2020-07-09 17:27:24 +00:00
|
|
|
|
2021-04-04 01:38:29 +00:00
|
|
|
if original_name.len() > 255 {
|
|
|
|
return Err(error::ErrorBadRequest("filename is too long"));
|
2020-07-13 13:22:33 +00:00
|
|
|
}
|
2021-04-04 01:38:29 +00:00
|
|
|
let valid_till = if let Some(keep_for) = keep_for {
|
|
|
|
let keep_for = keep_for.parse().map_err(|e| {
|
|
|
|
error::ErrorBadRequest(format!("field keep_for is not a number: {}", e))
|
|
|
|
})?;
|
|
|
|
let max_keep_for = Duration::days(31).num_seconds();
|
|
|
|
if keep_for > max_keep_for {
|
|
|
|
return Err(error::ErrorBadRequest(format!(
|
|
|
|
"maximum allowed validity is {} seconds, but you specified {} seconds",
|
|
|
|
max_keep_for, keep_for
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
Local::now() + Duration::seconds(keep_for)
|
|
|
|
} else {
|
|
|
|
Local::now() + Duration::seconds(1800)
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(UploadConfig {
|
|
|
|
original_name,
|
|
|
|
valid_till,
|
|
|
|
kind,
|
|
|
|
delete_on_download,
|
|
|
|
})
|
2020-07-09 17:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_field_name(field: &Field) -> Result<String, error::Error> {
|
|
|
|
Ok(field
|
2020-07-08 19:26:46 +00:00
|
|
|
.content_disposition()
|
2020-12-03 22:30:37 +00:00
|
|
|
.ok_or(error::ParseError::Incomplete)?
|
2020-07-08 19:26:46 +00:00
|
|
|
.get_name()
|
|
|
|
.map(|s| s.to_owned())
|
2020-12-03 22:30:37 +00:00
|
|
|
.ok_or(error::ParseError::Incomplete)?)
|
2020-07-08 19:26:46 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 17:27:24 +00:00
|
|
|
async fn parse_string(name: &str, field: actix_multipart::Field) -> Result<String, error::Error> {
|
|
|
|
let data = read_content(field).await?;
|
|
|
|
String::from_utf8(data)
|
|
|
|
.map_err(|_| error::ErrorBadRequest(format!("could not parse field {} as utf-8", name)))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn read_content(mut field: actix_multipart::Field) -> Result<Vec<u8>, error::Error> {
|
2020-07-08 19:26:46 +00:00
|
|
|
let mut data = Vec::new();
|
|
|
|
while let Some(chunk) = field.next().await {
|
2020-07-09 17:27:24 +00:00
|
|
|
data.extend(chunk.map_err(error::ErrorBadRequest)?);
|
2020-07-08 19:26:46 +00:00
|
|
|
}
|
2020-07-09 17:27:24 +00:00
|
|
|
Ok(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn write_to_file(
|
|
|
|
file: &mut File,
|
|
|
|
mut field: actix_multipart::Field,
|
2021-03-09 22:36:24 +00:00
|
|
|
max_size: Option<u64>,
|
2020-07-09 17:27:24 +00:00
|
|
|
) -> Result<(), error::Error> {
|
2021-03-09 22:36:24 +00:00
|
|
|
let mut written_bytes: u64 = 0;
|
2020-07-09 17:27:24 +00:00
|
|
|
while let Some(chunk) = field.next().await {
|
2021-03-09 22:36:24 +00:00
|
|
|
let chunk = chunk.map_err(error::ErrorBadRequest)?;
|
|
|
|
if let Some(max_size) = max_size {
|
|
|
|
written_bytes += chunk.len() as u64;
|
|
|
|
if written_bytes > max_size {
|
|
|
|
return Err(error::ErrorBadRequest(format!(
|
|
|
|
"exceeded maximum file size of {} bytes",
|
|
|
|
max_size
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
file.write_all(chunk.as_ref())
|
|
|
|
.await
|
|
|
|
.map_err(|_| error::ErrorInternalServerError("could not write file"))?;
|
2020-07-09 17:27:24 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_original_filename(field: &actix_multipart::Field) -> Option<String> {
|
|
|
|
field.content_disposition().and_then(|content_disposition| {
|
|
|
|
content_disposition
|
|
|
|
.parameters
|
|
|
|
.into_iter()
|
|
|
|
.find_map(|param| match param {
|
|
|
|
DispositionParam::Filename(filename) => Some(filename),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
})
|
2020-07-08 19:26:46 +00:00
|
|
|
}
|