const TYPES: &[(&str, &str)] = &[
("png", "image/png"),
("jpg", "image/jpeg"),
("jpeg", "image/jpeg"),
("gif", "image/gif"),
("webp", "image/webp"),
("avif", "image/avif"),
("bmp", "image/bmp"),
("svg", "image/svg+xml"),
("mp3", "audio/mpeg"),
("wav", "audio/wav"),
("opus", "audio/ogg"),
("ogg", "audio/ogg"),
("flac", "audio/flac"),
("aac", "audio/aac"),
("pcm", "audio/pcm"),
("m4a", "audio/mp4"),
("mp4", "video/mp4"),
("webm", "video/webm"),
("mov", "video/quicktime"),
("mkv", "video/x-matroska"),
("avi", "video/x-msvideo"),
];
pub fn extension(content_type: &str) -> &str {
let essence = content_type.split(';').next().unwrap_or_default().trim();
match essence.to_ascii_lowercase().as_str() {
"audio/mpeg" => return "mp3",
"audio/ogg" => return "opus",
"audio/x-wav" | "audio/wave" | "audio/vnd.wave" => return "wav",
"audio/mp4" | "audio/x-m4a" => return "m4a",
"video/x-matroska" => return "mkv",
"video/x-msvideo" => return "avi",
"video/quicktime" => return "mov",
"image/svg+xml" => return "svg",
_ => {}
}
essence
.split_once('/')
.map(|(_, subtype)| subtype)
.filter(|subtype| !subtype.is_empty() && subtype.chars().all(|c| c.is_ascii_alphanumeric()))
.unwrap_or("bin")
}
pub fn from_path(path_or_url: &str) -> Option<&'static str> {
let path = path_or_url.split(['?', '#']).next().unwrap_or_default();
let file = path.rsplit(['/', '\\']).next().unwrap_or_default();
let (_, extension) = file.rsplit_once('.')?;
TYPES
.iter()
.find(|(known, _)| known.eq_ignore_ascii_case(extension))
.map(|(_, content_type)| *content_type)
}
pub fn with_extension(name: &str, content_type: &str) -> String {
if from_path(name).is_some() {
name.to_string()
} else {
format!("{name}.{}", extension(content_type))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn extension_follows_the_subtype_with_a_few_exceptions() {
for (content_type, expected) in [
("image/png", "png"),
("image/jpeg; q=1", "jpeg"),
("audio/webm;codecs=opus", "webm"),
("audio/mpeg; charset=x", "mp3"),
("audio/ogg", "opus"),
("video/quicktime", "mov"),
("image/svg+xml", "svg"),
("application/octet-stream", "bin"),
("audio/x-wav", "wav"),
("video/x-matroska", "mkv"),
("application/x-unknown", "bin"),
("video", "bin"),
("", "bin"),
] {
assert_eq!(extension(content_type), expected, "{content_type}");
}
}
#[test]
fn from_path_reads_the_last_extension_of_the_file() {
for (path, expected) in [
("a.PNG", Some("image/png")),
(
"https://example.com/v.webm?x=1.png#t.gif",
Some("video/webm"),
),
("dir.mp4/file", None),
("voice.opus", Some("audio/ogg")),
("notes.txt", None),
("noextension", None),
] {
assert_eq!(from_path(path), expected, "{path}");
}
}
#[test]
fn with_extension_keeps_only_known_extensions() {
for (name, content_type, expected) in [
("intro", "audio/mpeg", "intro.mp3"),
("intro.v2", "audio/mpeg", "intro.v2.mp3"),
("intro.wav", "audio/mpeg", "intro.wav"),
("my.picture", "image/png", "my.picture.png"),
] {
assert_eq!(with_extension(name, content_type), expected);
}
}
}