2021-04-07 22:03:02 +00:00
|
|
|
use crate::{config, file_kind::FileKind};
|
2020-07-09 17:27:24 +00:00
|
|
|
use actix_multipart::{Field, Multipart};
|
2021-09-11 00:08:47 +00:00
|
|
|
use actix_web::{error, http::header::DispositionParam, Error};
|
2022-02-26 23:34:57 +00:00
|
|
|
use futures_util::{StreamExt, TryStreamExt};
|
|
|
|
use std::path::Path;
|
2022-07-02 20:28:48 +00:00
|
|
|
use time::{Duration, OffsetDateTime};
|
2022-02-26 23:34:57 +00:00
|
|
|
use tokio::{fs::File, io::AsyncWriteExt};
|
2020-07-08 19:26:46 +00:00
|
|
|
|
2021-12-20 10:12:09 +00:00
|
|
|
const MAX_UPLOAD_SECONDS: i64 = 31 * 24 * 60 * 60;
|
2022-02-27 00:50:29 +00:00
|
|
|
const DEFAULT_UPLOAD_SECONDS: u32 = 30 * 60;
|
2021-08-18 21:22:50 +00:00
|
|
|
|
2021-04-04 01:38:29 +00:00
|
|
|
pub(crate) struct UploadConfig {
|
|
|
|
pub original_name: String,
|
2022-02-27 00:50:29 +00:00
|
|
|
pub valid_till: OffsetDateTime,
|
2021-04-04 01:38:29 +00:00
|
|
|
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,
|
2021-09-11 00:08:47 +00:00
|
|
|
file_name: &Path,
|
2021-04-07 22:03:02 +00:00
|
|
|
config: &config::Config,
|
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;
|
2021-04-07 22:03:02 +00:00
|
|
|
let mut password = None;
|
|
|
|
let mut size = 0;
|
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);
|
2022-02-26 23:34:57 +00:00
|
|
|
if file_original_name == None || file_original_name.map(|f| f.as_str()) == Some("")
|
|
|
|
{
|
2020-07-09 17:27:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-02-26 23:34:57 +00:00
|
|
|
original_name = file_original_name.map(|f| f.to_string());
|
2021-04-04 12:30:31 +00:00
|
|
|
kind = Some(FileKind::Binary);
|
2021-09-11 00:08:47 +00:00
|
|
|
size = create_file(file_name, field, config.max_file_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));
|
2021-04-04 12:30:31 +00:00
|
|
|
kind = Some(FileKind::Text);
|
2021-09-11 00:08:47 +00:00
|
|
|
size = create_file(file_name, field, config.max_file_size).await?;
|
2020-07-09 17:27:24 +00:00
|
|
|
}
|
2021-04-04 01:38:29 +00:00
|
|
|
"delete_on_download" => {
|
2021-09-24 20:51:13 +00:00
|
|
|
delete_on_download = parse_string(name, field).await? != "false";
|
2021-04-04 01:38:29 +00:00
|
|
|
}
|
2021-04-07 22:03:02 +00:00
|
|
|
"password" => {
|
|
|
|
password = Some(parse_string(name, field).await?);
|
|
|
|
}
|
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"))?;
|
2022-02-27 00:50:29 +00:00
|
|
|
let keep_for: u32 = keep_for
|
|
|
|
.map(|k| k.parse())
|
|
|
|
.transpose()
|
|
|
|
.map_err(|e| error::ErrorBadRequest(format!("field keep_for is not a number: {}", e)))?
|
|
|
|
.unwrap_or(DEFAULT_UPLOAD_SECONDS);
|
2022-07-02 20:28:48 +00:00
|
|
|
let valid_duration = Duration::seconds(keep_for.into());
|
2022-02-27 00:50:29 +00:00
|
|
|
let valid_till = OffsetDateTime::now_utc() + valid_duration;
|
2021-04-04 01:38:29 +00:00
|
|
|
|
2021-12-20 10:12:09 +00:00
|
|
|
let upload_config = UploadConfig {
|
2021-04-04 01:38:29 +00:00
|
|
|
original_name,
|
|
|
|
valid_till,
|
|
|
|
kind,
|
|
|
|
delete_on_download,
|
2021-12-20 10:12:09 +00:00
|
|
|
};
|
|
|
|
|
2022-02-27 00:50:29 +00:00
|
|
|
check_requirements(&upload_config, size, password, &valid_duration, config)?;
|
2021-12-20 10:12:09 +00:00
|
|
|
|
|
|
|
Ok(upload_config)
|
2020-07-09 17:27:24 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 10:12:09 +00:00
|
|
|
fn check_requirements(
|
|
|
|
upload_config: &UploadConfig,
|
2021-04-07 22:03:02 +00:00
|
|
|
size: u64,
|
|
|
|
password: Option<String>,
|
2022-02-27 00:50:29 +00:00
|
|
|
valid_duration: &Duration,
|
2021-04-07 22:03:02 +00:00
|
|
|
config: &config::Config,
|
|
|
|
) -> Result<(), error::Error> {
|
2021-12-20 10:12:09 +00:00
|
|
|
if upload_config.original_name.len() > 255 {
|
|
|
|
return Err(error::ErrorBadRequest("filename is too long"));
|
|
|
|
}
|
|
|
|
|
2022-02-27 00:50:29 +00:00
|
|
|
let valid_seconds = valid_duration.whole_seconds();
|
2021-12-20 10:12:09 +00:00
|
|
|
if valid_seconds > MAX_UPLOAD_SECONDS {
|
|
|
|
return Err(error::ErrorBadRequest(format!(
|
|
|
|
"maximum allowed validity is {} seconds, but you specified {} seconds",
|
|
|
|
MAX_UPLOAD_SECONDS, valid_seconds
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
2021-04-07 22:03:02 +00:00
|
|
|
if let Some(no_auth_limits) = &config.no_auth_limits {
|
2022-02-27 00:50:29 +00:00
|
|
|
let requires_auth = valid_seconds > no_auth_limits.max_time.whole_seconds()
|
|
|
|
|| valid_seconds > no_auth_limits.large_file_max_time.whole_seconds()
|
2021-04-07 22:03:02 +00:00
|
|
|
&& size > no_auth_limits.large_file_size;
|
2021-12-20 10:12:09 +00:00
|
|
|
// hIGh sECUriTy paSsWoRD CHEck
|
2021-04-07 22:03:02 +00:00
|
|
|
if requires_auth && password.as_ref() != Some(&no_auth_limits.auth_password) {
|
|
|
|
return Err(error::ErrorBadRequest(
|
|
|
|
"upload requires authentication, but authentication was incorrect",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2021-12-20 10:12:09 +00:00
|
|
|
|
2021-04-07 22:03:02 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
.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();
|
2022-02-26 23:34:57 +00:00
|
|
|
while let Some(chunk) = field.try_next().await.map_err(error::ErrorBadRequest)? {
|
|
|
|
data.extend(chunk);
|
2020-07-08 19:26:46 +00:00
|
|
|
}
|
2020-07-09 17:27:24 +00:00
|
|
|
Ok(data)
|
|
|
|
}
|
|
|
|
|
2021-09-11 00:08:47 +00:00
|
|
|
async fn create_file(
|
|
|
|
filename: &Path,
|
|
|
|
field: Field,
|
|
|
|
max_file_size: Option<u64>,
|
|
|
|
) -> Result<u64, Error> {
|
|
|
|
let mut file = File::create(&filename).await.map_err(|file_err| {
|
|
|
|
log::error!("could not create file {:?}", file_err);
|
|
|
|
error::ErrorInternalServerError("could not create file")
|
|
|
|
})?;
|
|
|
|
let written_bytes = write_to_file(&mut file, field, max_file_size).await?;
|
|
|
|
Ok(written_bytes)
|
|
|
|
}
|
|
|
|
|
2020-07-09 17:27:24 +00:00
|
|
|
async fn write_to_file(
|
|
|
|
file: &mut File,
|
2021-09-11 00:08:47 +00:00
|
|
|
mut field: Field,
|
2021-03-09 22:36:24 +00:00
|
|
|
max_size: Option<u64>,
|
2021-04-07 22:03:02 +00:00
|
|
|
) -> Result<u64, 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)?;
|
2021-04-07 22:03:02 +00:00
|
|
|
written_bytes += chunk.len() as u64;
|
2021-03-09 22:36:24 +00:00
|
|
|
if let Some(max_size) = max_size {
|
|
|
|
if written_bytes > max_size {
|
|
|
|
return Err(error::ErrorBadRequest(format!(
|
|
|
|
"exceeded maximum file size of {} bytes",
|
|
|
|
max_size
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
}
|
2022-05-09 21:07:24 +00:00
|
|
|
file.write_all(&chunk).await.map_err(|write_err| {
|
|
|
|
log::error!("could not write file {:?}", write_err);
|
|
|
|
error::ErrorInternalServerError("could not write file")
|
|
|
|
})?;
|
2020-07-09 17:27:24 +00:00
|
|
|
}
|
2021-04-07 22:03:02 +00:00
|
|
|
Ok(written_bytes)
|
2020-07-09 17:27:24 +00:00
|
|
|
}
|
|
|
|
|
2022-02-26 23:34:57 +00:00
|
|
|
fn get_original_filename(field: &actix_multipart::Field) -> Option<&String> {
|
2021-12-20 10:12:09 +00:00
|
|
|
field
|
2022-02-26 23:34:57 +00:00
|
|
|
.content_disposition()
|
2021-12-20 10:12:09 +00:00
|
|
|
.parameters
|
2022-02-26 23:34:57 +00:00
|
|
|
.iter()
|
2021-12-20 10:12:09 +00:00
|
|
|
.find_map(|param| match param {
|
|
|
|
DispositionParam::Filename(filename) => Some(filename),
|
|
|
|
_ => None,
|
|
|
|
})
|
2020-07-08 19:26:46 +00:00
|
|
|
}
|