From 1e2fe41d7541e17574fcdb85e122eefc2c111ad7 Mon Sep 17 00:00:00 2001 From: Consoli Date: Mon, 30 Sep 2024 17:00:57 -0300 Subject: [PATCH] Handle thumbnail encoder error (#2745) * Treat thumbnail encoder error * Use a single instance of WebPConfig for the entire application * Clippy --- .../media_processor/helpers/thumbnailer.rs | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/core/crates/heavy-lifting/src/media_processor/helpers/thumbnailer.rs b/core/crates/heavy-lifting/src/media_processor/helpers/thumbnailer.rs index caa06a3fe..adcdaae61 100644 --- a/core/crates/heavy-lifting/src/media_processor/helpers/thumbnailer.rs +++ b/core/crates/heavy-lifting/src/media_processor/helpers/thumbnailer.rs @@ -32,7 +32,7 @@ use tokio::{ }; use tracing::{error, instrument, trace}; use uuid::Uuid; -use webp::Encoder; +use webp::{Encoder, WebPConfig}; // Files names constants pub const THUMBNAIL_CACHE_DIR_NAME: &str = "thumbnails"; @@ -92,6 +92,15 @@ pub static ALL_THUMBNAILABLE_EXTENSIONS: Lazy> = Lazy::new(|| { THUMBNAILABLE_EXTENSIONS.clone() }); +static WEBP_CONFIG: std::sync::LazyLock = std::sync::LazyLock::new(|| { + let mut config = WebPConfig::new().expect("failed to instantiate global webp config"); + config.lossless = 0; + config.alpha_compression = 1; + config.quality = TARGET_QUALITY; + + config +}); + /// This type is used to pass the relevant data to the frontend so it can request the thumbnail. /// Tt supports extending the shard hex to support deeper directory structures in the future #[derive(Debug, Serialize, Deserialize, Type, Clone)] @@ -354,10 +363,17 @@ fn inner_generate_image_thumbnail( ) })?; + let thumb = encoder.encode_advanced(&WEBP_CONFIG).map_err(|reason| { + thumbnailer::NonCriticalThumbnailerError::WebPEncoding( + file_path.clone(), + format!("{reason:?}"), + ) + })?; + // Type `WebPMemory` is !Send, which makes the `Future` in this function `!Send`, // this make us `deref` to have a `&[u8]` and then `to_owned` to make a `Vec` // which implies on a unwanted clone... - Ok(encoder.encode(TARGET_QUALITY).deref().to_owned()) + Ok(thumb.deref().to_owned()) } #[instrument(