Compare commits

..

No commits in common. "b9849153f06459294af6c09d0d3f09daa9885349" and "2de28ca5dbd671a4077d7672f9669103ba536e15" have entirely different histories.

2 changed files with 24 additions and 33 deletions

View File

@ -5,14 +5,10 @@ use futures_util::{StreamExt, TryStreamExt};
use mime::{Mime, APPLICATION_OCTET_STREAM, TEXT_PLAIN};
use std::{
cmp::{max, min},
io::ErrorKind,
path::Path,
};
use time::{Duration, OffsetDateTime};
use tokio::{
fs::{self, File},
io::AsyncWriteExt,
};
use tokio::{fs::File, io::AsyncWriteExt};
const MAX_UPLOAD_DURATION: Duration = Duration::days(31);
const DEFAULT_UPLOAD_DURATION: Duration = Duration::minutes(30);
@ -25,25 +21,6 @@ pub(crate) struct UploadConfig {
}
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,
file_path: &Path,
config: &config::Config,

View File

@ -41,12 +41,28 @@ pub async fn upload(
error::ErrorInternalServerError("could not create file")
})?;
let parsed_multipart = multipart::parse_multipart(payload, &file_name, &config).await;
let UploadConfig {
original_name,
content_type,
valid_till,
delete_on_download,
} = multipart::parse_multipart(payload, &file_name, &config).await?;
} = match parsed_multipart {
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(|| {
format!(
@ -140,17 +156,15 @@ fn get_file_url(req: &HttpRequest, id: &str, name: Option<&str>) -> String {
}
}
pub async fn uploaded(
req: HttpRequest,
path: web::Path<(String, Option<String>)>,
) -> Result<HttpResponse, Error> {
let (id, name) = path.into_inner();
pub async fn uploaded(req: HttpRequest) -> Result<HttpResponse, Error> {
let id = req.match_info().query("id");
let name = req.match_info().get("name");
let upload_html = if name.is_some() {
UPLOAD_SHORT_HTML
.replace("{link}", &get_file_url(&req, &id, name.as_deref()))
.replace("{shortlink}", &get_file_url(&req, &id, None))
.replace("{link}", &get_file_url(&req, id, name))
.replace("{shortlink}", &get_file_url(&req, id, None))
} else {
UPLOAD_HTML.replace("{link}", &get_file_url(&req, &id, name.as_deref()))
UPLOAD_HTML.replace("{link}", &get_file_url(&req, id, name))
};
Ok(HttpResponse::Ok()
.content_type("text/html")