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 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::{ use tokio::{fs::File, io::AsyncWriteExt};
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);
@ -25,25 +21,6 @@ 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,

View File

@ -41,12 +41,28 @@ 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,
} = 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(|| { let file_name = original_name.clone().unwrap_or_else(|| {
format!( format!(
@ -140,17 +156,15 @@ fn get_file_url(req: &HttpRequest, id: &str, name: Option<&str>) -> String {
} }
} }
pub async fn uploaded( pub async fn uploaded(req: HttpRequest) -> Result<HttpResponse, Error> {
req: HttpRequest, let id = req.match_info().query("id");
path: web::Path<(String, Option<String>)>, let name = req.match_info().get("name");
) -> Result<HttpResponse, Error> {
let (id, name) = path.into_inner();
let upload_html = if name.is_some() { let upload_html = if name.is_some() {
UPLOAD_SHORT_HTML UPLOAD_SHORT_HTML
.replace("{link}", &get_file_url(&req, &id, name.as_deref())) .replace("{link}", &get_file_url(&req, id, name))
.replace("{shortlink}", &get_file_url(&req, &id, None)) .replace("{shortlink}", &get_file_url(&req, id, None))
} else { } 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() Ok(HttpResponse::Ok()
.content_type("text/html") .content_type("text/html")