use mime_guess for guessing mime

This commit is contained in:
panicbit 2020-11-18 22:58:23 +01:00
parent ddf9c4b9f8
commit 5a92d8d28f
2 changed files with 9 additions and 11 deletions

View File

@ -21,6 +21,7 @@ itertools = "0.9.0"
log = "0.4.11" log = "0.4.11"
webpki = "0.21.0" webpki = "0.21.0"
lazy_static = "1.4.0" lazy_static = "1.4.0"
mime_guess = "2.0.3"
[dev-dependencies] [dev-dependencies]
env_logger = "0.8.1" env_logger = "0.8.1"

View File

@ -5,7 +5,6 @@ use tokio::{
fs::{self, File}, fs::{self, File},
io, io,
}; };
use crate::GEMINI_MIME_STR;
use crate::types::{Response, Document, document::HeadingLevel::*}; use crate::types::{Response, Document, document::HeadingLevel::*};
use itertools::Itertools; use itertools::Itertools;
@ -89,18 +88,16 @@ async fn serve_dir_listing<P: AsRef<Path>, B: AsRef<Path>>(path: P, virtual_path
pub fn guess_mime_from_path<P: AsRef<Path>>(path: P) -> Mime { pub fn guess_mime_from_path<P: AsRef<Path>>(path: P) -> Mime {
let path = path.as_ref(); let path = path.as_ref();
let extension = path.extension().and_then(|s| s.to_str()); let extension = path.extension().and_then(|s| s.to_str());
let mime = match extension { let extension = match extension {
Some(extension) => match extension { Some(extension) => extension,
"gemini" | "gmi" => GEMINI_MIME_STR, None => return mime::APPLICATION_OCTET_STREAM,
"txt" => "text/plain",
"jpeg" | "jpg" | "jpe" => "image/jpeg",
"png" => "image/png",
_ => "application/octet-stream",
},
None => "application/octet-stream",
}; };
mime.parse::<Mime>().unwrap_or(mime::APPLICATION_OCTET_STREAM) if let "gemini" | "gmi" = extension {
return crate::GEMINI_MIME.clone();
}
mime_guess::from_ext(extension).first_or_octet_stream()
} }
/// A convenience trait alias for `AsRef<T> + Into<T::Owned>`, /// A convenience trait alias for `AsRef<T> + Into<T::Owned>`,