From e4857cde42e3922cac4f3cc1ce1b86a91555dd64 Mon Sep 17 00:00:00 2001 From: neri Date: Fri, 30 Sep 2022 15:42:08 +0200 Subject: [PATCH] perform mime guess on application/octet-stream upload --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/multipart.rs | 19 ++++++++++++------- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 50011ca..833a9c6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -427,7 +427,7 @@ dependencies = [ [[package]] name = "datatrash" -version = "1.1.7" +version = "1.1.8" dependencies = [ "actix-files", "actix-governor", diff --git a/Cargo.toml b/Cargo.toml index 55fa49b..68201ff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "datatrash" -version = "1.1.7" +version = "1.1.8" authors = ["neri"] edition = "2021" diff --git a/src/multipart.rs b/src/multipart.rs index dd0af39..1f68a79 100644 --- a/src/multipart.rs +++ b/src/multipart.rs @@ -2,7 +2,7 @@ use crate::config; use actix_multipart::{Field, Multipart}; use actix_web::{error, http::header::DispositionParam, Error}; use futures_util::{StreamExt, TryStreamExt}; -use mime::{Mime, TEXT_PLAIN}; +use mime::{Mime, APPLICATION_OCTET_STREAM, TEXT_PLAIN}; use std::path::Path; use time::{Duration, OffsetDateTime}; use tokio::{fs::File, io::AsyncWriteExt}; @@ -38,12 +38,17 @@ pub(crate) async fn parse_multipart( } "file" => { let (mime, uploaded_name) = get_file_metadata(&field); - if uploaded_name == None || uploaded_name.map(|f| f.as_str()) == Some("") { + if uploaded_name == None || uploaded_name.as_deref() == Some("") { continue; } - original_name = uploaded_name.map(|f| f.to_string()); - content_type = Some(mime.clone()); + original_name = uploaded_name; size = create_file(file_path, field, config.max_file_size).await?; + println!("mime: {}", mime); + content_type = Some(if mime == APPLICATION_OCTET_STREAM { + get_content_type(file_path) + } else { + mime.clone() + }); } "text" => { if original_name.is_some() { @@ -179,14 +184,14 @@ async fn write_to_file( Ok(written_bytes) } -fn get_file_metadata(field: &actix_multipart::Field) -> (&Mime, Option<&String>) { - let mime = field.content_type(); +fn get_file_metadata(field: &actix_multipart::Field) -> (Mime, Option) { + let mime = field.content_type().clone(); let filename = field .content_disposition() .parameters .iter() .find_map(|param| match param { - DispositionParam::Filename(filename) => Some(filename), + DispositionParam::Filename(filename) => Some(filename.clone()), _ => None, }); (mime, filename)