Refactoring manuel

This commit is contained in:
2025-10-17 14:36:39 +02:00
parent f94a600ff8
commit 2f9abff6f1
18 changed files with 664 additions and 334 deletions

View File

@@ -3,132 +3,163 @@
//! Ce module étend le cache générique de `pmocache` avec des fonctionnalités
//! spécifiques aux images : conversion WebP et génération de variantes.
use std::path::PathBuf;
use std::sync::Arc;
use anyhow::{anyhow, Result};
use pmocache::{Cache as GenericCache, CacheConfig};
use anyhow::Result;
use pmocache::{CacheConfig, FileCache};
use crate::webp;
use crate::db::DB;
use std::path::PathBuf;
use std::ops::Deref;
/// Configuration pour le cache de couvertures
pub struct CoversConfig;
impl CacheConfig for CoversConfig {
fn file_extension() -> &'static str {
"webp"
}
fn table_name() -> &'static str {
"covers"
}
fn cache_type() -> &'static str {
"image"
}
/// Cache name (ex: "covers", "audio", "cache")
fn cache_name() -> &'static str {
"covers"
}
}
/// Cache d'images avec conversion WebP et génération de variantes
///
/// Gère le téléchargement, la conversion en WebP, le stockage et la génération
/// de variantes de tailles pour les images de couvertures.
/// Format des fichiers : `{pk}.{qualificatif}.webp`
/// Exemple : `a1b2c3d4.orig.webp`, `a1b2c3d4.thumb.webp`
///
/// Ce type est un wrapper autour de `pmocache::Cache<CoversConfig>` qui permet
/// d'implémenter le trait `FileCache` avec conversion WebP automatique.
#[derive(Debug)]
pub struct Cache {
cache: GenericCache,
pub(crate) dir: PathBuf,
pub(crate) limit: usize,
pub db: Arc<DB>,
}
pub struct Cache(pmocache::Cache<CoversConfig>);
impl Cache {
/// Crée un nouveau cache d'images
///
/// # Arguments
///
/// * `dir` - Répertoire de stockage du cache
/// * `limit` - Limite de taille du cache (nombre d'images)
///
/// # Exemple
///
/// ```rust,no_run
/// use pmocovers::Cache;
///
/// let cache = Cache::new("./cache", 1000).unwrap();
/// ```
pub fn new(dir: &str, limit: usize) -> Result<Self> {
let config = CacheConfig::new(dir, limit, "covers", "orig.webp");
let cache = GenericCache::new(config)?;
pub fn new(dir: &str, limit: usize, base_url: &str) -> Result<Self> {
Ok(Self(pmocache::Cache::new(dir, limit, base_url)?))
}
}
Ok(Self {
dir: PathBuf::from(dir),
limit,
db: Arc::clone(&cache.db),
cache,
})
/// Permet d'accéder aux méthodes publiques de `pmocache::Cache` directement
impl Deref for Cache {
type Target = pmocache::Cache<CoversConfig>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
/// Implémentation de FileCache pour Cache avec conversion WebP automatique
impl FileCache for Cache {
fn cache_type(&self) -> &str {
CoversConfig::cache_type()
}
/// Télécharge une image depuis une URL et l'ajoute au cache
///
/// # Arguments
///
/// * `url` - URL de l'image à télécharger
///
/// # Returns
///
/// La clé primaire (pk) de l'image dans le cache
pub async fn add_from_url(&self, url: &str) -> Result<String> {
fn validate_data(&self, data: &[u8]) -> Result<Vec<u8>> {
// Convertir l'image en WebP
let img = image::load_from_memory(data)?;
webp::encode_webp(&img)
}
async fn add_from_url(&self, url: &str, collection: Option<&str>) -> Result<String> {
let response = reqwest::get(url).await?;
if !response.status().is_success() {
return Err(anyhow!("Bad status: {}", response.status()));
return Err(anyhow::anyhow!("Bad status: {}", response.status()));
}
let data = response.bytes().await?;
self.add(url, &data).await
self.add(url, &data, collection).await
}
/// S'assure qu'une image est présente dans le cache
///
/// Si l'image existe déjà, retourne sa clé. Sinon, la télécharge.
///
/// # Arguments
///
/// * `url` - URL de l'image
pub async fn ensure_from_url(&self, url: &str) -> Result<String> {
self.cache.ensure_from_url(url, None).await
}
/// Ajoute une image au cache avec conversion en WebP
///
/// # Arguments
///
/// * `url` - URL source de l'image
/// * `data` - Données brutes de l'image
pub async fn add(&self, url: &str, data: &[u8]) -> Result<String> {
async fn ensure_from_url(&self, url: &str, collection: Option<&str>) -> Result<String> {
let pk = pmocache::pk_from_url(url);
let orig_path = self.dir.join(format!("{}.orig.webp", pk));
// Vérifier si le fichier existe déjà
if !orig_path.exists() {
// Convertir l'image en WebP
let img = image::load_from_memory(data)?;
let webp_data = webp::encode_webp(&img)?;
tokio::fs::write(&orig_path, webp_data).await?;
if self.db.get(&pk).is_ok() {
let file_path = self.file_path(&pk);
if file_path.exists() {
return Ok(pk);
}
}
// Ajouter à la DB (sans collection pour les covers)
self.db.add(&pk, url, None)?;
self.add_from_url(url, collection).await
}
async fn add(&self, url: &str, data: &[u8], collection: Option<&str>) -> Result<String> {
// Valider et convertir les données en WebP
let webp_data = self.validate_data(data)?;
let pk = pmocache::pk_from_url(url);
let file_path = self.file_path(&pk);
if !file_path.exists() {
tokio::fs::write(&file_path, &webp_data).await?;
}
self.db.add(&pk, url, collection)?;
Ok(pk)
}
/// Récupère le chemin d'une image dans le cache
///
/// # Arguments
///
/// * `pk` - Clé primaire de l'image
pub async fn get(&self, pk: &str) -> Result<PathBuf> {
self.cache.get(pk).await
async fn get(&self, pk: &str) -> Result<PathBuf> {
self.db.get(pk)?;
self.db.update_hit(pk)?;
let file_path = self.file_path(pk);
if file_path.exists() {
Ok(file_path)
} else {
Err(anyhow::anyhow!("File not found"))
}
}
/// Supprime tous les fichiers et entrées du cache
pub async fn purge(&self) -> Result<()> {
self.cache.purge().await
async fn get_collection(&self, collection: &str) -> Result<Vec<PathBuf>> {
let entries = self.db.get_by_collection(collection)?;
let mut paths = Vec::new();
for entry in entries {
let path = self.file_path(&entry.pk);
if path.exists() {
paths.push(path);
}
}
Ok(paths)
}
/// Consolide le cache en supprimant les orphelins et en re-téléchargeant les images manquantes
pub async fn consolidate(&self) -> Result<()> {
async fn purge(&self) -> Result<()> {
let cache_dir = PathBuf::from(self.get_cache_dir());
let mut entries = tokio::fs::read_dir(&cache_dir).await?;
while let Some(entry) = entries.next_entry().await? {
if entry.path().is_file() && entry.path() != cache_dir.join("cache.db") {
tokio::fs::remove_file(entry.path()).await?;
}
}
self.db
.purge()
.map_err(|e| anyhow::anyhow!("Database error: {}", e))
}
async fn consolidate(&self) -> Result<()> {
// Récupérer la liste des entrées à traiter
let entries = self.db.get_all()?;
// Supprimer les entrées sans fichiers correspondants
for entry in entries {
let orig_path = self.dir.join(format!("{}.orig.webp", entry.pk));
if !orig_path.exists() {
let file_path = self.file_path(&entry.pk);
if !file_path.exists() {
match reqwest::get(&entry.source_url).await {
Ok(response) if response.status().is_success() => {
let data = response.bytes().await?;
self.add(&entry.source_url, &data).await?;
self.add(&entry.source_url, &data, entry.collection.as_deref())
.await?;
}
_ => {
self.db.delete(&entry.pk)?;
@@ -138,13 +169,14 @@ impl Cache {
}
// Supprimer les fichiers sans entrées DB correspondantes
let mut dir_entries = tokio::fs::read_dir(&self.dir).await?;
let cache_dir_path = PathBuf::from(self.get_cache_dir());
let mut dir_entries = tokio::fs::read_dir(&cache_dir_path).await?;
while let Some(entry) = dir_entries.next_entry().await? {
let path = entry.path();
if path.is_file() && path != self.dir.join("cache.db") {
if path.is_file() && path != cache_dir_path.join("cache.db") {
if let Some(file_name) = path.file_name().and_then(|n| n.to_str()) {
if file_name.ends_with(".orig.webp") {
let pk = file_name.trim_end_matches(".orig.webp");
// Format attendu: {pk}.{qualifier}.{EXT}
if let Some(pk) = file_name.split('.').next() {
if self.db.get(pk).is_err() {
tokio::fs::remove_file(path).await?;
}
@@ -156,8 +188,11 @@ impl Cache {
Ok(())
}
/// Retourne le répertoire du cache
pub fn cache_dir(&self) -> String {
self.dir.to_string_lossy().to_string()
fn get_cache_dir(&self) -> String {
self.get_cache_dir()
}
fn get_base_url(&self) -> &str {
self.get_base_url()
}
}