Handle thumbnail encoder error (#2745)

* Treat thumbnail encoder error

* Use a single instance of WebPConfig for the entire application

* Clippy
This commit is contained in:
Consoli
2024-09-30 17:00:57 -03:00
committed by GitHub
parent 1e13dd414a
commit 1e2fe41d75

View File

@@ -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<Vec<Extension>> = Lazy::new(|| {
THUMBNAILABLE_EXTENSIONS.clone()
});
static WEBP_CONFIG: std::sync::LazyLock<WebPConfig> = 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<u8>`
// which implies on a unwanted clone...
Ok(encoder.encode(TARGET_QUALITY).deref().to_owned())
Ok(thumb.deref().to_owned())
}
#[instrument(