diff --git a/Cargo.lock b/Cargo.lock index 4f5fd9f..a36657f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -893,6 +893,7 @@ dependencies = [ "http-body-util", "infer", "jsonwebtoken", + "mime_guess", "password-hash", "reqwest", "serde", diff --git a/Cargo.toml b/Cargo.toml index bc7a83d..a06e19d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ clap = { version = "4.5.53", features = ["derive"] } dashmap = "6.1.0" infer = "0.19.0" jsonwebtoken = "9.3.1" +mime_guess = "2.0.5" password-hash = "0.5.0" serde = { version = "1.0.219", features = ["derive"] } serde_json = "1.0.143" diff --git a/src/routes/messages.rs b/src/routes/messages.rs index 59e0264..3749632 100644 --- a/src/routes/messages.rs +++ b/src/routes/messages.rs @@ -406,17 +406,21 @@ async fn create_message_with_attachments( return Err(APIError::FileTooLarge); } - // Determine file type - let kind = infer::get(&data); - let mime = kind - .map(|k| k.mime_type()) + let mut mime = mime_guess::from_path(&file_name) + .first_raw() .unwrap_or("application/octet-stream"); + if mime == "application/octet-stream" { + if let Some(kind) = infer::get(&data) { + mime = kind.mime_type(); + } + } + let file_type = if mime.starts_with("image/") { "image" } else if mime.starts_with("video/") { "video" - } else if mime.starts_with("text/") { + } else if mime.starts_with("text/") || std::str::from_utf8(&data).is_ok() { "textfile" } else { "binaryfile"