Rewrite offline playlist system to use file paths instead of IDs
Problem: Pending playlists created offline used auto-increment track IDs, which change on library re-scans, causing FOREIGN KEY errors and data loss. Solution: Store file_path instead of IDs for local tracks. Paths are stable identifiers that only change if files are moved/renamed. Backend changes: - Add local_track_paths TEXT column to pending_playlist_sync table - Update PendingPlaylist struct to include local_track_paths field - Modify create/add functions to accept paths instead of IDs - Add get_track_by_path command to resolve paths to current IDs - Keep local_track_ids for backward compatibility Frontend changes: - Update PendingPlaylist interface with localTrackPaths field - Fetch file paths from track IDs before creating pending playlists - Implement path-to-ID resolution in sync function - Gracefully handle missing files (log warning and skip) - Backward compatible with old ID-based pending playlists Testing: - Simulated playlist creation with 3 local tracks (IDs 197-199) - Simulated re-scan by changing IDs to 1500-1502 - Verified path resolution finds tracks with new IDs correctly - Verified missing files are handled gracefully This makes the offline playlist system robust and production-ready.
This commit is contained in:
1 parent
8a6816d8d3
commit
c1d32b4181
11 files changed
+481
-111
No files matched your search
@@ -53,6 +53,7 @@
|
||||
let playlistSettings = $state<Map<number, PlaylistSettings>>(new Map());
|
||||
let playlistStats = $state<Map<number, PlaylistStats>>(new Map());
|
||||
let localTrackCounts = $state<Map<number, number>>(new Map());
|
||||
let pendingPlaylistsMap = $state<Map<number, import('$lib/stores/offlineStore').PendingPlaylist>>(new Map());
|
||||
let loading = $state(true);
|
||||
|
||||
// Offline state
|
||||
@@ -203,32 +204,95 @@
|
||||
async function loadData() {
|
||||
loading = true;
|
||||
try {
|
||||
const [playlistsResult, settingsResult, statsResult, localCountsResult] = await Promise.all([
|
||||
invoke<Playlist[]>('get_user_playlists'),
|
||||
invoke<PlaylistSettings[]>('playlist_get_all_settings'),
|
||||
invoke<PlaylistStats[]>('playlist_get_all_stats'),
|
||||
invoke<Record<string, number>>('playlist_get_all_local_track_counts')
|
||||
]);
|
||||
if (offlineStatus.isOffline) {
|
||||
// Offline mode: Load both regular playlists AND pending playlists
|
||||
const [playlistsResult, pendingPlaylistsResult, settingsResult, statsResult, localCountsResult] = await Promise.all([
|
||||
invoke<Playlist[]>('get_user_playlists'),
|
||||
invoke<import('$lib/stores/offlineStore').PendingPlaylist[]>('get_pending_playlists'),
|
||||
invoke<PlaylistSettings[]>('playlist_get_all_settings'),
|
||||
invoke<PlaylistStats[]>('playlist_get_all_stats'),
|
||||
invoke<Record<string, number>>('playlist_get_all_local_track_counts')
|
||||
]);
|
||||
|
||||
playlists = playlistsResult;
|
||||
// Process regular playlists
|
||||
playlists = playlistsResult;
|
||||
|
||||
const settingsMap = new Map<number, PlaylistSettings>();
|
||||
for (const s of settingsResult) {
|
||||
settingsMap.set(s.qobuz_playlist_id, s);
|
||||
// Process pending playlists and add them to the playlists array
|
||||
const newPendingMap = new Map<number, import('$lib/stores/offlineStore').PendingPlaylist>();
|
||||
const pendingAsPlaylists: Playlist[] = pendingPlaylistsResult.map(p => {
|
||||
const negativeId = -p.id;
|
||||
newPendingMap.set(negativeId, p);
|
||||
|
||||
return {
|
||||
id: negativeId,
|
||||
name: p.name,
|
||||
tracks_count: p.trackIds.length, // Only Qobuz tracks for correct filtering
|
||||
images: [],
|
||||
duration: 0,
|
||||
owner: { id: 0, name: 'You (Offline)' }
|
||||
};
|
||||
});
|
||||
|
||||
// Combine regular and pending playlists
|
||||
playlists = [...playlistsResult, ...pendingAsPlaylists];
|
||||
pendingPlaylistsMap = newPendingMap;
|
||||
|
||||
// Process settings
|
||||
const settingsMap = new Map<number, PlaylistSettings>();
|
||||
for (const s of settingsResult) {
|
||||
settingsMap.set(s.qobuz_playlist_id, s);
|
||||
}
|
||||
playlistSettings = settingsMap;
|
||||
|
||||
// Process stats
|
||||
const statsMap = new Map<number, PlaylistStats>();
|
||||
for (const s of statsResult) {
|
||||
statsMap.set(s.qobuz_playlist_id, s);
|
||||
}
|
||||
playlistStats = statsMap;
|
||||
|
||||
// Process local track counts for regular playlists
|
||||
const localCountsMap = new Map<number, number>();
|
||||
for (const [id, count] of Object.entries(localCountsResult)) {
|
||||
localCountsMap.set(Number(id), count);
|
||||
}
|
||||
|
||||
// Add local track counts for pending playlists
|
||||
for (const [negativeId, pending] of newPendingMap.entries()) {
|
||||
localCountsMap.set(negativeId, pending.localTrackIds.length);
|
||||
}
|
||||
|
||||
localTrackCounts = localCountsMap;
|
||||
} else {
|
||||
// Online mode: Load only regular playlists
|
||||
const [playlistsResult, settingsResult, statsResult, localCountsResult] = await Promise.all([
|
||||
invoke<Playlist[]>('get_user_playlists'),
|
||||
invoke<PlaylistSettings[]>('playlist_get_all_settings'),
|
||||
invoke<PlaylistStats[]>('playlist_get_all_stats'),
|
||||
invoke<Record<string, number>>('playlist_get_all_local_track_counts')
|
||||
]);
|
||||
|
||||
playlists = playlistsResult;
|
||||
pendingPlaylistsMap = new Map(); // Clear pending playlists when online
|
||||
|
||||
const settingsMap = new Map<number, PlaylistSettings>();
|
||||
for (const s of settingsResult) {
|
||||
settingsMap.set(s.qobuz_playlist_id, s);
|
||||
}
|
||||
playlistSettings = settingsMap;
|
||||
|
||||
const statsMap = new Map<number, PlaylistStats>();
|
||||
for (const s of statsResult) {
|
||||
statsMap.set(s.qobuz_playlist_id, s);
|
||||
}
|
||||
playlistStats = statsMap;
|
||||
|
||||
const localCountsMap = new Map<number, number>();
|
||||
for (const [id, count] of Object.entries(localCountsResult)) {
|
||||
localCountsMap.set(Number(id), count);
|
||||
}
|
||||
localTrackCounts = localCountsMap;
|
||||
}
|
||||
playlistSettings = settingsMap;
|
||||
|
||||
const statsMap = new Map<number, PlaylistStats>();
|
||||
for (const s of statsResult) {
|
||||
statsMap.set(s.qobuz_playlist_id, s);
|
||||
}
|
||||
playlistStats = statsMap;
|
||||
|
||||
const localCountsMap = new Map<number, number>();
|
||||
for (const [id, count] of Object.entries(localCountsResult)) {
|
||||
localCountsMap.set(Number(id), count);
|
||||
}
|
||||
localTrackCounts = localCountsMap;
|
||||
} catch (err) {
|
||||
console.error('Failed to load playlists:', err);
|
||||
} finally {
|
||||
|
||||
Reference in new issue
Block a user