1 use crate::{settings::structs::Settings, version::VERSION, LemmyError};
3 use encoding::{all::encodings, DecoderTrap};
4 use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
5 use reqwest_middleware::ClientWithMiddleware;
6 use serde::{Deserialize, Serialize};
7 use std::future::Future;
9 use tracing::{error, info};
13 #[derive(Clone, Debug, Error)]
14 #[error("Error sending request, {0}")]
15 struct SendError(pub String);
17 #[derive(Clone, Debug, Error)]
18 #[error("Error receiving response, {0}")]
19 pub struct RecvError(pub String);
21 pub async fn retry<F, Fut, T>(f: F) -> Result<T, reqwest_middleware::Error>
24 Fut: Future<Output = Result<T, reqwest_middleware::Error>>,
26 retry_custom(|| async { Ok((f)().await) }).await
29 async fn retry_custom<F, Fut, T>(f: F) -> Result<T, reqwest_middleware::Error>
32 Fut: Future<Output = Result<Result<T, reqwest_middleware::Error>, reqwest_middleware::Error>>,
34 let mut response: Option<Result<T, reqwest_middleware::Error>> = None;
38 Ok(t) => return Ok(t),
39 Err(reqwest_middleware::Error::Reqwest(e)) => {
41 response = Some(Err(reqwest_middleware::Error::Reqwest(e)));
44 return Err(reqwest_middleware::Error::Reqwest(e));
47 return Err(otherwise);
52 response.expect("retry http request")
55 #[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]
56 pub struct SiteMetadata {
57 pub title: Option<String>,
58 pub description: Option<String>,
60 pub html: Option<String>,
63 /// Fetches the post link html tags (like title, description, image, etc)
64 pub async fn fetch_site_metadata(
65 client: &ClientWithMiddleware,
67 ) -> Result<SiteMetadata, LemmyError> {
68 info!("Fetching site metadata for url: {}", url);
69 let response = client.get(url.as_str()).send().await?;
71 // Can't use .text() here, because it only checks the content header, not the actual bytes
72 // https://github.com/LemmyNet/lemmy/issues/1964
73 let html_bytes = response
76 .map_err(|e| RecvError(e.to_string()))?
79 let tags = html_to_site_metadata(&html_bytes)?;
84 fn html_to_site_metadata(html_bytes: &[u8]) -> Result<SiteMetadata, LemmyError> {
85 let html = String::from_utf8_lossy(html_bytes);
87 // Make sure the first line is doctype html
93 .ok_or_else(|| LemmyError::from_message("No lines in html"))?
96 if !first_line.starts_with("<!doctype html>") {
97 return Err(LemmyError::from_message(
98 "Site metadata page fetch is not DOCTYPE html",
102 let mut page = HTML::from_string(html.to_string(), None)?;
104 // If the web page specifies that it isn't actually UTF-8, re-decode the received bytes with the
105 // proper encoding. If the specified encoding cannot be found, fall back to the original UTF-8
107 if let Some(charset) = page.meta.get("charset") {
108 if charset.to_lowercase() != "utf-8" {
109 if let Some(encoding_ref) = encodings().iter().find(|e| e.name() == charset) {
110 if let Ok(html_with_encoding) = encoding_ref.decode(html_bytes, DecoderTrap::Replace) {
111 page = HTML::from_string(html_with_encoding, None)?;
117 let page_title = page.title;
118 let page_description = page.description;
120 let og_description = page
124 .map(|t| t.to_string());
129 .map(|t| t.to_string());
134 .map(|ogo| Url::parse(&ogo.url).ok())
137 let title = og_title.or(page_title);
138 let description = og_description.or(page_description);
139 let image = og_image;
149 #[derive(Deserialize, Debug, Clone)]
150 pub(crate) struct PictrsResponse {
151 files: Vec<PictrsFile>,
155 #[derive(Deserialize, Debug, Clone)]
156 pub(crate) struct PictrsFile {
159 delete_token: String,
162 pub(crate) async fn fetch_pictrs(
163 client: &ClientWithMiddleware,
166 ) -> Result<PictrsResponse, LemmyError> {
167 if let Some(pictrs_url) = settings.pictrs_url.to_owned() {
168 is_image_content_type(client, image_url).await?;
170 let fetch_url = format!(
171 "{}/image/download?url={}",
173 utf8_percent_encode(image_url.as_str(), NON_ALPHANUMERIC) // TODO this might not be needed
176 let response = client.get(&fetch_url).send().await?;
178 let response: PictrsResponse = response
181 .map_err(|e| RecvError(e.to_string()))?;
183 if response.msg == "ok" {
186 Err(anyhow!("{}", &response.msg).into())
189 Err(anyhow!("pictrs_url not set up in config").into())
193 /// Both are options, since the URL might be either an html page, or an image
194 /// Returns the SiteMetadata, and a Pictrs URL, if there is a picture associated
195 pub async fn fetch_site_data(
196 client: &ClientWithMiddleware,
199 ) -> (Option<SiteMetadata>, Option<Url>) {
203 // Ignore errors, since it may be an image, or not have the data.
204 // Warning, this may ignore SSL errors
205 let metadata_option = fetch_site_metadata(client, url).await.ok();
207 // Fetch pictrs thumbnail
208 let pictrs_hash = match &metadata_option {
209 Some(metadata_res) => match &metadata_res.image {
210 // Metadata, with image
211 // Try to generate a small thumbnail if there's a full sized one from post-links
212 Some(metadata_image) => fetch_pictrs(client, settings, metadata_image)
214 .map(|r| r.files[0].file.to_owned()),
215 // Metadata, but no image
216 None => fetch_pictrs(client, settings, url)
218 .map(|r| r.files[0].file.to_owned()),
220 // No metadata, try to fetch the URL as an image
221 None => fetch_pictrs(client, settings, url)
223 .map(|r| r.files[0].file.to_owned()),
226 // The full urls are necessary for federation
227 let pictrs_thumbnail = pictrs_hash
230 "{}/pictrs/image/{}",
231 settings.get_protocol_and_hostname(),
239 (metadata_option, pictrs_thumbnail)
241 None => (None, None),
245 async fn is_image_content_type(client: &ClientWithMiddleware, url: &Url) -> Result<(), LemmyError> {
246 let response = client.get(url.as_str()).send().await?;
250 .ok_or_else(|| anyhow!("No Content-Type header"))?
252 .starts_with("image/")
256 Err(anyhow!("Not an image type.").into())
260 pub fn build_user_agent(settings: &Settings) -> String {
264 settings.get_protocol_and_hostname()
270 use crate::request::{build_user_agent, fetch_site_metadata};
273 use super::SiteMetadata;
274 use crate::settings::structs::Settings;
276 // These helped with testing
278 async fn test_site_metadata() {
279 let settings = Settings::init().unwrap();
280 let client = reqwest::Client::builder()
281 .user_agent(build_user_agent(&settings))
285 let sample_url = Url::parse("https://www.redspark.nu/en/peoples-war/district-leader-of-chand-led-cpn-arrested-in-bhojpur/").unwrap();
286 let sample_res = fetch_site_metadata(&client, &sample_url).await.unwrap();
289 title: Some("District Leader Of Chand Led CPN Arrested In Bhojpur - Redspark".to_string()),
290 description: Some("BHOJPUR: A district leader of the outlawed Netra Bikram Chand alias Biplav-led outfit has been arrested. According to District Police".to_string()),
291 image: Some(Url::parse("https://www.redspark.nu/wp-content/uploads/2020/03/netra-bikram-chand-attends-program-1272019033653-1000x0-845x653-1.jpg").unwrap()),
295 let youtube_url = Url::parse("https://www.youtube.com/watch?v=IquO_TcMZIQ").unwrap();
296 let youtube_res = fetch_site_metadata(&client, &youtube_url).await.unwrap();
299 title: Some("A Hard Look at Rent and Rent Seeking with Michael Hudson & Pepe Escobar".to_string()),
300 description: Some("An interactive discussion on wealth inequality and the “Great Game” on the control of natural resources.In this webinar organized jointly by the Henry George...".to_string()),
301 image: Some(Url::parse("https://i.ytimg.com/vi/IquO_TcMZIQ/maxresdefault.jpg").unwrap()),
307 // fn test_pictshare() {
308 // let res = fetch_pictshare("https://upload.wikimedia.org/wikipedia/en/2/27/The_Mandalorian_logo.jpg");
309 // assert!(res.is_ok());
310 // let res_other = fetch_pictshare("https://upload.wikimedia.org/wikipedia/en/2/27/The_Mandalorian_logo.jpgaoeu");
311 // assert!(res_other.is_err());