Move some things about.

This commit is contained in:
flamingos-cant 2024-12-04 01:38:04 +00:00
parent 6d39a97cb0
commit b838ca130a

View file

@ -68,10 +68,12 @@ pub async fn fetch_link_metadata(url: &Url, context: &LemmyContext) -> LemmyResu
.headers() .headers()
.get(CONTENT_TYPE) .get(CONTENT_TYPE)
.and_then(|h| h.to_str().ok()) .and_then(|h| h.to_str().ok())
.and_then(|h| h.parse().ok()); .and_then(|h| h.parse().ok())
// If we don't get a content_type from the response (e.g. if the server is down),
let mut opengraph_data = Default::default(); // then try to infer the content_type from the file extension.
.or(mime_guess::from_path(url.path()).first());
let opengraph_data = 'ograph: {
if let Some(c) = &content_type { if let Some(c) = &content_type {
// application/xhtml+xml is a subset of HTML // application/xhtml+xml is a subset of HTML
let application_xhtml: Mime = "application/xhtml+xml".parse()?; let application_xhtml: Mime = "application/xhtml+xml".parse()?;
@ -80,12 +82,12 @@ pub async fn fetch_link_metadata(url: &Url, context: &LemmyContext) -> LemmyResu
{ {
// Can't use .text() here, because it only checks the content header, not the actual bytes // Can't use .text() here, because it only checks the content header, not the actual bytes
// https://github.com/LemmyNet/lemmy/issues/1964 // https://github.com/LemmyNet/lemmy/issues/1964
// So we want to do deep inspection of the actually returned bytes but need to be careful not // So we want to do deep inspection of the actually returned bytes but need to be careful
// spend too much time parsing binary data as HTML // not spend too much time parsing binary data as HTML
// only take first bytes regardless of how many bytes the server returns // only take first bytes regardless of how many bytes the server returns
let html_bytes = collect_bytes_until_limit(response, bytes_to_fetch).await?; let html_bytes = collect_bytes_until_limit(response, bytes_to_fetch).await?;
opengraph_data = extract_opengraph_data(&html_bytes, url) break 'ograph extract_opengraph_data(&html_bytes, url)
.map_err(|e| info!("{e}")) .map_err(|e| info!("{e}"))
.unwrap_or_default(); .unwrap_or_default();
} }
@ -94,14 +96,12 @@ pub async fn fetch_link_metadata(url: &Url, context: &LemmyContext) -> LemmyResu
else if c.subtype() == "octet-stream" { else if c.subtype() == "octet-stream" {
// Don't need to fetch as much data for this as we do with opengraph // Don't need to fetch as much data for this as we do with opengraph
let octet_bytes = collect_bytes_until_limit(response, 512).await?; let octet_bytes = collect_bytes_until_limit(response, 512).await?;
content_type = infer::get(&octet_bytes).map_or(content_type, |t| t.mime_type().parse().ok()); content_type =
infer::get(&octet_bytes).map_or(content_type, |t| t.mime_type().parse().ok());
} }
} }
// If we don't get a content_type from the response (e.g. if the server is down), Default::default()
// then try to infer the content_type from the file extension. };
else {
content_type = mime_guess::from_path(url.path()).first();
}
Ok(LinkMetadata { Ok(LinkMetadata {
opengraph_data, opengraph_data,