2025-10-12 21:39:20 +02:00
|
|
|
//! Module de gestion du cache d'images avec conversion WebP
|
|
|
|
|
//!
|
|
|
|
|
//! Ce module étend le cache générique de `pmocache` avec des fonctionnalités
|
2025-10-17 22:15:00 +02:00
|
|
|
//! spécifiques aux images : conversion WebP automatique lors du téléchargement.
|
2025-10-12 21:39:20 +02:00
|
|
|
|
2025-10-17 14:36:39 +02:00
|
|
|
use anyhow::Result;
|
2025-10-17 22:15:00 +02:00
|
|
|
use pmocache::{CacheConfig, StreamTransformer};
|
|
|
|
|
use std::sync::Arc;
|
2025-10-17 14:36:39 +02:00
|
|
|
|
|
|
|
|
/// Configuration pour le cache de couvertures
|
|
|
|
|
pub struct CoversConfig;
|
|
|
|
|
|
|
|
|
|
impl CacheConfig for CoversConfig {
|
|
|
|
|
fn file_extension() -> &'static str {
|
|
|
|
|
"webp"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn cache_type() -> &'static str {
|
|
|
|
|
"image"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn cache_name() -> &'static str {
|
|
|
|
|
"covers"
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-07 08:33:06 +02:00
|
|
|
|
2025-10-17 22:15:00 +02:00
|
|
|
/// Type alias pour le cache de couvertures avec conversion WebP
|
|
|
|
|
pub type Cache = pmocache::Cache<CoversConfig>;
|
2025-10-07 08:33:06 +02:00
|
|
|
|
2025-10-17 22:15:00 +02:00
|
|
|
/// Créateur de transformer WebP
|
|
|
|
|
///
|
|
|
|
|
/// Convertit automatiquement toute image téléchargée en format WebP
|
|
|
|
|
fn create_webp_transformer() -> StreamTransformer {
|
2025-10-28 00:10:51 +01:00
|
|
|
Box::new(|mut input, mut file, context| {
|
2025-10-17 22:15:00 +02:00
|
|
|
Box::pin(async move {
|
|
|
|
|
// Télécharger tout en mémoire
|
2025-10-19 17:26:06 +02:00
|
|
|
let bytes = input.bytes().await?;
|
2025-10-17 22:15:00 +02:00
|
|
|
|
|
|
|
|
// Convertir en WebP
|
|
|
|
|
let img = image::load_from_memory(&bytes)
|
|
|
|
|
.map_err(|e| format!("Image decode error: {}", e))?;
|
2025-10-19 13:42:29 +02:00
|
|
|
let webp_data =
|
|
|
|
|
crate::webp::encode_webp(&img).map_err(|e| format!("WebP encode error: {}", e))?;
|
2025-10-17 22:15:00 +02:00
|
|
|
|
|
|
|
|
// Écrire et mettre à jour la progression
|
|
|
|
|
use tokio::io::AsyncWriteExt;
|
2025-10-19 13:42:29 +02:00
|
|
|
file.write_all(&webp_data)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|e| e.to_string())?;
|
2025-10-17 22:15:00 +02:00
|
|
|
file.flush().await.map_err(|e| e.to_string())?;
|
2025-10-28 00:10:51 +01:00
|
|
|
context.report_progress(webp_data.len() as u64);
|
2025-10-17 22:15:00 +02:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
})
|
|
|
|
|
})
|
2025-10-17 14:36:39 +02:00
|
|
|
}
|
|
|
|
|
|
2025-10-17 22:15:00 +02:00
|
|
|
/// Crée un cache de couvertures avec conversion WebP automatique
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `dir` - Répertoire de stockage du cache
|
|
|
|
|
/// * `limit` - Limite de taille du cache (nombre d'images)
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// Instance du cache configurée pour la conversion WebP automatique
|
|
|
|
|
///
|
|
|
|
|
/// # Exemple
|
|
|
|
|
///
|
|
|
|
|
/// ```rust,no_run
|
|
|
|
|
/// use pmocovers::cache;
|
|
|
|
|
///
|
2025-10-18 09:58:39 +02:00
|
|
|
/// let cache = cache::new_cache("./cache", 1000).unwrap();
|
2025-10-17 22:15:00 +02:00
|
|
|
/// ```
|
2025-10-18 09:58:39 +02:00
|
|
|
pub fn new_cache(dir: &str, limit: usize) -> Result<Cache> {
|
2025-10-17 22:15:00 +02:00
|
|
|
let transformer_factory = Arc::new(|| create_webp_transformer());
|
2025-10-18 09:58:39 +02:00
|
|
|
Cache::with_transformer(dir, limit, Some(transformer_factory))
|
|
|
|
|
}
|