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]]
name = "datatrash"
version = "1.1.7"
version = "1.1.8"
dependencies = [
"actix-files",
"actix-governor",

View File

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

View File

@ -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<String>) {
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)