mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-12-23 03:11:32 +00:00
Move some things about.
This commit is contained in:
parent
6d39a97cb0
commit
b838ca130a
1 changed files with 31 additions and 31 deletions
|
@ -68,10 +68,12 @@ pub async fn fetch_link_metadata(url: &Url, context: &LemmyContext) -> LemmyResu
|
|||
.headers()
|
||||
.get(CONTENT_TYPE)
|
||||
.and_then(|h| h.to_str().ok())
|
||||
.and_then(|h| h.parse().ok());
|
||||
|
||||
let mut opengraph_data = Default::default();
|
||||
.and_then(|h| h.parse().ok())
|
||||
// If we don't get a content_type from the response (e.g. if the server is down),
|
||||
// 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 {
|
||||
// application/xhtml+xml is a subset of HTML
|
||||
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
|
||||
// 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
|
||||
// spend too much time parsing binary data as HTML
|
||||
// So we want to do deep inspection of the actually returned bytes but need to be careful
|
||||
// not spend too much time parsing binary data as HTML
|
||||
|
||||
// only take first bytes regardless of how many bytes the server returns
|
||||
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}"))
|
||||
.unwrap_or_default();
|
||||
}
|
||||
|
@ -94,14 +96,12 @@ pub async fn fetch_link_metadata(url: &Url, context: &LemmyContext) -> LemmyResu
|
|||
else if c.subtype() == "octet-stream" {
|
||||
// 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?;
|
||||
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),
|
||||
// then try to infer the content_type from the file extension.
|
||||
else {
|
||||
content_type = mime_guess::from_path(url.path()).first();
|
||||
}
|
||||
Default::default()
|
||||
};
|
||||
|
||||
Ok(LinkMetadata {
|
||||
opengraph_data,
|
||||
|
|
Loading…
Reference in a new issue