Cleaning up fetch_pictrs request

This commit is contained in:
Dessalines 2021-08-18 12:50:12 -04:00
parent bc3a8ee481
commit 41d752523b
4 changed files with 14 additions and 16 deletions

View file

@ -52,7 +52,7 @@ impl PerformCrud for CreatePost {
// Fetch post links and pictrs cached image // Fetch post links and pictrs cached image
let data_url = data.url.as_ref(); let data_url = data.url.as_ref();
let (metadata_res, pictrs_thumbnail) = let (metadata_res, pictrs_thumbnail) =
fetch_site_metadata_and_pictrs_data(context.client(), data_url).await?; fetch_site_metadata_and_pictrs_data(context.client(), data_url).await;
let (embed_title, embed_description, embed_html) = metadata_res let (embed_title, embed_description, embed_html) = metadata_res
.map(|u| (u.title, u.description, u.html)) .map(|u| (u.title, u.description, u.html))
.unwrap_or((None, None, None)); .unwrap_or((None, None, None));

View file

@ -52,7 +52,7 @@ impl PerformCrud for EditPost {
// Fetch post links and Pictrs cached image // Fetch post links and Pictrs cached image
let data_url = data.url.as_ref(); let data_url = data.url.as_ref();
let (metadata_res, pictrs_thumbnail) = let (metadata_res, pictrs_thumbnail) =
fetch_site_metadata_and_pictrs_data(context.client(), data_url).await?; fetch_site_metadata_and_pictrs_data(context.client(), data_url).await;
let (embed_title, embed_description, embed_html) = metadata_res let (embed_title, embed_description, embed_html) = metadata_res
.map(|u| (u.title, u.description, u.html)) .map(|u| (u.title, u.description, u.html))
.unwrap_or((None, None, None)); .unwrap_or((None, None, None));

View file

@ -189,7 +189,7 @@ impl FromApub for Post {
let thumbnail_url: Option<Url> = page.image.clone().map(|i| i.url); let thumbnail_url: Option<Url> = page.image.clone().map(|i| i.url);
let (metadata_res, pictrs_thumbnail) = if let Some(url) = &page.url { let (metadata_res, pictrs_thumbnail) = if let Some(url) = &page.url {
fetch_site_metadata_and_pictrs_data(context.client(), Some(url)).await? fetch_site_metadata_and_pictrs_data(context.client(), Some(url)).await
} else { } else {
(None, thumbnail_url) (None, thumbnail_url)
}; };

View file

@ -120,7 +120,7 @@ pub(crate) struct PictrsFile {
pub(crate) async fn fetch_pictrs( pub(crate) async fn fetch_pictrs(
client: &Client, client: &Client,
image_url: &Url, image_url: &Url,
) -> Result<Option<PictrsResponse>, LemmyError> { ) -> Result<PictrsResponse, LemmyError> {
if let Some(pictrs_url) = Settings::get().pictrs_url { if let Some(pictrs_url) = Settings::get().pictrs_url {
is_image_content_type(client, image_url).await?; is_image_content_type(client, image_url).await?;
@ -138,12 +138,12 @@ pub(crate) async fn fetch_pictrs(
.map_err(|e| RecvError(e.to_string()))?; .map_err(|e| RecvError(e.to_string()))?;
if response.msg == "ok" { if response.msg == "ok" {
Ok(Some(response)) Ok(response)
} else { } else {
Err(anyhow!("{}", &response.msg).into()) Err(anyhow!("{}", &response.msg).into())
} }
} else { } else {
Ok(None) Err(anyhow!("pictrs_url not set up in config").into())
} }
} }
@ -151,7 +151,7 @@ pub(crate) async fn fetch_pictrs(
pub async fn fetch_site_metadata_and_pictrs_data( pub async fn fetch_site_metadata_and_pictrs_data(
client: &Client, client: &Client,
url: Option<&Url>, url: Option<&Url>,
) -> Result<(Option<SiteMetadata>, Option<Url>), LemmyError> { ) -> (Option<SiteMetadata>, Option<Url>) {
match &url { match &url {
Some(url) => { Some(url) => {
// Fetch metadata // Fetch metadata
@ -162,22 +162,19 @@ pub async fn fetch_site_metadata_and_pictrs_data(
// Fetch pictrs thumbnail // Fetch pictrs thumbnail
let pictrs_hash = match &metadata_option { let pictrs_hash = match &metadata_option {
Some(metadata_res) => match &metadata_res.image { Some(metadata_res) => match &metadata_res.image {
// Metadata, with image
// Try to generate a small thumbnail if there's a full sized one from post-links
Some(metadata_image) => fetch_pictrs(client, metadata_image) Some(metadata_image) => fetch_pictrs(client, metadata_image)
.await .await
// Ignore the error, just return None
.unwrap_or(None)
.map(|r| r.files[0].file.to_owned()), .map(|r| r.files[0].file.to_owned()),
// Try to generate a small thumbnail if there's a full sized one from post-links // Metadata, but no image
None => fetch_pictrs(client, url) None => fetch_pictrs(client, url)
.await .await
// Ignore the error, just return None
.unwrap_or(None)
.map(|r| r.files[0].file.to_owned()), .map(|r| r.files[0].file.to_owned()),
}, },
// No metadata, try to fetch the URL as an image
None => fetch_pictrs(client, url) None => fetch_pictrs(client, url)
.await .await
// Ignore the error, just return None
.unwrap_or(None)
.map(|r| r.files[0].file.to_owned()), .map(|r| r.files[0].file.to_owned()),
}; };
@ -191,11 +188,12 @@ pub async fn fetch_site_metadata_and_pictrs_data(
)) ))
.ok() .ok()
}) })
.ok()
.flatten(); .flatten();
Ok((metadata_option, pictrs_thumbnail)) (metadata_option, pictrs_thumbnail)
} }
None => Ok((None, None)), None => (None, None),
} }
} }