From 170e3b101c8c886877ac05754a08a442824d7a41 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Tue, 19 Nov 2024 08:46:18 -0500 Subject: [PATCH] Mime check fixes. (#5213) * Mime check fixes. * Adding back comment. --- crates/api_common/src/request.rs | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/crates/api_common/src/request.rs b/crates/api_common/src/request.rs index dc77ba866a..cc506b896d 100644 --- a/crates/api_common/src/request.rs +++ b/crates/api_common/src/request.rs @@ -23,7 +23,6 @@ use lemmy_utils::{ REQWEST_TIMEOUT, VERSION, }; -use mime::Mime; use reqwest::{ header::{CONTENT_TYPE, RANGE}, Client, @@ -64,20 +63,20 @@ pub async fn fetch_link_metadata(url: &Url, context: &LemmyContext) -> LemmyResu .await? .error_for_status()?; - let mut content_type: Option = response - .headers() - .get(CONTENT_TYPE) - .and_then(|h| h.to_str().ok()) - .and_then(|h| h.parse().ok()); - // In some cases servers send a wrong mime type for images, which prevents thumbnail // generation. To avoid this we also try to guess the mime type from file extension. - let guess = mime_guess::from_path(url.path()); - if let Some(guess) = guess.first() { - if guess.type_() == mime::IMAGE { - content_type = Some(guess); - } - } + let content_type = mime_guess::from_path(url.path()) + .first() + // If you can guess that its an image type, then return that first. + .filter(|guess| guess.type_() == mime::IMAGE) + // Otherwise, get the content type from the headers + .or( + response + .headers() + .get(CONTENT_TYPE) + .and_then(|h| h.to_str().ok()) + .and_then(|h| h.parse().ok()), + ); let opengraph_data = { // if the content type is not text/html, we don't need to parse it