1 use crate::{settings::Settings, LemmyError};
4 use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
6 use serde::Deserialize;
7 use std::future::Future;
10 #[derive(Clone, Debug, Error)]
11 #[error("Error sending request, {0}")]
12 struct SendError(pub String);
14 #[derive(Clone, Debug, Error)]
15 #[error("Error receiving response, {0}")]
16 pub struct RecvError(pub String);
18 pub async fn retry<F, Fut, T>(f: F) -> Result<T, reqwest::Error>
21 Fut: Future<Output = Result<T, reqwest::Error>>,
23 retry_custom(|| async { Ok((f)().await) }).await
26 async fn retry_custom<F, Fut, T>(f: F) -> Result<T, reqwest::Error>
29 Fut: Future<Output = Result<Result<T, reqwest::Error>, reqwest::Error>>,
31 let mut response: Option<Result<T, reqwest::Error>> = None;
35 Ok(t) => return Ok(t),
38 response = Some(Err(e));
49 #[derive(Deserialize, Debug)]
50 pub(crate) struct IframelyResponse {
51 title: Option<String>,
52 description: Option<String>,
53 thumbnail_url: Option<String>,
57 pub(crate) async fn fetch_iframely(
60 ) -> Result<IframelyResponse, LemmyError> {
61 let fetch_url = format!("{}/oembed?url={}", Settings::get().iframely_url, url);
63 let response = retry(|| client.get(&fetch_url).send()).await?;
65 let res: IframelyResponse = response
68 .map_err(|e| RecvError(e.to_string()))?;
72 #[derive(Deserialize, Debug, Clone)]
73 pub(crate) struct PictrsResponse {
74 files: Vec<PictrsFile>,
78 #[derive(Deserialize, Debug, Clone)]
79 pub(crate) struct PictrsFile {
84 pub(crate) async fn fetch_pictrs(
87 ) -> Result<PictrsResponse, LemmyError> {
88 is_image_content_type(client, image_url).await?;
90 let fetch_url = format!(
91 "{}/image/download?url={}",
92 Settings::get().pictrs_url,
93 utf8_percent_encode(image_url, NON_ALPHANUMERIC) // TODO this might not be needed
96 let response = retry(|| client.get(&fetch_url).send()).await?;
98 let response: PictrsResponse = response
101 .map_err(|e| RecvError(e.to_string()))?;
103 if response.msg == "ok" {
106 Err(anyhow!("{}", &response.msg).into())
110 pub async fn fetch_iframely_and_pictrs_data(
121 // Fetch iframely data
122 let (iframely_title, iframely_description, iframely_thumbnail_url, iframely_html) =
123 match fetch_iframely(client, url).await {
124 Ok(res) => (res.title, res.description, res.thumbnail_url, res.html),
126 error!("iframely err: {}", e);
127 (None, None, None, None)
131 // Fetch pictrs thumbnail
132 let pictrs_hash = match iframely_thumbnail_url {
133 Some(iframely_thumbnail_url) => match fetch_pictrs(client, &iframely_thumbnail_url).await {
134 Ok(res) => Some(res.files[0].file.to_owned()),
136 error!("pictrs err: {}", e);
140 // Try to generate a small thumbnail if iframely is not supported
141 None => match fetch_pictrs(client, &url).await {
142 Ok(res) => Some(res.files[0].file.to_owned()),
144 error!("pictrs err: {}", e);
150 // The full urls are necessary for federation
151 let pictrs_thumbnail = if let Some(pictrs_hash) = pictrs_hash {
153 "{}/pictrs/image/{}",
154 Settings::get().get_protocol_and_hostname(),
163 iframely_description,
168 None => (None, None, None, None),
172 async fn is_image_content_type(client: &Client, test: &str) -> Result<(), LemmyError> {
173 let response = retry(|| client.get(test).send()).await?;
178 .ok_or_else(|| anyhow!("No Content-Type header"))?
180 .starts_with("image/")
184 Err(anyhow!("Not an image type.").into())
190 use crate::request::is_image_content_type;
194 actix_rt::System::new("tset_image").block_on(async move {
195 let client = reqwest::Client::default();
196 assert!(is_image_content_type(&client, "https://1734811051.rsc.cdn77.org/data/images/full/365645/as-virus-kills-navajos-in-their-homes-tribal-women-provide-lifeline.jpg?w=600?w=650").await.is_ok());
197 assert!(is_image_content_type(&client,
198 "https://twitter.com/BenjaminNorton/status/1259922424272957440?s=20"
205 // These helped with testing
207 // fn test_iframely() {
208 // let res = fetch_iframely(client, "https://www.redspark.nu/?p=15341").await;
209 // assert!(res.is_ok());
213 // fn test_pictshare() {
214 // let res = fetch_pictshare("https://upload.wikimedia.org/wikipedia/en/2/27/The_Mandalorian_logo.jpg");
215 // assert!(res.is_ok());
216 // let res_other = fetch_pictshare("https://upload.wikimedia.org/wikipedia/en/2/27/The_Mandalorian_logo.jpgaoeu");
217 // assert!(res_other.is_err());