move multipart error handling into its module
This commit is contained in:
parent
2de28ca5db
commit
dc2f7ecab0
|
@ -5,10 +5,14 @@ use futures_util::{StreamExt, TryStreamExt};
|
||||||
use mime::{Mime, APPLICATION_OCTET_STREAM, TEXT_PLAIN};
|
use mime::{Mime, APPLICATION_OCTET_STREAM, TEXT_PLAIN};
|
||||||
use std::{
|
use std::{
|
||||||
cmp::{max, min},
|
cmp::{max, min},
|
||||||
|
io::ErrorKind,
|
||||||
path::Path,
|
path::Path,
|
||||||
};
|
};
|
||||||
use time::{Duration, OffsetDateTime};
|
use time::{Duration, OffsetDateTime};
|
||||||
use tokio::{fs::File, io::AsyncWriteExt};
|
use tokio::{
|
||||||
|
fs::{self, File},
|
||||||
|
io::AsyncWriteExt,
|
||||||
|
};
|
||||||
|
|
||||||
const MAX_UPLOAD_DURATION: Duration = Duration::days(31);
|
const MAX_UPLOAD_DURATION: Duration = Duration::days(31);
|
||||||
const DEFAULT_UPLOAD_DURATION: Duration = Duration::minutes(30);
|
const DEFAULT_UPLOAD_DURATION: Duration = Duration::minutes(30);
|
||||||
|
@ -21,6 +25,25 @@ pub(crate) struct UploadConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn parse_multipart(
|
pub(crate) async fn parse_multipart(
|
||||||
|
payload: Multipart,
|
||||||
|
file_path: &Path,
|
||||||
|
config: &config::Config,
|
||||||
|
) -> Result<UploadConfig, error::Error> {
|
||||||
|
match parse_multipart_inner(payload, file_path, config).await {
|
||||||
|
Ok(data) => Ok(data),
|
||||||
|
Err(err) => {
|
||||||
|
match fs::remove_file(file_path).await {
|
||||||
|
Err(err) if err.kind() != ErrorKind::NotFound => {
|
||||||
|
log::error!("could not remove file {:?}", err);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn parse_multipart_inner(
|
||||||
mut payload: Multipart,
|
mut payload: Multipart,
|
||||||
file_path: &Path,
|
file_path: &Path,
|
||||||
config: &config::Config,
|
config: &config::Config,
|
||||||
|
|
|
@ -41,28 +41,12 @@ pub async fn upload(
|
||||||
error::ErrorInternalServerError("could not create file")
|
error::ErrorInternalServerError("could not create file")
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let parsed_multipart = multipart::parse_multipart(payload, &file_name, &config).await;
|
|
||||||
let UploadConfig {
|
let UploadConfig {
|
||||||
original_name,
|
original_name,
|
||||||
content_type,
|
content_type,
|
||||||
valid_till,
|
valid_till,
|
||||||
delete_on_download,
|
delete_on_download,
|
||||||
} = match parsed_multipart {
|
} = multipart::parse_multipart(payload, &file_name, &config).await?;
|
||||||
Ok(data) => data,
|
|
||||||
Err(err) => {
|
|
||||||
match fs::remove_file(file_name).await {
|
|
||||||
Ok(()) => {}
|
|
||||||
Err(err) if err.kind() == ErrorKind::NotFound => {}
|
|
||||||
Err(err) => {
|
|
||||||
log::error!("could not remove file {:?}", err);
|
|
||||||
return Err(error::ErrorInternalServerError(
|
|
||||||
"could not parse multipart; could not remove file",
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Err(err);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let file_name = original_name.clone().unwrap_or_else(|| {
|
let file_name = original_name.clone().unwrap_or_else(|| {
|
||||||
format!(
|
format!(
|
||||||
|
|
Loading…
Reference in New Issue