Skip to content

Commit

Permalink
Revert "Guess image mime type from file extension (fixes LemmyNet#5196)…
Browse files Browse the repository at this point in the history
… (LemmyNet#5212)"

This reverts commit 63ea99d.
  • Loading branch information
flamingo-cant-draw committed Nov 25, 2024
1 parent ba3d574 commit 878e8b8
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 33 deletions.
19 changes: 1 addition & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/api_common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ actix-web = { workspace = true, optional = true }
enum-map = { workspace = true }
urlencoding = { workspace = true }
mime = { version = "0.3.17", optional = true }
mime_guess = "2.0.5"
webpage = { version = "2.0", default-features = false, features = [
"serde",
], optional = true }
Expand Down
20 changes: 6 additions & 14 deletions crates/api_common/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use lemmy_utils::{
REQWEST_TIMEOUT,
VERSION,
};
use mime::Mime;
use reqwest::{
header::{CONTENT_TYPE, RANGE},
Client,
Expand Down Expand Up @@ -63,20 +64,11 @@ pub async fn fetch_link_metadata(url: &Url, context: &LemmyContext) -> LemmyResu
.await?
.error_for_status()?;

// 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 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 content_type: Option<Mime> = 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 878e8b8

Please sign in to comment.