Skip to content

Commit

Permalink
Mime check fixes. (#5213)
Browse files Browse the repository at this point in the history
* Mime check fixes.

* Adding back comment.
  • Loading branch information
dessalines authored Nov 19, 2024
1 parent 73f516d commit 170e3b1
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions crates/api_common/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use lemmy_utils::{
REQWEST_TIMEOUT,
VERSION,
};
use mime::Mime;
use reqwest::{
header::{CONTENT_TYPE, RANGE},
Client,
Expand Down Expand Up @@ -64,20 +63,20 @@ pub async fn fetch_link_metadata(url: &Url, context: &LemmyContext) -> LemmyResu
.await?
.error_for_status()?;

let mut content_type: Option<Mime> = 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
Expand Down

0 comments on commit 170e3b1

Please sign in to comment.