diff --git a/src/file_info.rs b/src/file_info.rs index 35ad601..9b6e396 100644 --- a/src/file_info.rs +++ b/src/file_info.rs @@ -36,7 +36,10 @@ pub async fn find_by_id( .await } -pub async fn create(file_info: &FileInfo, db: &sqlx::Pool) -> Result<(), sqlx::Error> { +pub async fn create( + file_info: &FileInfo, + db: &sqlx::Pool, +) -> Result<(), sqlx::Error> { sqlx::query( "INSERT INTO Files (file_id, file_name, content_type, valid_till, delete_on_download) \ VALUES ($1, $2, $3, $4, $5)", diff --git a/src/main.rs b/src/main.rs index c5589da..28ba4cb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,7 @@ mod upload; use crate::rate_limit::ForwardedPeerIpKeyExtractor; use actix_files::Files; use actix_governor::{Governor, GovernorConfigBuilder}; +use actix_web::middleware::from_fn; use actix_web::{ http::header::{ HeaderName, CROSS_ORIGIN_OPENER_POLICY, PERMISSIONS_POLICY, REFERRER_POLICY, @@ -22,7 +23,6 @@ use actix_web::{ web::{self, Data}, App, Error, HttpResponse, HttpServer, }; -use actix_web::middleware::from_fn; use env_logger::Env; use sqlx::postgres::PgPool; use std::env; diff --git a/src/mime_relations.rs b/src/mime_relations.rs index a20d482..1f8133f 100644 --- a/src/mime_relations.rs +++ b/src/mime_relations.rs @@ -8,7 +8,7 @@ use mime::Mime; lazy_static! { static ref ALIASES: HashMap = load_mime_aliases(); - static ref PARENTS: Vec<(Mime, Mime)> = load_mime_parent_relations(); + static ref PARENTS: HashMap> = load_mime_parent_relations(); static ref EXTENSIONS: HashMap = load_mime_extensions(); } @@ -20,14 +20,18 @@ fn load_mime_aliases() -> HashMap { .collect() } -fn load_mime_parent_relations() -> Vec<(Mime, Mime)> { - tree_magic_db::subclasses() +fn load_mime_parent_relations() -> HashMap> { + let mut parents = HashMap::>::new(); + let subclasses = tree_magic_db::subclasses() .lines() .filter_map(|line| line.split_once(' ')) .filter_map(|(child, parent)| { Some((Mime::from_str(child).ok()?, Mime::from_str(parent).ok()?)) - }) - .collect() + }); + for (child, parent) in subclasses { + parents.entry(child).or_default().push(parent); + } + parents } fn load_mime_extensions() -> HashMap { @@ -40,25 +44,22 @@ fn load_mime_extensions() -> HashMap { .collect() } -pub(crate) fn get_alias(mimetype: Mime) -> Mime { +pub fn resolve_alias(mimetype: Mime) -> Mime { ALIASES.get(&mimetype).cloned().unwrap_or(mimetype) } -fn get_mime_parents(mimetype: &Mime) -> Vec<&Mime> { - PARENTS - .iter() - .filter_map(|(child, parent)| (child == mimetype).then_some(parent)) - .collect() +fn get_mime_parents(mimetype: &Mime) -> &[Mime] { + PARENTS.get(mimetype).map(Vec::as_slice).unwrap_or(&[]) } -pub(crate) fn matches_text(mime: &Mime) -> bool { +pub fn matches_text(mime: &Mime) -> bool { if mime.type_() == mime::TEXT { return true; } - return get_mime_parents(mime).into_iter().any(matches_text); + get_mime_parents(mime).iter().any(matches_text) } -pub(crate) fn get_extension(mimetype: &Mime) -> Option<&'static str> { +pub fn get_extension(mimetype: &Mime) -> Option<&'static str> { let mut queue = VecDeque::new(); queue.push_back(mimetype); while let Some(mime) = queue.pop_front() { diff --git a/src/multipart.rs b/src/multipart.rs index 1a19f89..8b14c5e 100644 --- a/src/multipart.rs +++ b/src/multipart.rs @@ -73,7 +73,7 @@ pub(crate) async fn parse_multipart_inner( (size, first_bytes) = create_file(file_path, field, config.max_file_size).await?; content_type = Some( mime.filter(|mime| *mime != APPLICATION_OCTET_STREAM) - .map(mime_relations::get_alias) + .map(mime_relations::resolve_alias) .or_else(|| get_content_type(&first_bytes)) .unwrap_or(APPLICATION_OCTET_STREAM), ); diff --git a/src/script_nonce.rs b/src/script_nonce.rs index 806fa72..9bd81d0 100644 --- a/src/script_nonce.rs +++ b/src/script_nonce.rs @@ -1,10 +1,10 @@ +use actix_web::middleware::Next; use actix_web::{ body::MessageBody, dev::{ServiceRequest, ServiceResponse}, http::header::{HeaderValue, CONTENT_SECURITY_POLICY}, Error, HttpMessage, }; -use actix_web::middleware::Next; use rand::Rng; use std::fmt::Display; diff --git a/src/template.rs b/src/template.rs index 558ac27..be39c51 100644 --- a/src/template.rs +++ b/src/template.rs @@ -133,7 +133,9 @@ pub fn insert_abuse_template(html: String, req: Option<&HttpRequest>, config: &C pub fn insert_script_nonce(req: &HttpRequest, html: String) -> String { let extensions = &req.extensions(); - let script_nonce = extensions.get::().expect("script_nonce available"); + let script_nonce = extensions + .get::() + .expect("script_nonce available"); html.replace("{script_nonce}", &script_nonce.to_string()) } diff --git a/src/upload.rs b/src/upload.rs index d5f2447..ae30f71 100644 --- a/src/upload.rs +++ b/src/upload.rs @@ -21,7 +21,7 @@ pub async fn index(req: HttpRequest, config: web::Data) -> HttpResponse let index_html = template::build_index_html(&req, &config); HttpResponse::Ok() .content_type("text/html") - .body(index_html) + .body(index_html) } pub async fn upload(