From 3751656722ca4f0a220a6555ec37d270e6b6d06c Mon Sep 17 00:00:00 2001 From: axon-q Date: Sat, 9 Jun 2018 11:20:06 +0000 Subject: [PATCH] expose fs::file_extension_to_mime() function --- src/fs.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/fs.rs b/src/fs.rs index a4418bce3..307557785 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -29,6 +29,14 @@ use param::FromParam; /// Env variable for default cpu pool size for `StaticFiles` const ENV_CPU_POOL_VAR: &str = "ACTIX_FS_POOL"; +/// Return the MIME type associated with a filename extension (case-insensitive). +/// If `ext` is empty or no associated type for the extension was found, returns +/// the type `application/octet-stream`. +#[inline] +pub fn file_extension_to_mime(ext: &str) -> mime::Mime { + get_mime_type(ext) +} + /// A file with an associated name; responds with the Content-Type based on the /// file extension. #[derive(Debug)] @@ -692,6 +700,18 @@ mod tests { use http::{header, Method, StatusCode}; use test::{self, TestRequest}; + #[test] + fn test_file_extension_to_mime() { + let m = file_extension_to_mime("jpg"); + assert_eq!(m, mime::IMAGE_JPEG); + + let m = file_extension_to_mime("invalid extension!!"); + assert_eq!(m, mime::APPLICATION_OCTET_STREAM); + + let m = file_extension_to_mime(""); + assert_eq!(m, mime::APPLICATION_OCTET_STREAM); + } + #[test] fn test_named_file_text() { assert!(NamedFile::open("test--").is_err());