perform mime guess on application/octet-stream upload

This commit is contained in:
neri 2022-09-30 15:42:08 +02:00
parent 44aa90369d
commit e4857cde42
3 changed files with 14 additions and 9 deletions

2
Cargo.lock generated
View File

@ -427,7 +427,7 @@ dependencies = [
[[package]] [[package]]
name = "datatrash" name = "datatrash"
version = "1.1.7" version = "1.1.8"
dependencies = [ dependencies = [
"actix-files", "actix-files",
"actix-governor", "actix-governor",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "datatrash" name = "datatrash"
version = "1.1.7" version = "1.1.8"
authors = ["neri"] authors = ["neri"]
edition = "2021" edition = "2021"

View File

@ -2,7 +2,7 @@ use crate::config;
use actix_multipart::{Field, Multipart}; use actix_multipart::{Field, Multipart};
use actix_web::{error, http::header::DispositionParam, Error}; use actix_web::{error, http::header::DispositionParam, Error};
use futures_util::{StreamExt, TryStreamExt}; use futures_util::{StreamExt, TryStreamExt};
use mime::{Mime, TEXT_PLAIN}; use mime::{Mime, APPLICATION_OCTET_STREAM, TEXT_PLAIN};
use std::path::Path; use std::path::Path;
use time::{Duration, OffsetDateTime}; use time::{Duration, OffsetDateTime};
use tokio::{fs::File, io::AsyncWriteExt}; use tokio::{fs::File, io::AsyncWriteExt};
@ -38,12 +38,17 @@ pub(crate) async fn parse_multipart(
} }
"file" => { "file" => {
let (mime, uploaded_name) = get_file_metadata(&field); 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; continue;
} }
original_name = uploaded_name.map(|f| f.to_string()); original_name = uploaded_name;
content_type = Some(mime.clone());
size = create_file(file_path, field, config.max_file_size).await?; 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" => { "text" => {
if original_name.is_some() { if original_name.is_some() {
@ -179,14 +184,14 @@ async fn write_to_file(
Ok(written_bytes) Ok(written_bytes)
} }
fn get_file_metadata(field: &actix_multipart::Field) -> (&Mime, Option<&String>) { fn get_file_metadata(field: &actix_multipart::Field) -> (Mime, Option<String>) {
let mime = field.content_type(); let mime = field.content_type().clone();
let filename = field let filename = field
.content_disposition() .content_disposition()
.parameters .parameters
.iter() .iter()
.find_map(|param| match param { .find_map(|param| match param {
DispositionParam::Filename(filename) => Some(filename), DispositionParam::Filename(filename) => Some(filename.clone()),
_ => None, _ => None,
}); });
(mime, filename) (mime, filename)