lazy cache webapp

This commit is contained in:
2025-12-15 12:18:01 +01:00
parent 90399b408a
commit d2d8111668
5 changed files with 255 additions and 14 deletions

View File

@@ -21,12 +21,28 @@
use crate::{CacheStatus, MusicSourceError, Result};
use pmoaudiocache::{AudioMetadata, Cache as AudioCache};
use pmocovers::Cache as CoverCache;
use serde_json::Value as JsonValue;
use serde_json::{json, Value as JsonValue};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::io::AsyncRead;
use tokio::sync::RwLock;
fn log_metadata_warning(key: &str, audio_pk: &str, error: &str) {
#[cfg(feature = "server")]
{
tracing::warn!(
"Failed to store metadata {} for {}: {}",
key,
audio_pk,
error
);
}
#[cfg(not(feature = "server"))]
{
let _ = (key, audio_pk, error);
}
}
/// Métadonnées d'une piste en cache
#[derive(Debug, Clone)]
pub struct TrackMetadata {
@@ -252,6 +268,7 @@ impl SourceCacheManager {
.db
.set_a_metadata_by_key(&lazy_pk, "audio_metadata", json);
}
self.seed_audio_metadata(&lazy_pk, &meta);
}
Ok(lazy_pk)
@@ -303,6 +320,62 @@ impl SourceCacheManager {
cache.remove(track_id);
}
/// Pré-remplit les métadonnées audio pour une entrée lazy
fn seed_audio_metadata(&self, audio_pk: &str, metadata: &AudioMetadata) {
let audio_pk = audio_pk.to_string();
let mut store = |key: &str, value: JsonValue| {
if let Err(e) = self.audio_cache.db.set_a_metadata(&audio_pk, key, value) {
log_metadata_warning(key, &audio_pk, &e.to_string());
}
};
if let Some(title) = metadata.title.as_ref() {
store("title", JsonValue::String(title.clone()));
}
if let Some(artist) = metadata.artist.as_ref() {
store("artist", JsonValue::String(artist.clone()));
}
if let Some(album) = metadata.album.as_ref() {
store("album", JsonValue::String(album.clone()));
}
if let Some(year) = metadata.year {
store("year", json!(year));
}
if let Some(track_number) = metadata.track_number {
store("track_number", json!(track_number));
}
if let Some(track_total) = metadata.track_total {
store("track_total", json!(track_total));
}
if let Some(disc_number) = metadata.disc_number {
store("disc_number", json!(disc_number));
}
if let Some(disc_total) = metadata.disc_total {
store("disc_total", json!(disc_total));
}
if let Some(genre) = metadata.genre.as_ref() {
store("genre", JsonValue::String(genre.clone()));
}
if let Some(duration_secs) = metadata.duration_secs {
store("duration_secs", json!(duration_secs));
store("duration_ms", json!(duration_secs * 1000));
}
if let Some(sample_rate) = metadata.sample_rate {
store("sample_rate", json!(sample_rate));
}
if let Some(channels) = metadata.channels {
store("channels", json!(channels));
}
if let Some(bitrate) = metadata.bitrate {
store("bitrate", json!(bitrate));
}
if let Some(conversion) = metadata.conversion.as_ref() {
if let Ok(value) = serde_json::to_value(conversion) {
store("conversion", value);
}
}
}
/// Stocke une métadonnée personnalisée pour un fichier audio caché
///
/// Permet de stocker des métadonnées arbitraires (clé/valeur JSON) associées