Skip to content

Commit fb6c8a7

Browse files
ThinkerDreamerpaolobarbolini
authored andcommitted
feat: use mime_guess for MIME types
1 parent a62c64b commit fb6c8a7

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

Cargo.lock

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

static-serve-macro/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ proc-macro = true
1616
display_full_error = "1.1"
1717
flate2 = "1.1"
1818
glob = "0.3"
19+
mime_guess = "2.0.5"
1920
proc-macro2 = "1.0"
2021
quote = "1.0"
2122
sha1 = "0.10"

static-serve-macro/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use thiserror::Error;
1111
pub(crate) enum Error {
1212
#[error("{}", UnknownFileExtension(.0.as_deref()))]
1313
UnknownFileExtension(Option<OsString>),
14+
#[error("File extension for file {} is not valid unicode", 0.to_string())]
15+
InvalidFileExtension(OsString),
1416
#[error("Cannot canonicalize assets directory")]
1517
CannotCanonicalizeDirectory(#[source] io::Error),
1618
#[error("Invalid unicode in directory name")]

static-serve-macro/src/lib.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -357,15 +357,28 @@ fn maybe_get_compressed(compressed: &[u8], contents: &[u8]) -> Option<LitByteStr
357357
.then(|| LitByteStr::new(compressed, Span::call_site()))
358358
}
359359

360-
fn file_content_type(path: &Path) -> Result<&'static str, error::Error> {
360+
/// Use `mime_guess` to get the best guess of the file's MIME type
361+
/// by looking at its extension, or return an error if unable.
362+
///
363+
/// We accept the first guess because [`mime_guess` updates the order
364+
/// according to the latest IETF RTC](https://docs.rs/mime_guess/2.0.5/mime_guess/struct.MimeGuess.html#note-ordering)
365+
fn file_content_type(path: &Path) -> Result<String, error::Error> {
361366
match path.extension() {
362-
Some(ext) if ext.eq_ignore_ascii_case("css") => Ok("text/css"),
363-
Some(ext) if ext.eq_ignore_ascii_case("js") => Ok("text/javascript"),
364-
Some(ext) if ext.eq_ignore_ascii_case("txt") => Ok("text/plain"),
365-
Some(ext) if ext.eq_ignore_ascii_case("woff") => Ok("font/woff"),
366-
Some(ext) if ext.eq_ignore_ascii_case("woff2") => Ok("font/woff2"),
367-
Some(ext) if ext.eq_ignore_ascii_case("svg") => Ok("image/svg+xml"),
368-
ext => Err(error::Error::UnknownFileExtension(ext.map(Into::into))),
367+
Some(ext) => {
368+
let guesses = mime_guess::MimeGuess::from_ext(
369+
ext.to_str()
370+
.ok_or(error::Error::InvalidFileExtension(path.into()))?,
371+
);
372+
373+
if let Some(guess) = guesses.first_raw() {
374+
Ok(guess.to_owned())
375+
} else {
376+
Err(error::Error::UnknownFileExtension(
377+
path.extension().map(Into::into),
378+
))
379+
}
380+
}
381+
None => Err(error::Error::UnknownFileExtension(None)),
369382
}
370383
}
371384

0 commit comments

Comments
 (0)