refactor(backend): rename module and APIs for compliance

- Renamed download_cache module to offline_cache
- Updated Tauri command names (e.g., cache_track_for_offline)
- Updated event names (offline:caching_started, etc.)
- Updated library database function names
- Updated frontend invoke calls to use new command names
This commit is contained in:
vicrodh committed 2026-01-23 12:14:39 -06:00
1 parent 84a2164079
commit 6d5d264778
15 files changed
+348 -343

No files matched your search

+15 -15
View File
@@ -1443,7 +1443,7 @@ pub struct BackfillReport {
#[tauri::command]
pub async fn library_backfill_downloads(
state: State<'_, LibraryState>,
download_cache_state: State<'_, crate::download_cache::DownloadCacheState>,
offline_cache_state: State<'_, crate::offline_cache::OfflineCacheState>,
) -> Result<BackfillReport, String> {
log::info!("Command: library_backfill_downloads");
@@ -1455,14 +1455,14 @@ pub async fn library_backfill_downloads(
failed_tracks: Vec::new(),
};
// Get all ready downloads directly from download cache DB
let downloads = {
let cache_db = download_cache_state.db.lock().await;
// Get all ready cached tracks directly from offline cache DB
let cached_tracks = {
let cache_db = offline_cache_state.db.lock().await;
let mut stmt = cache_db
.conn()
.prepare("SELECT track_id, title, artist, album, album_id, duration_secs, file_path, quality, bit_depth, sample_rate FROM cached_tracks WHERE status = 'ready'")
.map_err(|e| format!("Failed to query downloads: {}", e))?;
.map_err(|e| format!("Failed to query cached tracks: {}", e))?;
let rows = stmt
.query_map([], |row| {
@@ -1480,14 +1480,14 @@ pub async fn library_backfill_downloads(
.map_err(|e| format!("Failed to map rows: {}", e))?;
rows.collect::<Result<Vec<_>, _>>()
.map_err(|e| format!("Failed to collect downloads: {}", e))?
.map_err(|e| format!("Failed to collect cached tracks: {}", e))?
}; // cache_db lock is dropped here
report.total_downloads = downloads.len();
report.total_downloads = cached_tracks.len();
let library_db = state.db.lock().await;
for (track_id, title, artist, album, duration_secs, file_path, bit_depth, sample_rate) in downloads {
for (track_id, title, artist, album, duration_secs, file_path, bit_depth, sample_rate) in cached_tracks {
// Strategy: Try to match by qobuz_track_id first, then by file_path
// This handles both intact downloads and downloads damaged by scanner
@@ -1502,15 +1502,15 @@ pub async fn library_backfill_downloads(
if exists_by_id {
// Track exists with correct qobuz_track_id (not damaged)
// Check if it just needs source repair
match library_db.is_qobuz_download_by_path(&file_path) {
match library_db.is_qobuz_cached_track_by_path(&file_path) {
Ok(true) => {
// Already marked as qobuz_download, nothing to do
// Already marked as cached track, nothing to do
report.skipped_tracks += 1;
},
Ok(false) => {
// Has qobuz_track_id but lost source marker - unusual case
log::info!("Repairing source for track with intact ID {}: {}", track_id, title);
match library_db.repair_qobuz_download_by_path(track_id, &file_path) {
match library_db.repair_qobuz_cached_track_by_path(track_id, &file_path) {
Ok(true) => report.repaired_tracks += 1,
Ok(false) => report.skipped_tracks += 1,
Err(e) => {
@@ -1520,7 +1520,7 @@ pub async fn library_backfill_downloads(
}
},
Err(e) => {
log::warn!("Failed to check download status for {}: {}", track_id, e);
log::warn!("Failed to check cached track status for {}: {}", track_id, e);
report.failed_tracks.push(title);
}
}
@@ -1529,8 +1529,8 @@ pub async fn library_backfill_downloads(
if exists_by_path {
// Track exists by path but lost qobuz_track_id (damaged by scanner)
log::info!("Repairing damaged download (lost ID) {}: {}", track_id, title);
match library_db.repair_qobuz_download_by_path(track_id, &file_path) {
log::info!("Repairing damaged cached track (lost ID) {}: {}", track_id, title);
match library_db.repair_qobuz_cached_track_by_path(track_id, &file_path) {
Ok(true) => report.repaired_tracks += 1,
Ok(false) => report.skipped_tracks += 1,
Err(e) => {
@@ -1542,7 +1542,7 @@ pub async fn library_backfill_downloads(
}
// Track doesn't exist - insert as new
match library_db.insert_qobuz_download_direct(
match library_db.insert_qobuz_cached_track_direct(
track_id,
&title,
&artist,
+27 -22
View File
@@ -719,9 +719,9 @@ impl LibraryDatabase {
// === Track Management ===
/// Check if a file path is already registered as a Qobuz download
/// Returns true if the file exists with source = 'qobuz_download'
pub fn is_qobuz_download_by_path(&self, file_path: &str) -> Result<bool, LibraryError> {
/// Check if a file path is already registered as a Qobuz cached track
/// Returns true if the file exists with source = 'qobuz_download' (legacy name kept for DB compatibility)
pub fn is_qobuz_cached_track_by_path(&self, file_path: &str) -> Result<bool, LibraryError> {
let count: i64 = self.conn
.query_row(
"SELECT COUNT(*) FROM local_tracks WHERE file_path = ?1 AND source = 'qobuz_download'",
@@ -732,11 +732,11 @@ impl LibraryDatabase {
Ok(count > 0)
}
/// Insert or update a track (skips if file is already a Qobuz download)
/// Insert or update a track (skips if file is already a Qobuz cached track)
pub fn insert_track(&self, track: &LocalTrack) -> Result<i64, LibraryError> {
// Don't overwrite Qobuz downloads with scanned data
if self.is_qobuz_download_by_path(&track.file_path)? {
log::debug!("Skipping track insert - already exists as Qobuz download: {}", track.file_path);
// Don't overwrite Qobuz cached tracks with scanned data
if self.is_qobuz_cached_track_by_path(&track.file_path)? {
log::debug!("Skipping track insert - already exists as Qobuz cached track: {}", track.file_path);
// Return the existing ID
return self.conn
.query_row(
@@ -2120,7 +2120,8 @@ impl LibraryDatabase {
/// Repair a track by file_path - restores both qobuz_track_id and source
/// This handles tracks that were damaged by scanner's INSERT OR REPLACE
/// Returns true if the track was found and updated
pub fn repair_qobuz_download_by_path(
/// Note: Database source field remains 'qobuz_download' for compatibility
pub fn repair_qobuz_cached_track_by_path(
&self,
qobuz_track_id: u64,
file_path: &str,
@@ -2131,7 +2132,7 @@ impl LibraryDatabase {
WHERE file_path = ?2 AND (source IS NULL OR source != 'qobuz_download')",
params![qobuz_track_id as i64, file_path],
)
.map_err(|e| LibraryError::Database(format!("Failed to repair download by path: {}", e)))?;
.map_err(|e| LibraryError::Database(format!("Failed to repair cached track by path: {}", e)))?;
Ok(updated > 0)
}
@@ -2147,8 +2148,9 @@ impl LibraryDatabase {
Ok(count > 0)
}
/// Insert a Qobuz download into the library
pub fn insert_qobuz_download_direct(
/// Insert a Qobuz cached track into the library
/// Note: Database source field remains 'qobuz_download' for compatibility
pub fn insert_qobuz_cached_track_direct(
&self,
track_id: u64,
title: &str,
@@ -2202,12 +2204,13 @@ impl LibraryDatabase {
track_id as i64,
],
)
.map_err(|e| LibraryError::Database(format!("Failed to insert Qobuz download: {}", e)))?;
.map_err(|e| LibraryError::Database(format!("Failed to insert Qobuz cached track: {}", e)))?;
Ok(())
}
/// Insert a Qobuz download with full metadata and album grouping
pub fn insert_qobuz_download_with_grouping(
/// Insert a Qobuz cached track with full metadata and album grouping
/// Note: Database source field remains 'qobuz_download' for compatibility
pub fn insert_qobuz_cached_track_with_grouping(
&self,
track_id: u64,
title: &str,
@@ -2228,7 +2231,7 @@ impl LibraryDatabase {
use std::time::SystemTime;
// First, remove any existing entry for this qobuz_track_id to prevent duplicates
let _ = self.remove_qobuz_download(track_id);
let _ = self.remove_qobuz_cached_track(track_id);
// Get file size if file exists
let file_size_bytes = std::fs::metadata(file_path)
@@ -2276,27 +2279,29 @@ impl LibraryDatabase {
track_id as i64,
],
)
.map_err(|e| LibraryError::Database(format!("Failed to insert Qobuz download: {}", e)))?;
.map_err(|e| LibraryError::Database(format!("Failed to insert Qobuz cached track: {}", e)))?;
Ok(())
}
/// Remove a Qobuz download from the library by track_id
pub fn remove_qobuz_download(&self, qobuz_track_id: u64) -> Result<(), LibraryError> {
/// Remove a Qobuz cached track from the library by track_id
/// Note: Database source field remains 'qobuz_download' for compatibility
pub fn remove_qobuz_cached_track(&self, qobuz_track_id: u64) -> Result<(), LibraryError> {
self.conn.execute(
"DELETE FROM local_tracks WHERE qobuz_track_id = ?1 AND source = 'qobuz_download'",
params![qobuz_track_id as i64],
)
.map_err(|e| LibraryError::Database(format!("Failed to remove Qobuz download: {}", e)))?;
.map_err(|e| LibraryError::Database(format!("Failed to remove Qobuz cached track: {}", e)))?;
Ok(())
}
/// Remove all Qobuz downloads from the library
pub fn remove_all_qobuz_downloads(&self) -> Result<usize, LibraryError> {
/// Remove all Qobuz cached tracks from the library
/// Note: Database source field remains 'qobuz_download' for compatibility
pub fn remove_all_qobuz_cached_tracks(&self) -> Result<usize, LibraryError> {
let count = self.conn.execute(
"DELETE FROM local_tracks WHERE source = 'qobuz_download'",
[],
)
.map_err(|e| LibraryError::Database(format!("Failed to remove all Qobuz downloads: {}", e)))?;
.map_err(|e| LibraryError::Database(format!("Failed to remove all Qobuz cached tracks: {}", e)))?;
Ok(count)
}