]> Untitled Git - lemmy.git/blob - crates/api_common/src/request.rs
dc09ecaa7f3f69a88c852498064b71eb34ee1d53
[lemmy.git] / crates / api_common / src / request.rs
1 use crate::post::SiteMetadata;
2 use encoding::{all::encodings, DecoderTrap};
3 use lemmy_db_schema::newtypes::DbUrl;
4 use lemmy_utils::{
5   error::{LemmyError, LemmyErrorType},
6   settings::structs::Settings,
7   version::VERSION,
8   REQWEST_TIMEOUT,
9 };
10 use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
11 use reqwest_middleware::ClientWithMiddleware;
12 use serde::Deserialize;
13 use tracing::info;
14 use url::Url;
15 use webpage::HTML;
16
17 /// Fetches the post link html tags (like title, description, image, etc)
18 #[tracing::instrument(skip_all)]
19 pub async fn fetch_site_metadata(
20   client: &ClientWithMiddleware,
21   url: &Url,
22 ) -> Result<SiteMetadata, LemmyError> {
23   info!("Fetching site metadata for url: {}", url);
24   let response = client.get(url.as_str()).send().await?;
25
26   // Can't use .text() here, because it only checks the content header, not the actual bytes
27   // https://github.com/LemmyNet/lemmy/issues/1964
28   let html_bytes = response.bytes().await.map_err(LemmyError::from)?.to_vec();
29
30   let tags = html_to_site_metadata(&html_bytes, url)?;
31
32   Ok(tags)
33 }
34
35 fn html_to_site_metadata(html_bytes: &[u8], url: &Url) -> Result<SiteMetadata, LemmyError> {
36   let html = String::from_utf8_lossy(html_bytes);
37
38   // Make sure the first line is doctype html
39   let first_line = html
40     .trim_start()
41     .lines()
42     .next()
43     .ok_or(LemmyErrorType::NoLinesInHtml)?
44     .to_lowercase();
45
46   if !first_line.starts_with("<!doctype html>") {
47     return Err(LemmyErrorType::SiteMetadataPageIsNotDoctypeHtml)?;
48   }
49
50   let mut page = HTML::from_string(html.to_string(), None)?;
51
52   // If the web page specifies that it isn't actually UTF-8, re-decode the received bytes with the
53   // proper encoding. If the specified encoding cannot be found, fall back to the original UTF-8
54   // version.
55   if let Some(charset) = page.meta.get("charset") {
56     if charset.to_lowercase() != "utf-8" {
57       if let Some(encoding_ref) = encodings().iter().find(|e| e.name() == charset) {
58         if let Ok(html_with_encoding) = encoding_ref.decode(html_bytes, DecoderTrap::Replace) {
59           page = HTML::from_string(html_with_encoding, None)?;
60         }
61       }
62     }
63   }
64
65   let page_title = page.title;
66   let page_description = page.description;
67
68   let og_description = page
69     .opengraph
70     .properties
71     .get("description")
72     .map(std::string::ToString::to_string);
73   let og_title = page
74     .opengraph
75     .properties
76     .get("title")
77     .map(std::string::ToString::to_string);
78   let og_image = page
79     .opengraph
80     .images
81     .first()
82     // join also works if the target URL is absolute
83     .and_then(|ogo| url.join(&ogo.url).ok());
84   let og_embed_url = page
85     .opengraph
86     .videos
87     .first()
88     // join also works if the target URL is absolute
89     .and_then(|v| url.join(&v.url).ok());
90
91   Ok(SiteMetadata {
92     title: og_title.or(page_title),
93     description: og_description.or(page_description),
94     image: og_image.map(Into::into),
95     embed_video_url: og_embed_url.map(Into::into),
96   })
97 }
98
99 #[derive(Deserialize, Debug, Clone)]
100 pub(crate) struct PictrsResponse {
101   files: Vec<PictrsFile>,
102   msg: String,
103 }
104
105 #[derive(Deserialize, Debug, Clone)]
106 pub(crate) struct PictrsFile {
107   file: String,
108   #[allow(dead_code)]
109   delete_token: String,
110 }
111
112 #[derive(Deserialize, Debug, Clone)]
113 pub(crate) struct PictrsPurgeResponse {
114   msg: String,
115 }
116
117 #[tracing::instrument(skip_all)]
118 pub(crate) async fn fetch_pictrs(
119   client: &ClientWithMiddleware,
120   settings: &Settings,
121   image_url: &Url,
122 ) -> Result<PictrsResponse, LemmyError> {
123   let pictrs_config = settings.pictrs_config()?;
124   is_image_content_type(client, image_url).await?;
125
126   let fetch_url = format!(
127     "{}image/download?url={}",
128     pictrs_config.url,
129     utf8_percent_encode(image_url.as_str(), NON_ALPHANUMERIC) // TODO this might not be needed
130   );
131
132   let response = client
133     .get(&fetch_url)
134     .timeout(REQWEST_TIMEOUT)
135     .send()
136     .await?;
137
138   let response: PictrsResponse = response.json().await.map_err(LemmyError::from)?;
139
140   if response.msg == "ok" {
141     Ok(response)
142   } else {
143     Err(LemmyErrorType::PictrsResponseError(response.msg))?
144   }
145 }
146
147 /// Purges an image from pictrs
148 /// Note: This should often be coerced from a Result to .ok() in order to fail softly, because:
149 /// - It might fail due to image being not local
150 /// - It might not be an image
151 /// - Pictrs might not be set up
152 pub async fn purge_image_from_pictrs(
153   client: &ClientWithMiddleware,
154   settings: &Settings,
155   image_url: &Url,
156 ) -> Result<(), LemmyError> {
157   let pictrs_config = settings.pictrs_config()?;
158   is_image_content_type(client, image_url).await?;
159
160   let alias = image_url
161     .path_segments()
162     .ok_or(LemmyErrorType::ImageUrlMissingPathSegments)?
163     .next_back()
164     .ok_or(LemmyErrorType::ImageUrlMissingLastPathSegment)?;
165
166   let purge_url = format!("{}/internal/purge?alias={}", pictrs_config.url, alias);
167
168   let pictrs_api_key = pictrs_config
169     .api_key
170     .ok_or(LemmyErrorType::PictrsApiKeyNotProvided)?;
171   let response = client
172     .post(&purge_url)
173     .timeout(REQWEST_TIMEOUT)
174     .header("x-api-token", pictrs_api_key)
175     .send()
176     .await?;
177
178   let response: PictrsPurgeResponse = response.json().await.map_err(LemmyError::from)?;
179
180   if response.msg == "ok" {
181     Ok(())
182   } else {
183     Err(LemmyErrorType::PictrsPurgeResponseError(response.msg))?
184   }
185 }
186
187 /// Both are options, since the URL might be either an html page, or an image
188 /// Returns the SiteMetadata, and a Pictrs URL, if there is a picture associated
189 #[tracing::instrument(skip_all)]
190 pub async fn fetch_site_data(
191   client: &ClientWithMiddleware,
192   settings: &Settings,
193   url: Option<&Url>,
194   include_image: bool,
195 ) -> (Option<SiteMetadata>, Option<DbUrl>) {
196   match &url {
197     Some(url) => {
198       // Fetch metadata
199       // Ignore errors, since it may be an image, or not have the data.
200       // Warning, this may ignore SSL errors
201       let metadata_option = fetch_site_metadata(client, url).await.ok();
202       if !include_image {
203         return (metadata_option, None);
204       }
205
206       let missing_pictrs_file =
207         |r: PictrsResponse| r.files.first().expect("missing pictrs file").file.clone();
208
209       // Fetch pictrs thumbnail
210       let pictrs_hash = match &metadata_option {
211         Some(metadata_res) => match &metadata_res.image {
212           // Metadata, with image
213           // Try to generate a small thumbnail if there's a full sized one from post-links
214           Some(metadata_image) => fetch_pictrs(client, settings, metadata_image)
215             .await
216             .map(missing_pictrs_file),
217           // Metadata, but no image
218           None => fetch_pictrs(client, settings, url)
219             .await
220             .map(missing_pictrs_file),
221         },
222         // No metadata, try to fetch the URL as an image
223         None => fetch_pictrs(client, settings, url)
224           .await
225           .map(missing_pictrs_file),
226       };
227
228       // The full urls are necessary for federation
229       let pictrs_thumbnail = pictrs_hash
230         .map(|p| {
231           Url::parse(&format!(
232             "{}/pictrs/image/{}",
233             settings.get_protocol_and_hostname(),
234             p
235           ))
236           .ok()
237         })
238         .ok()
239         .flatten();
240
241       (metadata_option, pictrs_thumbnail.map(Into::into))
242     }
243     None => (None, None),
244   }
245 }
246
247 #[tracing::instrument(skip_all)]
248 async fn is_image_content_type(client: &ClientWithMiddleware, url: &Url) -> Result<(), LemmyError> {
249   let response = client.get(url.as_str()).send().await?;
250   if response
251     .headers()
252     .get("Content-Type")
253     .ok_or(LemmyErrorType::NoContentTypeHeader)?
254     .to_str()?
255     .starts_with("image/")
256   {
257     Ok(())
258   } else {
259     Err(LemmyErrorType::NotAnImageType)?
260   }
261 }
262
263 pub fn build_user_agent(settings: &Settings) -> String {
264   format!(
265     "Lemmy/{}; +{}",
266     VERSION,
267     settings.get_protocol_and_hostname()
268   )
269 }
270
271 #[cfg(test)]
272 mod tests {
273   use crate::request::{
274     build_user_agent,
275     fetch_site_metadata,
276     html_to_site_metadata,
277     SiteMetadata,
278   };
279   use lemmy_utils::settings::SETTINGS;
280   use url::Url;
281
282   // These helped with testing
283   #[tokio::test]
284   async fn test_site_metadata() {
285     let settings = &SETTINGS.clone();
286     let client = reqwest::Client::builder()
287       .user_agent(build_user_agent(settings))
288       .build()
289       .unwrap()
290       .into();
291     let sample_url = Url::parse("https://gitlab.com/IzzyOnDroid/repo/-/wikis/FAQ").unwrap();
292     let sample_res = fetch_site_metadata(&client, &sample_url).await.unwrap();
293     assert_eq!(
294       SiteMetadata {
295         title: Some("FAQ · Wiki · IzzyOnDroid / repo · GitLab".to_string()),
296         description: Some(
297           "The F-Droid compatible repo at https://apt.izzysoft.de/fdroid/".to_string()
298         ),
299         image: Some(
300           Url::parse("https://gitlab.com/uploads/-/system/project/avatar/4877469/iod_logo.png")
301             .unwrap()
302             .into()
303         ),
304         embed_video_url: None,
305       },
306       sample_res
307     );
308   }
309
310   // #[test]
311   // fn test_pictshare() {
312   //   let res = fetch_pictshare("https://upload.wikimedia.org/wikipedia/en/2/27/The_Mandalorian_logo.jpg");
313   //   assert!(res.is_ok());
314   //   let res_other = fetch_pictshare("https://upload.wikimedia.org/wikipedia/en/2/27/The_Mandalorian_logo.jpgaoeu");
315   //   assert!(res_other.is_err());
316   // }
317
318   #[test]
319   fn test_resolve_image_url() {
320     // url that lists the opengraph fields
321     let url = Url::parse("https://example.com/one/two.html").unwrap();
322
323     // root relative url
324     let html_bytes = b"<!DOCTYPE html><html><head><meta property='og:image' content='/image.jpg'></head><body></body></html>";
325     let metadata = html_to_site_metadata(html_bytes, &url).expect("Unable to parse metadata");
326     assert_eq!(
327       metadata.image,
328       Some(Url::parse("https://example.com/image.jpg").unwrap().into())
329     );
330
331     // base relative url
332     let html_bytes = b"<!DOCTYPE html><html><head><meta property='og:image' content='image.jpg'></head><body></body></html>";
333     let metadata = html_to_site_metadata(html_bytes, &url).expect("Unable to parse metadata");
334     assert_eq!(
335       metadata.image,
336       Some(
337         Url::parse("https://example.com/one/image.jpg")
338           .unwrap()
339           .into()
340       )
341     );
342
343     // absolute url
344     let html_bytes = b"<!DOCTYPE html><html><head><meta property='og:image' content='https://cdn.host.com/image.jpg'></head><body></body></html>";
345     let metadata = html_to_site_metadata(html_bytes, &url).expect("Unable to parse metadata");
346     assert_eq!(
347       metadata.image,
348       Some(Url::parse("https://cdn.host.com/image.jpg").unwrap().into())
349     );
350
351     // protocol relative url
352     let html_bytes = b"<!DOCTYPE html><html><head><meta property='og:image' content='//example.com/image.jpg'></head><body></body></html>";
353     let metadata = html_to_site_metadata(html_bytes, &url).expect("Unable to parse metadata");
354     assert_eq!(
355       metadata.image,
356       Some(Url::parse("https://example.com/image.jpg").unwrap().into())
357     );
358   }
359 }