Use mime_guess for file extension MIME type detection

Replace the hand-maintained extension-to-MIME mapping with the
mime_guess crate, which was already in the dependency tree.
This covers hundreds of file extensions out of the box.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Slavi Pantaleev
2026-03-20 12:17:05 +02:00
parent 3d3bd3c9f9
commit f3d1b32ad7

View File

@@ -17,30 +17,7 @@ pub fn get_file_extension(mime_type: &mime::Mime) -> String {
}
pub fn get_mime_type_from_file_name(file_name: &str) -> mime::Mime {
let extension = file_name.rsplit('.').next().unwrap_or("");
match extension.to_lowercase().as_str() {
"jpg" | "jpeg" => mime::IMAGE_JPEG,
"png" => mime::IMAGE_PNG,
"gif" => mime::IMAGE_GIF,
"webp" => "image/webp".parse().unwrap(),
"svg" => mime::IMAGE_SVG,
"tiff" | "tif" => "image/tiff".parse().unwrap(),
"bmp" => "image/bmp".parse().unwrap(),
"heic" | "heif" => "image/heic".parse().unwrap(),
"avif" => "image/avif".parse().unwrap(),
"pdf" => "application/pdf".parse().unwrap(),
"txt" | "log" => mime::TEXT_PLAIN,
"csv" => mime::TEXT_CSV,
"json" => mime::APPLICATION_JSON,
"xml" => "application/xml".parse().unwrap(),
"html" | "htm" => mime::TEXT_HTML,
"md" | "markdown" => "text/markdown".parse().unwrap(),
"yaml" | "yml" => "application/yaml".parse().unwrap(),
"toml" => "application/toml".parse().unwrap(),
"rs" | "py" | "js" | "ts" | "go" | "c" | "cpp" | "h" | "java" | "rb" | "sh" | "css" => {
mime::TEXT_PLAIN
}
_ => mime::APPLICATION_OCTET_STREAM,
}
mime_guess::from_path(file_name)
.first()
.unwrap_or(mime::APPLICATION_OCTET_STREAM)
}