Upload to Qobuz now attaches Plex sidecar rows via the existing add_plex_track_to_playlist helper (positions after the local block), and the local entity is deleted only when the sidecar attach succeeds - a DB failure keeps the playlist with an upload-incomplete toast instead of silently dropping it. Playlist manager: local playlists interleave into the active sort with the same comparators as the Qobuz set (name/tracks real interleave; playcount sorts locals as zero, custom as after-the-positioned-set, recent after the API order - documented). u64-keyed mutators verified safe with local: ids. Offline covers: cached_tracks.artwork_path turned out to have zero writers in history, so CachedTrackInfo gained artwork_path + file_path and a frontend-agnostic resolve_cover_path chain (index path, CMAF cover.jpg, v1 sibling cover.jpg). The offline manager album rows and the local- playlist cached rows now resolve covers through it. cc
145 lines
4.8 KiB
Rust
145 lines
4.8 KiB
Rust
//! Maintenance operations on the offline cache: bulk delete and re-download.
|
|
//! Pure logic — no Tauri state. Callable from any future TUI or headless binary.
|
|
|
|
use std::path::Path;
|
|
|
|
use crate::cmaf_store::BundleLayout;
|
|
use crate::{CachedTrackInfo, OfflineCacheDb, OfflineCacheStatus};
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct AlbumRemovalReport {
|
|
pub album_id: String,
|
|
pub removed_track_ids: Vec<u64>,
|
|
pub freed_bytes: u64,
|
|
}
|
|
|
|
/// Removes all cached tracks of an album: SQLite rows + on-disk CMAF bundles.
|
|
/// Filesystem errors per-track are logged and not propagated; SQLite is the
|
|
/// source of truth and the bundle directories are best-effort cleanup.
|
|
pub fn remove_album_cached_tracks(
|
|
db: &OfflineCacheDb,
|
|
offline_root: &Path,
|
|
album_id: &str,
|
|
) -> Result<AlbumRemovalReport, String> {
|
|
let (ids, bytes) = db.delete_album_tracks(album_id)?;
|
|
for &track_id in &ids {
|
|
let layout = BundleLayout::new(offline_root, track_id);
|
|
if layout.track_dir.exists() {
|
|
if let Err(e) = std::fs::remove_dir_all(&layout.track_dir) {
|
|
log::warn!(
|
|
"Failed to remove CMAF dir for track {}: {} (continuing)",
|
|
track_id,
|
|
e
|
|
);
|
|
}
|
|
}
|
|
}
|
|
Ok(AlbumRemovalReport {
|
|
album_id: album_id.to_string(),
|
|
removed_track_ids: ids,
|
|
freed_bytes: bytes,
|
|
})
|
|
}
|
|
|
|
/// Pre-flight cache-limit check executed before queuing new downloads.
|
|
///
|
|
/// Returns `Ok(())` when:
|
|
/// - `limit_bytes` is `None` (no limit configured); OR
|
|
/// - the current `total_size_bytes` from the cache stats is strictly below the
|
|
/// configured limit.
|
|
///
|
|
/// Returns `Err(message)` when the cache has already met or exceeded the
|
|
/// configured limit. The message is suitable for surfacing to end users via a
|
|
/// toast.
|
|
///
|
|
/// Intentionally simple: it does not predict the new track's size. Sufficient
|
|
/// for v1 — the user gets feedback the moment they hit the limit and can free
|
|
/// space or raise the limit through the offline cache manager.
|
|
pub fn check_cache_limit(
|
|
db: &OfflineCacheDb,
|
|
offline_root: &Path,
|
|
limit_bytes: Option<u64>,
|
|
) -> Result<(), String> {
|
|
let limit = match limit_bytes {
|
|
Some(value) => value,
|
|
None => return Ok(()),
|
|
};
|
|
let stats = db.get_stats(&offline_root.to_string_lossy(), Some(limit))?;
|
|
if stats.total_size_bytes >= limit {
|
|
return Err(
|
|
"Offline cache limit reached. Free space or raise the limit in the offline cache manager."
|
|
.to_string(),
|
|
);
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Filters tracks targeted by re-download: skip in-flight Downloading,
|
|
/// optionally restrict to Failed only.
|
|
pub fn select_redownload_targets(
|
|
tracks: &[CachedTrackInfo],
|
|
failed_only: bool,
|
|
) -> Vec<&CachedTrackInfo> {
|
|
tracks
|
|
.iter()
|
|
.filter(|track| match track.status {
|
|
OfflineCacheStatus::Downloading => false,
|
|
OfflineCacheStatus::Failed => true,
|
|
_ => !failed_only,
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::OfflineCacheStatus;
|
|
|
|
fn track_with_status(id: u64, status: OfflineCacheStatus) -> CachedTrackInfo {
|
|
CachedTrackInfo {
|
|
track_id: id,
|
|
title: format!("t{}", id),
|
|
artist: "A".into(),
|
|
album: None,
|
|
album_id: None,
|
|
duration_secs: 0,
|
|
file_size_bytes: 0,
|
|
quality: "lossless".into(),
|
|
bit_depth: None,
|
|
sample_rate: None,
|
|
status,
|
|
progress_percent: 0,
|
|
error_message: None,
|
|
created_at: "".into(),
|
|
last_accessed_at: "".into(),
|
|
artwork_path: None,
|
|
file_path: "".into(),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn redownload_targets_full_skips_only_downloading() {
|
|
let tracks = vec![
|
|
track_with_status(1, OfflineCacheStatus::Ready),
|
|
track_with_status(2, OfflineCacheStatus::Downloading),
|
|
track_with_status(3, OfflineCacheStatus::Failed),
|
|
track_with_status(4, OfflineCacheStatus::Queued),
|
|
];
|
|
let picked = select_redownload_targets(&tracks, false);
|
|
let ids: Vec<u64> = picked.iter().map(|track| track.track_id).collect();
|
|
assert_eq!(ids, vec![1, 3, 4]);
|
|
}
|
|
|
|
#[test]
|
|
fn redownload_targets_failed_only_returns_failed() {
|
|
let tracks = vec![
|
|
track_with_status(1, OfflineCacheStatus::Ready),
|
|
track_with_status(2, OfflineCacheStatus::Failed),
|
|
track_with_status(3, OfflineCacheStatus::Downloading),
|
|
];
|
|
let picked = select_redownload_targets(&tracks, true);
|
|
let ids: Vec<u64> = picked.iter().map(|track| track.track_id).collect();
|
|
assert_eq!(ids, vec![2]);
|
|
}
|
|
}
|