diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 5a7718360..0fec42ff5 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -686,6 +686,7 @@ pub fn run() { plex::plex_cache_save_sections, plex::plex_cache_get_tracks, plex::plex_cache_save_tracks, + plex::plex_cache_update_track_quality, plex::plex_cache_get_albums, plex::plex_cache_get_album_tracks, plex::plex_cache_search_tracks, diff --git a/src-tauri/src/plex/mod.rs b/src-tauri/src/plex/mod.rs index e4041a368..bdcc16479 100644 --- a/src-tauri/src/plex/mod.rs +++ b/src-tauri/src/plex/mod.rs @@ -111,6 +111,15 @@ pub struct PlexCachedTrack { pub album_key: String, } +#[derive(Debug, Clone, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct PlexTrackQualityUpdate { + pub rating_key: String, + pub container: Option, + pub sampling_rate_hz: Option, + pub bit_depth: Option, +} + #[derive(Debug, Clone, Deserialize)] #[serde(rename_all = "camelCase")] struct PlexPinResponse { @@ -1167,6 +1176,46 @@ pub fn plex_cache_save_tracks( Ok(tracks.len()) } +#[tauri::command] +pub fn plex_cache_update_track_quality(updates: Vec) -> Result { + if updates.is_empty() { + return Ok(0); + } + + let mut conn = open_plex_cache_db()?; + let tx = conn + .transaction() + .map_err(|e| format!("Failed to start Plex cache quality update transaction: {}", e))?; + + let now = now_epoch_secs(); + let mut updated_rows = 0usize; + for update in &updates { + let affected = tx + .execute( + "UPDATE plex_cache_tracks + SET container = COALESCE(?2, container), + sampling_rate_hz = COALESCE(?3, sampling_rate_hz), + bit_depth = COALESCE(?4, bit_depth), + updated_at = ?5 + WHERE rating_key = ?1", + params![ + update.rating_key, + update.container, + update.sampling_rate_hz.map(|v| v as i64), + update.bit_depth.map(|v| v as i64), + now, + ], + ) + .map_err(|e| format!("Failed to update Plex cache track quality: {}", e))?; + updated_rows += affected; + } + + tx.commit() + .map_err(|e| format!("Failed to commit Plex cache quality update transaction: {}", e))?; + + Ok(updated_rows) +} + #[tauri::command] pub fn plex_cache_clear() -> Result<(), String> { let conn = open_plex_cache_db()?; diff --git a/src/lib/components/views/LocalLibraryView.svelte b/src/lib/components/views/LocalLibraryView.svelte index 90fddcd06..96e87916d 100644 --- a/src/lib/components/views/LocalLibraryView.svelte +++ b/src/lib/components/views/LocalLibraryView.svelte @@ -149,6 +149,13 @@ bitDepth?: number; } + interface PlexTrackQualityUpdate { + ratingKey: string; + container?: string; + samplingRateHz?: number; + bitDepth?: number; + } + interface LocalArtist { name: string; album_count: number; @@ -1961,12 +1968,25 @@ ); const metadataByRatingKey = new Map(); + const qualityUpdates: PlexTrackQualityUpdate[] = []; for (const entry of metadataEntries) { if (!entry) continue; metadataByRatingKey.set(entry[0], entry[1]); + qualityUpdates.push({ + ratingKey: entry[0], + container: entry[1].container ?? entry[1].codec, + samplingRateHz: entry[1].samplingRateHz, + bitDepth: entry[1].bitDepth + }); } if (metadataByRatingKey.size === 0) return tracks; + if (qualityUpdates.length > 0) { + invoke('plex_cache_update_track_quality', { updates: qualityUpdates }).catch((error) => { + console.warn('[LocalLibrary] Failed to persist Plex track quality updates:', error); + }); + } + return tracks.map((track) => { const metadata = metadataByRatingKey.get(track.file_path); if (!metadata) return track;