2024-01-25 14:22:11 +00:00
|
|
|
use crate::{
|
|
|
|
context::LemmyContext,
|
2024-03-27 14:54:42 +00:00
|
|
|
lemmy_db_schema::traits::Crud,
|
2024-01-25 14:22:11 +00:00
|
|
|
post::{LinkMetadata, OpenGraphData},
|
2024-03-27 14:54:42 +00:00
|
|
|
send_activity::{ActivityChannel, SendActivityData},
|
|
|
|
utils::{local_site_opt_to_sensitive, proxy_image_link, proxy_image_link_opt_apub},
|
2024-01-25 14:22:11 +00:00
|
|
|
};
|
2024-03-27 13:00:52 +00:00
|
|
|
use activitypub_federation::config::Data;
|
2024-05-02 23:12:08 +00:00
|
|
|
use encoding_rs::{Encoding, UTF_8};
|
2024-03-08 15:17:26 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
newtypes::DbUrl,
|
2024-03-27 14:54:42 +00:00
|
|
|
source::{
|
|
|
|
images::{LocalImage, LocalImageForm},
|
|
|
|
local_site::LocalSite,
|
|
|
|
post::{Post, PostUpdateForm},
|
|
|
|
},
|
2024-03-08 15:17:26 +00:00
|
|
|
};
|
2022-06-13 19:15:04 +00:00
|
|
|
use lemmy_utils::{
|
2024-04-10 14:14:11 +00:00
|
|
|
error::{LemmyError, LemmyErrorType, LemmyResult},
|
2024-01-25 14:22:11 +00:00
|
|
|
settings::structs::{PictrsImageMode, Settings},
|
2024-03-27 14:54:42 +00:00
|
|
|
spawn_try_task,
|
2022-06-13 19:15:04 +00:00
|
|
|
REQWEST_TIMEOUT,
|
2024-04-02 15:19:51 +00:00
|
|
|
VERSION,
|
2022-06-13 19:15:04 +00:00
|
|
|
};
|
2024-01-25 14:22:11 +00:00
|
|
|
use mime::Mime;
|
|
|
|
use reqwest::{header::CONTENT_TYPE, Client, ClientBuilder};
|
2022-05-03 17:44:13 +00:00
|
|
|
use reqwest_middleware::ClientWithMiddleware;
|
|
|
|
use serde::Deserialize;
|
|
|
|
use tracing::info;
|
|
|
|
use url::Url;
|
2024-01-25 14:22:11 +00:00
|
|
|
use urlencoding::encode;
|
2022-05-03 17:44:13 +00:00
|
|
|
use webpage::HTML;
|
|
|
|
|
2024-01-25 14:22:11 +00:00
|
|
|
pub fn client_builder(settings: &Settings) -> ClientBuilder {
|
2024-04-02 15:19:51 +00:00
|
|
|
let user_agent = format!("Lemmy/{VERSION}; +{}", settings.get_protocol_and_hostname());
|
2024-01-25 14:22:11 +00:00
|
|
|
|
|
|
|
Client::builder()
|
|
|
|
.user_agent(user_agent.clone())
|
|
|
|
.timeout(REQWEST_TIMEOUT)
|
|
|
|
.connect_timeout(REQWEST_TIMEOUT)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Fetches metadata for the given link and optionally generates thumbnail.
|
2022-05-03 17:44:13 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2024-04-17 14:36:45 +00:00
|
|
|
pub async fn fetch_link_metadata(url: &Url, context: &LemmyContext) -> LemmyResult<LinkMetadata> {
|
2022-05-03 17:44:13 +00:00
|
|
|
info!("Fetching site metadata for url: {}", url);
|
2024-01-25 14:22:11 +00:00
|
|
|
let response = context.client().get(url.as_str()).send().await?;
|
|
|
|
|
|
|
|
let content_type: Option<Mime> = response
|
|
|
|
.headers()
|
|
|
|
.get(CONTENT_TYPE)
|
|
|
|
.and_then(|h| h.to_str().ok())
|
|
|
|
.and_then(|h| h.parse().ok());
|
2022-05-03 17:44:13 +00:00
|
|
|
|
|
|
|
// Can't use .text() here, because it only checks the content header, not the actual bytes
|
|
|
|
// https://github.com/LemmyNet/lemmy/issues/1964
|
|
|
|
let html_bytes = response.bytes().await.map_err(LemmyError::from)?.to_vec();
|
|
|
|
|
2024-02-26 15:24:09 +00:00
|
|
|
let opengraph_data = extract_opengraph_data(&html_bytes, url)
|
|
|
|
.map_err(|e| info!("{e}"))
|
|
|
|
.unwrap_or_default();
|
2024-01-25 14:22:11 +00:00
|
|
|
Ok(LinkMetadata {
|
|
|
|
opengraph_data,
|
|
|
|
content_type: content_type.map(|c| c.to_string()),
|
|
|
|
})
|
|
|
|
}
|
2022-05-03 17:44:13 +00:00
|
|
|
|
2024-03-27 14:54:42 +00:00
|
|
|
/// Generate post thumbnail in background task, because some sites can be very slow to respond.
|
|
|
|
///
|
|
|
|
/// Takes a callback to generate a send activity task, so that post can be federated with metadata.
|
2024-04-17 14:36:45 +00:00
|
|
|
///
|
|
|
|
/// TODO: `federated_thumbnail` param can be removed once we federate full metadata and can
|
|
|
|
/// write it to db directly, without calling this function.
|
|
|
|
/// https://github.com/LemmyNet/lemmy/issues/4598
|
2024-03-27 14:54:42 +00:00
|
|
|
pub fn generate_post_link_metadata(
|
|
|
|
post: Post,
|
|
|
|
custom_thumbnail: Option<Url>,
|
2024-04-17 14:36:45 +00:00
|
|
|
federated_thumbnail: Option<Url>,
|
2024-03-27 14:54:42 +00:00
|
|
|
send_activity: impl FnOnce(Post) -> Option<SendActivityData> + Send + 'static,
|
|
|
|
local_site: Option<LocalSite>,
|
|
|
|
context: Data<LemmyContext>,
|
|
|
|
) {
|
|
|
|
spawn_try_task(async move {
|
2024-04-17 14:36:45 +00:00
|
|
|
let metadata = match &post.url {
|
|
|
|
Some(url) => fetch_link_metadata(url, &context).await.unwrap_or_default(),
|
|
|
|
_ => Default::default(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let is_image_post = metadata
|
2024-04-10 14:59:46 +00:00
|
|
|
.content_type
|
|
|
|
.as_ref()
|
2024-04-17 14:36:45 +00:00
|
|
|
.is_some_and(|content_type| content_type.starts_with("image"));
|
|
|
|
|
|
|
|
// Decide if we are allowed to generate local thumbnail
|
|
|
|
let allow_sensitive = local_site_opt_to_sensitive(&local_site);
|
|
|
|
let allow_generate_thumbnail = allow_sensitive || !post.nsfw;
|
2024-04-10 14:59:46 +00:00
|
|
|
|
2024-04-17 14:36:45 +00:00
|
|
|
// Use custom thumbnail if available and its not an image post
|
|
|
|
let thumbnail_url = if !is_image_post && custom_thumbnail.is_some() {
|
|
|
|
custom_thumbnail
|
|
|
|
}
|
|
|
|
// Use federated thumbnail if available
|
|
|
|
else if federated_thumbnail.is_some() {
|
|
|
|
federated_thumbnail
|
|
|
|
}
|
|
|
|
// Generate local thumbnail if allowed
|
|
|
|
else if allow_generate_thumbnail {
|
2024-04-24 02:52:56 +00:00
|
|
|
match post
|
|
|
|
.url
|
|
|
|
.filter(|_| is_image_post)
|
|
|
|
.or(metadata.opengraph_data.image)
|
|
|
|
{
|
2024-04-17 14:36:45 +00:00
|
|
|
Some(url) => generate_pictrs_thumbnail(&url, &context).await.ok(),
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Otherwise use opengraph preview image directly
|
|
|
|
else {
|
|
|
|
metadata.opengraph_data.image.map(Into::into)
|
|
|
|
};
|
2024-04-10 14:59:46 +00:00
|
|
|
|
|
|
|
// Proxy the image fetch if necessary
|
|
|
|
let proxied_thumbnail_url = proxy_image_link_opt_apub(thumbnail_url, &context).await?;
|
2024-03-27 14:54:42 +00:00
|
|
|
|
|
|
|
let form = PostUpdateForm {
|
|
|
|
embed_title: Some(metadata.opengraph_data.title),
|
|
|
|
embed_description: Some(metadata.opengraph_data.description),
|
|
|
|
embed_video_url: Some(metadata.opengraph_data.embed_video_url),
|
2024-04-10 14:59:46 +00:00
|
|
|
thumbnail_url: Some(proxied_thumbnail_url),
|
2024-03-27 14:54:42 +00:00
|
|
|
url_content_type: Some(metadata.content_type),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let updated_post = Post::update(&mut context.pool(), post.id, &form).await?;
|
|
|
|
if let Some(send_activity) = send_activity(updated_post) {
|
|
|
|
ActivityChannel::submit_activity(send_activity, &context).await?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
2022-05-03 17:44:13 +00:00
|
|
|
|
2024-01-25 14:22:11 +00:00
|
|
|
/// Extract site metadata from HTML Opengraph attributes.
|
2024-04-10 14:14:11 +00:00
|
|
|
fn extract_opengraph_data(html_bytes: &[u8], url: &Url) -> LemmyResult<OpenGraphData> {
|
2022-05-03 17:44:13 +00:00
|
|
|
let html = String::from_utf8_lossy(html_bytes);
|
|
|
|
|
|
|
|
// Make sure the first line is doctype html
|
|
|
|
let first_line = html
|
|
|
|
.trim_start()
|
|
|
|
.lines()
|
|
|
|
.next()
|
2023-07-10 14:50:07 +00:00
|
|
|
.ok_or(LemmyErrorType::NoLinesInHtml)?
|
2022-05-03 17:44:13 +00:00
|
|
|
.to_lowercase();
|
|
|
|
|
2024-02-26 15:24:09 +00:00
|
|
|
if !first_line.starts_with("<!doctype html") {
|
2023-08-31 13:01:08 +00:00
|
|
|
Err(LemmyErrorType::SiteMetadataPageIsNotDoctypeHtml)?
|
2022-05-03 17:44:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut page = HTML::from_string(html.to_string(), None)?;
|
|
|
|
|
|
|
|
// If the web page specifies that it isn't actually UTF-8, re-decode the received bytes with the
|
|
|
|
// proper encoding. If the specified encoding cannot be found, fall back to the original UTF-8
|
|
|
|
// version.
|
|
|
|
if let Some(charset) = page.meta.get("charset") {
|
2024-05-02 23:12:08 +00:00
|
|
|
if charset != UTF_8.name() {
|
|
|
|
if let Some(enc) = Encoding::for_label(charset.as_bytes()) {
|
|
|
|
page = HTML::from_string(enc.decode(html_bytes).0.into_owned(), None)?;
|
2022-05-03 17:44:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let page_title = page.title;
|
|
|
|
let page_description = page.description;
|
|
|
|
|
|
|
|
let og_description = page
|
|
|
|
.opengraph
|
|
|
|
.properties
|
|
|
|
.get("description")
|
2022-11-19 04:33:54 +00:00
|
|
|
.map(std::string::ToString::to_string);
|
2022-05-03 17:44:13 +00:00
|
|
|
let og_title = page
|
|
|
|
.opengraph
|
|
|
|
.properties
|
|
|
|
.get("title")
|
2022-11-19 04:33:54 +00:00
|
|
|
.map(std::string::ToString::to_string);
|
2022-05-03 17:44:13 +00:00
|
|
|
let og_image = page
|
|
|
|
.opengraph
|
|
|
|
.images
|
2023-02-28 11:34:50 +00:00
|
|
|
.first()
|
2023-06-26 13:07:57 +00:00
|
|
|
// join also works if the target URL is absolute
|
|
|
|
.and_then(|ogo| url.join(&ogo.url).ok());
|
2022-06-02 21:44:47 +00:00
|
|
|
let og_embed_url = page
|
|
|
|
.opengraph
|
|
|
|
.videos
|
|
|
|
.first()
|
2023-06-26 13:07:57 +00:00
|
|
|
// join also works if the target URL is absolute
|
|
|
|
.and_then(|v| url.join(&v.url).ok());
|
2022-05-03 17:44:13 +00:00
|
|
|
|
2024-01-25 14:22:11 +00:00
|
|
|
Ok(OpenGraphData {
|
2022-06-02 21:44:47 +00:00
|
|
|
title: og_title.or(page_title),
|
|
|
|
description: og_description.or(page_description),
|
|
|
|
image: og_image.map(Into::into),
|
|
|
|
embed_video_url: og_embed_url.map(Into::into),
|
2022-05-03 17:44:13 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-25 14:22:11 +00:00
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
struct PictrsResponse {
|
2022-05-03 17:44:13 +00:00
|
|
|
files: Vec<PictrsFile>,
|
|
|
|
msg: String,
|
|
|
|
}
|
|
|
|
|
2024-01-25 14:22:11 +00:00
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
struct PictrsFile {
|
2022-05-03 17:44:13 +00:00
|
|
|
file: String,
|
|
|
|
delete_token: String,
|
|
|
|
}
|
|
|
|
|
2024-01-25 14:22:11 +00:00
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
struct PictrsPurgeResponse {
|
2022-06-13 19:15:04 +00:00
|
|
|
msg: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Purges an image from pictrs
|
|
|
|
/// Note: This should often be coerced from a Result to .ok() in order to fail softly, because:
|
|
|
|
/// - It might fail due to image being not local
|
|
|
|
/// - It might not be an image
|
|
|
|
/// - Pictrs might not be set up
|
2024-04-10 14:14:11 +00:00
|
|
|
pub async fn purge_image_from_pictrs(image_url: &Url, context: &LemmyContext) -> LemmyResult<()> {
|
2023-08-28 10:23:45 +00:00
|
|
|
is_image_content_type(context.client(), image_url).await?;
|
2022-06-13 19:15:04 +00:00
|
|
|
|
|
|
|
let alias = image_url
|
|
|
|
.path_segments()
|
2023-07-10 14:50:07 +00:00
|
|
|
.ok_or(LemmyErrorType::ImageUrlMissingPathSegments)?
|
2022-06-13 19:15:04 +00:00
|
|
|
.next_back()
|
2023-07-10 14:50:07 +00:00
|
|
|
.ok_or(LemmyErrorType::ImageUrlMissingLastPathSegment)?;
|
2022-06-13 19:15:04 +00:00
|
|
|
|
2023-09-06 13:13:30 +00:00
|
|
|
let pictrs_config = context.settings().pictrs_config()?;
|
|
|
|
let purge_url = format!("{}internal/purge?alias={}", pictrs_config.url, alias);
|
2022-06-13 19:15:04 +00:00
|
|
|
|
2022-07-11 20:38:37 +00:00
|
|
|
let pictrs_api_key = pictrs_config
|
|
|
|
.api_key
|
2023-07-10 14:50:07 +00:00
|
|
|
.ok_or(LemmyErrorType::PictrsApiKeyNotProvided)?;
|
2023-08-28 10:23:45 +00:00
|
|
|
let response = context
|
|
|
|
.client()
|
2022-06-13 19:15:04 +00:00
|
|
|
.post(&purge_url)
|
|
|
|
.timeout(REQWEST_TIMEOUT)
|
2022-07-11 20:38:37 +00:00
|
|
|
.header("x-api-token", pictrs_api_key)
|
2022-06-13 19:15:04 +00:00
|
|
|
.send()
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let response: PictrsPurgeResponse = response.json().await.map_err(LemmyError::from)?;
|
|
|
|
|
2024-01-25 14:22:11 +00:00
|
|
|
match response.msg.as_str() {
|
|
|
|
"ok" => Ok(()),
|
|
|
|
_ => Err(LemmyErrorType::PictrsPurgeResponseError(response.msg))?,
|
2022-05-03 17:44:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 13:13:30 +00:00
|
|
|
pub async fn delete_image_from_pictrs(
|
|
|
|
alias: &str,
|
|
|
|
delete_token: &str,
|
|
|
|
context: &LemmyContext,
|
2024-04-10 14:14:11 +00:00
|
|
|
) -> LemmyResult<()> {
|
2023-09-06 13:13:30 +00:00
|
|
|
let pictrs_config = context.settings().pictrs_config()?;
|
|
|
|
let url = format!(
|
|
|
|
"{}image/delete/{}/{}",
|
|
|
|
pictrs_config.url, &delete_token, &alias
|
|
|
|
);
|
|
|
|
context
|
|
|
|
.client()
|
|
|
|
.delete(&url)
|
|
|
|
.timeout(REQWEST_TIMEOUT)
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.map_err(LemmyError::from)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-01-25 14:22:11 +00:00
|
|
|
/// Retrieves the image with local pict-rs and generates a thumbnail. Returns the thumbnail url.
|
2022-05-03 17:44:13 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2024-04-10 14:14:11 +00:00
|
|
|
async fn generate_pictrs_thumbnail(image_url: &Url, context: &LemmyContext) -> LemmyResult<Url> {
|
2024-01-25 14:22:11 +00:00
|
|
|
let pictrs_config = context.settings().pictrs_config()?;
|
|
|
|
|
2024-04-10 14:09:54 +00:00
|
|
|
match pictrs_config.image_mode() {
|
|
|
|
PictrsImageMode::None => return Ok(image_url.clone()),
|
|
|
|
PictrsImageMode::ProxyAllImages => {
|
|
|
|
return Ok(proxy_image_link(image_url.clone(), context).await?.into())
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
};
|
2022-05-03 17:44:13 +00:00
|
|
|
|
2024-01-25 14:22:11 +00:00
|
|
|
// fetch remote non-pictrs images for persistent thumbnail link
|
|
|
|
// TODO: should limit size once supported by pictrs
|
|
|
|
let fetch_url = format!(
|
|
|
|
"{}image/download?url={}",
|
|
|
|
pictrs_config.url,
|
|
|
|
encode(image_url.as_str())
|
|
|
|
);
|
|
|
|
|
|
|
|
let response = context
|
|
|
|
.client()
|
|
|
|
.get(&fetch_url)
|
|
|
|
.timeout(REQWEST_TIMEOUT)
|
|
|
|
.send()
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let response: PictrsResponse = response.json().await?;
|
|
|
|
|
|
|
|
if response.msg == "ok" {
|
|
|
|
let thumbnail_url = Url::parse(&format!(
|
|
|
|
"{}/pictrs/image/{}",
|
|
|
|
context.settings().get_protocol_and_hostname(),
|
|
|
|
response.files.first().expect("missing pictrs file").file
|
|
|
|
))?;
|
2024-03-08 15:17:26 +00:00
|
|
|
for uploaded_image in response.files {
|
|
|
|
let form = LocalImageForm {
|
|
|
|
local_user_id: None,
|
|
|
|
pictrs_alias: uploaded_image.file.to_string(),
|
|
|
|
pictrs_delete_token: uploaded_image.delete_token.to_string(),
|
|
|
|
};
|
|
|
|
LocalImage::create(&mut context.pool(), &form).await?;
|
|
|
|
}
|
2024-01-25 14:22:11 +00:00
|
|
|
Ok(thumbnail_url)
|
|
|
|
} else {
|
|
|
|
Err(LemmyErrorType::PictrsResponseError(response.msg))?
|
|
|
|
}
|
2023-08-31 14:36:39 +00:00
|
|
|
}
|
|
|
|
|
2024-01-25 14:22:11 +00:00
|
|
|
// TODO: get rid of this by reading content type from db
|
2022-05-03 17:44:13 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2024-04-10 14:14:11 +00:00
|
|
|
async fn is_image_content_type(client: &ClientWithMiddleware, url: &Url) -> LemmyResult<()> {
|
2022-06-02 14:33:41 +00:00
|
|
|
let response = client.get(url.as_str()).send().await?;
|
2022-05-03 17:44:13 +00:00
|
|
|
if response
|
|
|
|
.headers()
|
|
|
|
.get("Content-Type")
|
2023-07-10 14:50:07 +00:00
|
|
|
.ok_or(LemmyErrorType::NoContentTypeHeader)?
|
2022-05-03 17:44:13 +00:00
|
|
|
.to_str()?
|
|
|
|
.starts_with("image/")
|
|
|
|
{
|
|
|
|
Ok(())
|
|
|
|
} else {
|
2023-07-10 14:50:07 +00:00
|
|
|
Err(LemmyErrorType::NotAnImageType)?
|
2022-05-03 17:44:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-27 13:00:52 +00:00
|
|
|
/// When adding a new avatar or similar image, delete the old one.
|
|
|
|
pub async fn replace_image(
|
|
|
|
new_image: &Option<String>,
|
|
|
|
old_image: &Option<DbUrl>,
|
|
|
|
context: &Data<LemmyContext>,
|
2024-04-10 14:14:11 +00:00
|
|
|
) -> LemmyResult<()> {
|
2024-03-27 13:00:52 +00:00
|
|
|
if new_image.is_some() {
|
|
|
|
// Ignore errors because image may be stored externally.
|
|
|
|
if let Some(avatar) = &old_image {
|
|
|
|
let image = LocalImage::delete_by_url(&mut context.pool(), avatar)
|
|
|
|
.await
|
|
|
|
.ok();
|
|
|
|
if let Some(image) = image {
|
|
|
|
delete_image_from_pictrs(&image.pictrs_alias, &image.pictrs_delete_token, context).await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-05-03 17:44:13 +00:00
|
|
|
#[cfg(test)]
|
2024-03-26 09:17:42 +00:00
|
|
|
#[allow(clippy::unwrap_used)]
|
|
|
|
#[allow(clippy::indexing_slicing)]
|
2022-05-03 17:44:13 +00:00
|
|
|
mod tests {
|
2023-07-17 15:04:14 +00:00
|
|
|
|
2024-01-25 14:22:11 +00:00
|
|
|
use crate::{
|
|
|
|
context::LemmyContext,
|
|
|
|
request::{extract_opengraph_data, fetch_link_metadata},
|
|
|
|
};
|
2024-01-04 09:47:18 +00:00
|
|
|
use pretty_assertions::assert_eq;
|
2024-01-25 14:22:11 +00:00
|
|
|
use serial_test::serial;
|
2022-05-03 17:44:13 +00:00
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
// These helped with testing
|
2023-06-26 08:24:11 +00:00
|
|
|
#[tokio::test]
|
2024-01-25 14:22:11 +00:00
|
|
|
#[serial]
|
|
|
|
async fn test_link_metadata() {
|
2024-03-06 16:21:46 +00:00
|
|
|
let context = LemmyContext::init_test_context().await;
|
2022-05-03 17:44:13 +00:00
|
|
|
let sample_url = Url::parse("https://gitlab.com/IzzyOnDroid/repo/-/wikis/FAQ").unwrap();
|
2024-04-17 14:36:45 +00:00
|
|
|
let sample_res = fetch_link_metadata(&sample_url, &context).await.unwrap();
|
2024-01-25 14:22:11 +00:00
|
|
|
assert_eq!(
|
|
|
|
Some("FAQ · Wiki · IzzyOnDroid / repo · GitLab".to_string()),
|
|
|
|
sample_res.opengraph_data.title
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Some("The F-Droid compatible repo at https://apt.izzysoft.de/fdroid/".to_string()),
|
|
|
|
sample_res.opengraph_data.description
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Some(
|
|
|
|
Url::parse("https://gitlab.com/uploads/-/system/project/avatar/4877469/iod_logo.png")
|
|
|
|
.unwrap()
|
|
|
|
.into()
|
|
|
|
),
|
|
|
|
sample_res.opengraph_data.image
|
|
|
|
);
|
|
|
|
assert_eq!(None, sample_res.opengraph_data.embed_video_url);
|
2022-05-03 17:44:13 +00:00
|
|
|
assert_eq!(
|
2024-01-25 14:22:11 +00:00
|
|
|
Some(mime::TEXT_HTML_UTF_8.to_string()),
|
|
|
|
sample_res.content_type
|
2022-05-03 17:44:13 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-26 13:07:57 +00:00
|
|
|
#[test]
|
|
|
|
fn test_resolve_image_url() {
|
|
|
|
// url that lists the opengraph fields
|
|
|
|
let url = Url::parse("https://example.com/one/two.html").unwrap();
|
|
|
|
|
|
|
|
// root relative url
|
|
|
|
let html_bytes = b"<!DOCTYPE html><html><head><meta property='og:image' content='/image.jpg'></head><body></body></html>";
|
2024-01-25 14:22:11 +00:00
|
|
|
let metadata = extract_opengraph_data(html_bytes, &url).expect("Unable to parse metadata");
|
2023-06-26 13:07:57 +00:00
|
|
|
assert_eq!(
|
|
|
|
metadata.image,
|
|
|
|
Some(Url::parse("https://example.com/image.jpg").unwrap().into())
|
|
|
|
);
|
|
|
|
|
|
|
|
// base relative url
|
|
|
|
let html_bytes = b"<!DOCTYPE html><html><head><meta property='og:image' content='image.jpg'></head><body></body></html>";
|
2024-01-25 14:22:11 +00:00
|
|
|
let metadata = extract_opengraph_data(html_bytes, &url).expect("Unable to parse metadata");
|
2023-06-26 13:07:57 +00:00
|
|
|
assert_eq!(
|
|
|
|
metadata.image,
|
|
|
|
Some(
|
|
|
|
Url::parse("https://example.com/one/image.jpg")
|
|
|
|
.unwrap()
|
|
|
|
.into()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// absolute url
|
|
|
|
let html_bytes = b"<!DOCTYPE html><html><head><meta property='og:image' content='https://cdn.host.com/image.jpg'></head><body></body></html>";
|
2024-01-25 14:22:11 +00:00
|
|
|
let metadata = extract_opengraph_data(html_bytes, &url).expect("Unable to parse metadata");
|
2023-06-26 13:07:57 +00:00
|
|
|
assert_eq!(
|
|
|
|
metadata.image,
|
|
|
|
Some(Url::parse("https://cdn.host.com/image.jpg").unwrap().into())
|
|
|
|
);
|
|
|
|
|
|
|
|
// protocol relative url
|
|
|
|
let html_bytes = b"<!DOCTYPE html><html><head><meta property='og:image' content='//example.com/image.jpg'></head><body></body></html>";
|
2024-01-25 14:22:11 +00:00
|
|
|
let metadata = extract_opengraph_data(html_bytes, &url).expect("Unable to parse metadata");
|
2023-06-26 13:07:57 +00:00
|
|
|
assert_eq!(
|
|
|
|
metadata.image,
|
|
|
|
Some(Url::parse("https://example.com/image.jpg").unwrap().into())
|
|
|
|
);
|
|
|
|
}
|
2022-05-03 17:44:13 +00:00
|
|
|
}
|