From 41421991d69c146762cab6260d3480b597f0450a Mon Sep 17 00:00:00 2001 From: Nutomic Date: Fri, 3 Jan 2025 19:09:24 +0000 Subject: [PATCH] Error handling for thumbnail generation (ref #5196) (#5298) --- crates/api_common/src/request.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/crates/api_common/src/request.rs b/crates/api_common/src/request.rs index 02e889872..4ccd032b9 100644 --- a/crates/api_common/src/request.rs +++ b/crates/api_common/src/request.rs @@ -32,7 +32,7 @@ use reqwest::{ }; use reqwest_middleware::ClientWithMiddleware; use serde::{Deserialize, Serialize}; -use tracing::info; +use tracing::{info, warn}; use url::Url; use urlencoding::encode; use webpage::HTML; @@ -173,15 +173,23 @@ pub async fn generate_post_link_metadata( metadata.opengraph_data.image.clone() }; + // Attempt to generate a thumbnail depending on the instance settings. Either by proxying, + // storing image persistently in pict-rs or returning the remote url directly as thumbnail. let thumbnail_url = if let (false, Some(url)) = (is_image_post, custom_thumbnail) { - proxy_image_link(url, &context).await.ok() - } else if let (true, Some(url)) = (allow_generate_thumbnail, image_url) { + proxy_image_link(url.clone(), &context) + .await + .map_err(|e| warn!("Failed to proxy thumbnail: {e}")) + .ok() + .or(Some(url.into())) + } else if let (true, Some(url)) = (allow_generate_thumbnail, image_url.clone()) { generate_pictrs_thumbnail(&url, &context) .await + .map_err(|e| warn!("Failed to generate thumbnail: {e}")) .ok() .map(Into::into) + .or(image_url) } else { - metadata.opengraph_data.image.clone() + image_url.clone() }; let form = PostUpdateForm {