Skip to content

Commit

Permalink
Add guess_mime_type_str
Browse files Browse the repository at this point in the history
  • Loading branch information
lnicola committed Oct 1, 2018
1 parent e0394dd commit 85aa234
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/lib.rs
Expand Up @@ -69,9 +69,24 @@ pub fn guess_mime_type<P: AsRef<Path>>(path: P) -> Mime {
///
/// Take care when processing files with assumptions based on the return value of this function.
pub fn guess_mime_type_opt<P: AsRef<Path>>(path: P) -> Option<Mime> {
guess_mime_type_str(path)
.map(|mime| mime.parse::<Mime>().unwrap())
}

/// Guess the MIME type string of `path` by its extension (as defined by `Path::extension()`).
///
/// If `path` has no extension, or its extension has no known MIME type mapping,
/// then `None` is returned.
///
/// ## Note
/// **Guess** is the operative word here, as there are no guarantees that the contents of the file
/// that `path` points to match the MIME type associated with the path's extension.
///
/// Take care when processing files with assumptions based on the return value of this function.
pub fn guess_mime_type_str<P: AsRef<Path>>(path: P) -> Option<&'static str> {
let ext = path.as_ref().extension().and_then(OsStr::to_str).unwrap_or("");

get_mime_type_opt(ext)
get_mime_type_str(ext)
}

/// Get the MIME type associated with a file extension.
Expand Down

0 comments on commit 85aa234

Please sign in to comment.