update all command handlers for Option-wrapped per-user state
Modify callers in commands/, cast/, library/, and lib.rs to unwrap Option<Store> with appropriate error messages. Fix drop(db) to drop(guard__) for correct mutex release.
This commit is contained in:
1 parent
aab3e67641
commit
dbb5d998de
11 files changed
+365
-191
No files matched your search
@@ -175,7 +175,8 @@ pub async fn cast_play_local_track(
|
||||
library_state: State<'_, LibraryState>,
|
||||
) -> Result<(), String> {
|
||||
let track = {
|
||||
let db = library_state.db.lock().await;
|
||||
let db_opt__ = library_state.db.lock().await;
|
||||
let db = db_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.get_track(track_id)
|
||||
.map_err(|e| e.to_string())?
|
||||
.ok_or_else(|| "Track not found".to_string())?
|
||||
|
||||
@@ -40,10 +40,10 @@ async fn maybe_purge_offline_cache_for_invalid_subscription(
|
||||
return;
|
||||
}
|
||||
};
|
||||
match store.should_purge_offline_cache(now) {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
log::warn!("Failed to evaluate purge condition: {}", e);
|
||||
match store.as_ref().and_then(|s| s.should_purge_offline_cache(now).ok()) {
|
||||
Some(v) => v,
|
||||
None => {
|
||||
log::warn!("Failed to evaluate purge condition");
|
||||
false
|
||||
}
|
||||
}
|
||||
@@ -59,9 +59,11 @@ async fn maybe_purge_offline_cache_for_invalid_subscription(
|
||||
return;
|
||||
}
|
||||
|
||||
if let Ok(store) = subscription_state.lock() {
|
||||
if let Err(e) = store.mark_offline_cache_purged(now) {
|
||||
log::warn!("Failed to persist purge timestamp: {}", e);
|
||||
if let Ok(guard) = subscription_state.lock() {
|
||||
if let Some(store) = guard.as_ref() {
|
||||
if let Err(e) = store.mark_offline_cache_purged(now) {
|
||||
log::warn!("Failed to persist purge timestamp: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -80,8 +82,10 @@ pub async fn login(
|
||||
|
||||
match client.login(&email, &password).await {
|
||||
Ok(session) => {
|
||||
if let Ok(store) = subscription_state.lock() {
|
||||
let _ = store.mark_valid(now);
|
||||
if let Ok(guard) = subscription_state.lock() {
|
||||
if let Some(store) = guard.as_ref() {
|
||||
let _ = store.mark_valid(now);
|
||||
}
|
||||
}
|
||||
Ok(LoginResponse {
|
||||
success: true,
|
||||
@@ -93,8 +97,10 @@ pub async fn login(
|
||||
})
|
||||
}
|
||||
Err(ApiError::IneligibleUser) => {
|
||||
if let Ok(store) = subscription_state.lock() {
|
||||
let _ = store.mark_invalid(now);
|
||||
if let Ok(guard) = subscription_state.lock() {
|
||||
if let Some(store) = guard.as_ref() {
|
||||
let _ = store.mark_invalid(now);
|
||||
}
|
||||
}
|
||||
maybe_purge_offline_cache_for_invalid_subscription(
|
||||
now,
|
||||
@@ -221,8 +227,10 @@ pub async fn auto_login(
|
||||
let now = now_unix_secs();
|
||||
match client.login(&creds.email, &creds.password).await {
|
||||
Ok(session) => {
|
||||
if let Ok(store) = subscription_state.lock() {
|
||||
let _ = store.mark_valid(now);
|
||||
if let Ok(guard) = subscription_state.lock() {
|
||||
if let Some(store) = guard.as_ref() {
|
||||
let _ = store.mark_valid(now);
|
||||
}
|
||||
}
|
||||
Ok(LoginResponse {
|
||||
success: true,
|
||||
@@ -234,8 +242,10 @@ pub async fn auto_login(
|
||||
})
|
||||
}
|
||||
Err(ApiError::IneligibleUser) => {
|
||||
if let Ok(store) = subscription_state.lock() {
|
||||
let _ = store.mark_invalid(now);
|
||||
if let Ok(guard) = subscription_state.lock() {
|
||||
if let Some(store) = guard.as_ref() {
|
||||
let _ = store.mark_invalid(now);
|
||||
}
|
||||
}
|
||||
maybe_purge_offline_cache_for_invalid_subscription(
|
||||
now,
|
||||
|
||||
@@ -25,6 +25,7 @@ pub fn clear_cache(state: State<'_, AppState>) -> Result<(), String> {
|
||||
#[tauri::command]
|
||||
pub async fn clear_artist_cache(cache_state: State<'_, ApiCacheState>) -> Result<usize, String> {
|
||||
log::info!("Command: clear_artist_cache");
|
||||
let cache = cache_state.cache.lock().await;
|
||||
let guard = cache_state.cache.lock().await;
|
||||
let cache = guard.as_ref().ok_or("No active session - please log in")?;
|
||||
cache.clear_all_artists()
|
||||
}
|
||||
@@ -40,7 +40,8 @@ pub async fn listenbrainz_set_enabled(
|
||||
|
||||
// Persist setting
|
||||
{
|
||||
let cache = state.cache.lock().await;
|
||||
let cache_opt__ = state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
cache.set_enabled(enabled)?;
|
||||
}
|
||||
|
||||
@@ -67,7 +68,8 @@ pub async fn listenbrainz_connect(
|
||||
|
||||
// Persist credentials
|
||||
{
|
||||
let cache = state.cache.lock().await;
|
||||
let cache_opt__ = state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
cache.set_credentials(Some(&token), Some(&user_info.user_name))?;
|
||||
}
|
||||
|
||||
@@ -89,7 +91,8 @@ pub async fn listenbrainz_disconnect(
|
||||
|
||||
// Clear persisted credentials
|
||||
{
|
||||
let cache = state.cache.lock().await;
|
||||
let cache_opt__ = state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
cache.clear_credentials()?;
|
||||
}
|
||||
|
||||
@@ -173,7 +176,8 @@ pub async fn listenbrainz_queue_listen(
|
||||
) -> Result<i64, String> {
|
||||
log::info!("Command: listenbrainz_queue_listen - {} - {}", artist, track);
|
||||
|
||||
let cache = state.cache.lock().await;
|
||||
let cache_opt__ = state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
cache.queue_listen(
|
||||
timestamp,
|
||||
&artist,
|
||||
@@ -193,7 +197,8 @@ pub async fn listenbrainz_get_queue(
|
||||
limit: Option<u32>,
|
||||
state: State<'_, ListenBrainzSharedState>,
|
||||
) -> Result<Vec<QueuedListen>, String> {
|
||||
let cache = state.cache.lock().await;
|
||||
let cache_opt__ = state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
cache.get_queued_listens(limit.unwrap_or(50))
|
||||
}
|
||||
|
||||
@@ -202,7 +207,8 @@ pub async fn listenbrainz_get_queue(
|
||||
pub async fn listenbrainz_get_queue_count(
|
||||
state: State<'_, ListenBrainzSharedState>,
|
||||
) -> Result<u32, String> {
|
||||
let cache = state.cache.lock().await;
|
||||
let cache_opt__ = state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
cache.get_queue_count()
|
||||
}
|
||||
|
||||
@@ -212,7 +218,8 @@ pub async fn listenbrainz_mark_sent(
|
||||
ids: Vec<i64>,
|
||||
state: State<'_, ListenBrainzSharedState>,
|
||||
) -> Result<(), String> {
|
||||
let cache = state.cache.lock().await;
|
||||
let cache_opt__ = state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
cache.mark_listens_sent(&ids)
|
||||
}
|
||||
|
||||
@@ -225,7 +232,8 @@ pub async fn listenbrainz_flush_queue(
|
||||
|
||||
// Get pending listens
|
||||
let pending = {
|
||||
let cache = state.cache.lock().await;
|
||||
let cache_opt__ = state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
cache.get_queued_listens(50)?
|
||||
};
|
||||
|
||||
@@ -261,7 +269,8 @@ pub async fn listenbrainz_flush_queue(
|
||||
Err(e) => {
|
||||
log::warn!("Failed to submit queued listen {}: {}", listen.id, e);
|
||||
// Increment attempt count
|
||||
let cache = state.cache.lock().await;
|
||||
let cache_opt__ = state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
let _ = cache.increment_attempts(listen.id);
|
||||
}
|
||||
}
|
||||
@@ -269,7 +278,8 @@ pub async fn listenbrainz_flush_queue(
|
||||
|
||||
// Mark successful ones as sent
|
||||
if !sent_ids.is_empty() {
|
||||
let cache = state.cache.lock().await;
|
||||
let cache_opt__ = state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
cache.mark_listens_sent(&sent_ids)?;
|
||||
}
|
||||
|
||||
@@ -285,7 +295,8 @@ pub async fn listenbrainz_clear_queue(
|
||||
state: State<'_, ListenBrainzSharedState>,
|
||||
) -> Result<(), String> {
|
||||
log::info!("Command: listenbrainz_clear_queue");
|
||||
let cache = state.cache.lock().await;
|
||||
let cache_opt__ = state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
cache.clear_queue()
|
||||
}
|
||||
|
||||
@@ -295,6 +306,7 @@ pub async fn listenbrainz_cleanup_queue(
|
||||
older_than_days: Option<u32>,
|
||||
state: State<'_, ListenBrainzSharedState>,
|
||||
) -> Result<u32, String> {
|
||||
let cache = state.cache.lock().await;
|
||||
let cache_opt__ = state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
cache.cleanup_sent_listens(older_than_days.unwrap_or(7))
|
||||
}
|
||||
@@ -24,7 +24,8 @@ pub async fn musicbrainz_resolve_track(
|
||||
// Check cache first (short lock)
|
||||
if let Some(ref isrc_val) = isrc {
|
||||
let cached = {
|
||||
let cache = state.cache.lock().await;
|
||||
let cache_opt__ = state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
cache.get_recording(isrc_val).ok().flatten()
|
||||
};
|
||||
if let Some(cached) = cached {
|
||||
@@ -41,7 +42,8 @@ pub async fn musicbrainz_resolve_track(
|
||||
let resolved = recording_to_resolved(recording);
|
||||
// Cache the result (short lock)
|
||||
{
|
||||
let cache = state.cache.lock().await;
|
||||
let cache_opt__ = state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
let _ = cache.set_recording(isrc_val, &resolved);
|
||||
}
|
||||
return Ok(resolved);
|
||||
@@ -64,7 +66,8 @@ pub async fn musicbrainz_resolve_track(
|
||||
let resolved = recording_to_resolved(recording);
|
||||
// Cache by ISRC if available
|
||||
if let Some(ref isrc_val) = isrc {
|
||||
let cache = state.cache.lock().await;
|
||||
let cache_opt__ = state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
let _ = cache.set_recording(isrc_val, &resolved);
|
||||
}
|
||||
return Ok(resolved);
|
||||
@@ -78,7 +81,8 @@ pub async fn musicbrainz_resolve_track(
|
||||
// Cache negative result
|
||||
if let Some(ref isrc_val) = isrc {
|
||||
let empty = ResolvedTrack::empty();
|
||||
let cache = state.cache.lock().await;
|
||||
let cache_opt__ = state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
let _ = cache.set_recording(isrc_val, &empty);
|
||||
}
|
||||
|
||||
@@ -97,7 +101,8 @@ pub async fn musicbrainz_resolve_artist(
|
||||
|
||||
// Check cache first
|
||||
let cached = {
|
||||
let cache = state.cache.lock().await;
|
||||
let cache_opt__ = state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
cache.get_artist(&name).ok().flatten()
|
||||
};
|
||||
if let Some(cached) = cached {
|
||||
@@ -115,7 +120,8 @@ pub async fn musicbrainz_resolve_artist(
|
||||
let resolved = artist_to_resolved(artist);
|
||||
// Cache the result
|
||||
{
|
||||
let cache = state.cache.lock().await;
|
||||
let cache_opt__ = state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
let _ = cache.set_artist(&name, &resolved);
|
||||
}
|
||||
return Ok(resolved);
|
||||
@@ -129,7 +135,8 @@ pub async fn musicbrainz_resolve_artist(
|
||||
// Cache negative result
|
||||
let empty = ResolvedArtist::empty();
|
||||
{
|
||||
let cache = state.cache.lock().await;
|
||||
let cache_opt__ = state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
let _ = cache.set_artist(&name, &empty);
|
||||
}
|
||||
|
||||
@@ -151,7 +158,8 @@ pub async fn musicbrainz_resolve_release(
|
||||
// Check cache first
|
||||
if let Some(ref upc_val) = upc {
|
||||
let cached = {
|
||||
let cache = state.cache.lock().await;
|
||||
let cache_opt__ = state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
cache.get_release(upc_val).ok().flatten()
|
||||
};
|
||||
if let Some(cached) = cached {
|
||||
@@ -168,7 +176,8 @@ pub async fn musicbrainz_resolve_release(
|
||||
let resolved = release_to_resolved(release);
|
||||
// Cache the result
|
||||
{
|
||||
let cache = state.cache.lock().await;
|
||||
let cache_opt__ = state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
let _ = cache.set_release(upc_val, &resolved);
|
||||
}
|
||||
return Ok(resolved);
|
||||
@@ -191,7 +200,8 @@ pub async fn musicbrainz_resolve_release(
|
||||
let resolved = release_to_resolved(release);
|
||||
// Cache by UPC if available
|
||||
if let Some(ref upc_val) = upc {
|
||||
let cache = state.cache.lock().await;
|
||||
let cache_opt__ = state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
let _ = cache.set_release(upc_val, &resolved);
|
||||
}
|
||||
return Ok(resolved);
|
||||
@@ -205,7 +215,8 @@ pub async fn musicbrainz_resolve_release(
|
||||
// Cache negative result
|
||||
if let Some(ref upc_val) = upc {
|
||||
let empty = ResolvedRelease::empty();
|
||||
let cache = state.cache.lock().await;
|
||||
let cache_opt__ = state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
let _ = cache.set_release(upc_val, &empty);
|
||||
}
|
||||
|
||||
@@ -224,7 +235,8 @@ pub async fn musicbrainz_get_artist_relationships(
|
||||
|
||||
// Check cache first
|
||||
let cached = {
|
||||
let cache = state.cache.lock().await;
|
||||
let cache_opt__ = state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
cache.get_artist_relations(&mbid).ok().flatten()
|
||||
};
|
||||
if let Some(cached) = cached {
|
||||
@@ -237,7 +249,8 @@ pub async fn musicbrainz_get_artist_relationships(
|
||||
let relations = extract_relationships(&response);
|
||||
// Cache the result
|
||||
{
|
||||
let cache = state.cache.lock().await;
|
||||
let cache_opt__ = state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
let _ = cache.set_artist_relations(&mbid, &relations);
|
||||
}
|
||||
Ok(relations)
|
||||
@@ -247,7 +260,8 @@ pub async fn musicbrainz_get_artist_relationships(
|
||||
// Cache negative result
|
||||
let empty = ArtistRelationships::empty();
|
||||
{
|
||||
let cache = state.cache.lock().await;
|
||||
let cache_opt__ = state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
let _ = cache.set_artist_relations(&mbid, &empty);
|
||||
}
|
||||
Ok(empty)
|
||||
@@ -282,7 +296,8 @@ pub async fn musicbrainz_set_enabled(
|
||||
pub async fn musicbrainz_get_cache_stats(
|
||||
state: State<'_, MusicBrainzSharedState>,
|
||||
) -> Result<CacheStats, String> {
|
||||
let cache = state.cache.lock().await;
|
||||
let cache_opt__ = state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
cache.get_stats()
|
||||
}
|
||||
|
||||
@@ -291,7 +306,8 @@ pub async fn musicbrainz_get_cache_stats(
|
||||
pub async fn musicbrainz_clear_cache(
|
||||
state: State<'_, MusicBrainzSharedState>,
|
||||
) -> Result<(), String> {
|
||||
let cache = state.cache.lock().await;
|
||||
let cache_opt__ = state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
cache.clear_all()
|
||||
}
|
||||
|
||||
@@ -300,7 +316,8 @@ pub async fn musicbrainz_clear_cache(
|
||||
pub async fn musicbrainz_cleanup_cache(
|
||||
state: State<'_, MusicBrainzSharedState>,
|
||||
) -> Result<usize, String> {
|
||||
let cache = state.cache.lock().await;
|
||||
let cache_opt__ = state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
cache.cleanup_expired()
|
||||
}
|
||||
|
||||
|
||||
@@ -328,7 +328,8 @@ async fn enrich_from_musicbrainz(
|
||||
) -> Result<(Option<String>, Vec<String>), String> {
|
||||
// Resolve artist to get MBID
|
||||
let resolved = {
|
||||
let cache = mb_state.cache.lock().await;
|
||||
let cache_opt__ = mb_state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
cache.get_artist(name).ok().flatten()
|
||||
};
|
||||
|
||||
|
||||
@@ -97,16 +97,24 @@ pub async fn play_track(
|
||||
|
||||
// First check offline cache (persistent disk cache)
|
||||
{
|
||||
let db = offline_cache.db.lock().await;
|
||||
if let Ok(Some(file_path)) = db.get_file_path(track_id) {
|
||||
let cached_path = {
|
||||
let db_opt__ = offline_cache.db.lock().await;
|
||||
if let Some(db) = db_opt__.as_ref() {
|
||||
if let Ok(Some(file_path)) = db.get_file_path(track_id) {
|
||||
let _ = db.touch(track_id);
|
||||
Some(file_path)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
if let Some(file_path) = cached_path {
|
||||
let path = std::path::Path::new(&file_path);
|
||||
if path.exists() {
|
||||
log::info!("[CACHE HIT] Track {} from OFFLINE cache: {:?}", track_id, path);
|
||||
|
||||
// Update last accessed time
|
||||
let _ = db.touch(track_id);
|
||||
drop(db); // Release lock before reading file
|
||||
|
||||
// Read file and play
|
||||
let audio_data = std::fs::read(path)
|
||||
.map_err(|e| format!("Failed to read cached file: {}", e))?;
|
||||
@@ -115,8 +123,8 @@ pub async fn play_track(
|
||||
|
||||
// Check if prefetch should be skipped (streaming_only mode)
|
||||
let skip_prefetch = {
|
||||
let store = audio_settings.store.lock().map_err(|e| format!("Lock error: {}", e))?;
|
||||
store.get_settings().map(|s| s.streaming_only).unwrap_or(false)
|
||||
let guard = audio_settings.store.lock().map_err(|e| format!("Lock error: {}", e))?;
|
||||
guard.as_ref().and_then(|s| s.get_settings().ok()).map(|s| s.streaming_only).unwrap_or(false)
|
||||
};
|
||||
|
||||
// Prefetch next track in background
|
||||
@@ -142,8 +150,8 @@ pub async fn play_track(
|
||||
|
||||
// Check if prefetch should be skipped (streaming_only mode)
|
||||
let skip_prefetch = {
|
||||
let store = audio_settings.store.lock().map_err(|e| format!("Lock error: {}", e))?;
|
||||
store.get_settings().map(|s| s.streaming_only).unwrap_or(false)
|
||||
let guard = audio_settings.store.lock().map_err(|e| format!("Lock error: {}", e))?;
|
||||
guard.as_ref().and_then(|s| s.get_settings().ok()).map(|s| s.streaming_only).unwrap_or(false)
|
||||
};
|
||||
|
||||
// Prefetch next track in background
|
||||
@@ -170,8 +178,8 @@ pub async fn play_track(
|
||||
|
||||
// Check if prefetch should be skipped (streaming_only mode)
|
||||
let skip_prefetch = {
|
||||
let store = audio_settings.store.lock().map_err(|e| format!("Lock error: {}", e))?;
|
||||
store.get_settings().map(|s| s.streaming_only).unwrap_or(false)
|
||||
let guard = audio_settings.store.lock().map_err(|e| format!("Lock error: {}", e))?;
|
||||
guard.as_ref().and_then(|s| s.get_settings().ok()).map(|s| s.streaming_only).unwrap_or(false)
|
||||
};
|
||||
|
||||
// Prefetch next track in background
|
||||
@@ -192,11 +200,11 @@ pub async fn play_track(
|
||||
|
||||
// Check streaming settings
|
||||
let (stream_first_enabled, buffer_seconds, streaming_only) = {
|
||||
let store = audio_settings.store.lock().map_err(|e| format!("Lock error: {}", e))?;
|
||||
match store.get_settings() {
|
||||
Ok(settings) => (settings.stream_first_track, settings.stream_buffer_seconds, settings.streaming_only),
|
||||
Err(e) => {
|
||||
log::warn!("Failed to get audio settings, using defaults: {}", e);
|
||||
let guard = audio_settings.store.lock().map_err(|e| format!("Lock error: {}", e))?;
|
||||
match guard.as_ref().and_then(|s| s.get_settings().ok()) {
|
||||
Some(settings) => (settings.stream_first_track, settings.stream_buffer_seconds, settings.streaming_only),
|
||||
None => {
|
||||
log::warn!("Failed to get audio settings, using defaults");
|
||||
(false, 3, false)
|
||||
}
|
||||
}
|
||||
@@ -323,7 +331,7 @@ pub async fn prefetch_track(
|
||||
quality: Option<String>,
|
||||
state: State<'_, AppState>,
|
||||
offline_cache: State<'_, OfflineCacheState>,
|
||||
audio_settings: State<'_, AudioSettingsState>,
|
||||
_audio_settings: State<'_, AudioSettingsState>,
|
||||
) -> Result<(), String> {
|
||||
let mut preferred_quality = parse_quality(quality.as_deref());
|
||||
|
||||
@@ -351,12 +359,18 @@ pub async fn prefetch_track(
|
||||
let result = async {
|
||||
// Check persistent offline cache first
|
||||
{
|
||||
let db = offline_cache.db.lock().await;
|
||||
if let Ok(Some(file_path)) = db.get_file_path(track_id) {
|
||||
let cached_path = {
|
||||
let db_opt__ = offline_cache.db.lock().await;
|
||||
if let Some(db) = db_opt__.as_ref() {
|
||||
db.get_file_path(track_id).ok().flatten()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
if let Some(file_path) = cached_path {
|
||||
let path = std::path::Path::new(&file_path);
|
||||
if path.exists() {
|
||||
log::info!("Prefetching track {} from offline cache", track_id);
|
||||
drop(db);
|
||||
let audio_data = std::fs::read(path)
|
||||
.map_err(|e| format!("Failed to read cached file: {}", e))?;
|
||||
cache.insert(track_id, audio_data);
|
||||
@@ -1096,10 +1110,12 @@ pub fn reinit_audio_device(
|
||||
log::info!("Command: reinit_audio_device {:?}", device);
|
||||
|
||||
// Reload settings from database to ensure Player has latest config (including backend_type)
|
||||
if let Ok(store) = audio_settings_state.store.lock() {
|
||||
if let Ok(fresh_settings) = store.get_settings() {
|
||||
log::info!("Reloading audio settings before reinit (backend_type: {:?})", fresh_settings.backend_type);
|
||||
let _ = state.player.reload_settings(fresh_settings);
|
||||
if let Ok(guard) = audio_settings_state.store.lock() {
|
||||
if let Some(store) = guard.as_ref() {
|
||||
if let Ok(fresh_settings) = store.get_settings() {
|
||||
log::info!("Reloading audio settings before reinit (backend_type: {:?})", fresh_settings.backend_type);
|
||||
let _ = state.player.reload_settings(fresh_settings);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -93,7 +93,8 @@ pub async fn get_playlist_suggestions_v2(
|
||||
for artist in &input.artists {
|
||||
// Check cache first
|
||||
let cached = {
|
||||
let cache = mb_state.cache.lock().await;
|
||||
let cache_opt__ = mb_state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
cache.get_artist(&artist.name).ok().flatten()
|
||||
};
|
||||
|
||||
@@ -149,7 +150,8 @@ pub async fn get_playlist_suggestions_v2(
|
||||
confidence: MatchConfidence::High,
|
||||
};
|
||||
{
|
||||
let cache = mb_state.cache.lock().await;
|
||||
let cache_opt__ = mb_state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
let _ = cache.set_artist(&artist.name, &resolved);
|
||||
}
|
||||
artist_info.push((mbid, artist.name.clone()));
|
||||
@@ -176,7 +178,8 @@ pub async fn get_playlist_suggestions_v2(
|
||||
|
||||
// Cache negative result
|
||||
{
|
||||
let cache = mb_state.cache.lock().await;
|
||||
let cache_opt__ = mb_state.cache.lock().await;
|
||||
let cache = cache_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
let _ = cache.set_artist(&artist.name, &ResolvedArtist::empty());
|
||||
}
|
||||
skipped_count += 1;
|
||||
@@ -278,7 +281,8 @@ pub async fn get_playlist_suggestions_v2(
|
||||
pub async fn get_vector_store_stats(
|
||||
store_state: State<'_, ArtistVectorStoreState>,
|
||||
) -> Result<StoreStats, String> {
|
||||
let store = store_state.store.lock().await;
|
||||
let guard = store_state.store.lock().await;
|
||||
let store = guard.as_ref().ok_or("No active session - please log in")?;
|
||||
store.get_stats()
|
||||
}
|
||||
|
||||
@@ -289,7 +293,8 @@ pub async fn cleanup_vector_store(
|
||||
store_state: State<'_, ArtistVectorStoreState>,
|
||||
) -> Result<usize, String> {
|
||||
let max_age_secs = max_age_days.unwrap_or(30) * 24 * 60 * 60;
|
||||
let mut store = store_state.store.lock().await;
|
||||
let mut guard = store_state.store.lock().await;
|
||||
let store = guard.as_mut().ok_or("No active session - please log in")?;
|
||||
store.cleanup_expired(max_age_secs)
|
||||
}
|
||||
|
||||
@@ -298,6 +303,7 @@ pub async fn cleanup_vector_store(
|
||||
pub async fn clear_vector_store(
|
||||
store_state: State<'_, ArtistVectorStoreState>,
|
||||
) -> Result<usize, String> {
|
||||
let mut store = store_state.store.lock().await;
|
||||
let mut guard = store_state.store.lock().await;
|
||||
let store = guard.as_mut().ok_or("No active session - please log in")?;
|
||||
store.clear_all()
|
||||
}
|
||||
@@ -292,7 +292,8 @@ pub async fn get_album(
|
||||
) -> Result<Album, String> {
|
||||
// Check cache first
|
||||
{
|
||||
let cache = cache_state.cache.lock().await;
|
||||
let guard__ = cache_state.cache.lock().await;
|
||||
let cache = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
if let Some(cached_data) = cache.get_album(&album_id, None)? {
|
||||
log::debug!("Cache hit for album {}", album_id);
|
||||
return serde_json::from_str(&cached_data)
|
||||
@@ -307,7 +308,8 @@ pub async fn get_album(
|
||||
|
||||
// Cache the result
|
||||
{
|
||||
let cache = cache_state.cache.lock().await;
|
||||
let guard__ = cache_state.cache.lock().await;
|
||||
let cache = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
let json = serde_json::to_string(&album)
|
||||
.map_err(|e| format!("Failed to serialize album: {}", e))?;
|
||||
cache.set_album(&album_id, &json)?;
|
||||
@@ -348,7 +350,8 @@ pub async fn get_track(
|
||||
) -> Result<Track, String> {
|
||||
// Check cache first
|
||||
{
|
||||
let cache = cache_state.cache.lock().await;
|
||||
let guard__ = cache_state.cache.lock().await;
|
||||
let cache = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
if let Some(cached_data) = cache.get_track(track_id, None)? {
|
||||
log::debug!("Cache hit for track {}", track_id);
|
||||
return serde_json::from_str(&cached_data)
|
||||
@@ -363,7 +366,8 @@ pub async fn get_track(
|
||||
|
||||
// Cache the result
|
||||
{
|
||||
let cache = cache_state.cache.lock().await;
|
||||
let guard__ = cache_state.cache.lock().await;
|
||||
let cache = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
let json = serde_json::to_string(&track)
|
||||
.map_err(|e| format!("Failed to serialize track: {}", e))?;
|
||||
cache.set_track(track_id, &json)?;
|
||||
@@ -389,7 +393,8 @@ pub async fn get_artist(
|
||||
|
||||
// Check cache first
|
||||
{
|
||||
let cache = cache_state.cache.lock().await;
|
||||
let guard__ = cache_state.cache.lock().await;
|
||||
let cache = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
if let Some(cached_data) = cache.get_artist(artist_id, &locale, None)? {
|
||||
log::debug!("Cache hit for artist {} (locale: {})", artist_id, locale);
|
||||
return serde_json::from_str(&cached_data)
|
||||
@@ -407,7 +412,8 @@ pub async fn get_artist(
|
||||
|
||||
// Cache the result
|
||||
{
|
||||
let cache = cache_state.cache.lock().await;
|
||||
let guard__ = cache_state.cache.lock().await;
|
||||
let cache = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
let json = serde_json::to_string(&artist)
|
||||
.map_err(|e| format!("Failed to serialize artist: {}", e))?;
|
||||
cache.set_artist(artist_id, &locale, &json)?;
|
||||
@@ -433,7 +439,8 @@ pub async fn get_artist_basic(
|
||||
|
||||
// Check cache first (reuse same cache - basic response works for both)
|
||||
{
|
||||
let cache = cache_state.cache.lock().await;
|
||||
let guard__ = cache_state.cache.lock().await;
|
||||
let cache = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
if let Some(cached_data) = cache.get_artist(artist_id, &locale, None)? {
|
||||
log::debug!("Cache hit for artist_basic {} (locale: {})", artist_id, locale);
|
||||
return serde_json::from_str(&cached_data)
|
||||
@@ -451,7 +458,8 @@ pub async fn get_artist_basic(
|
||||
|
||||
// Cache the result
|
||||
{
|
||||
let cache = cache_state.cache.lock().await;
|
||||
let guard__ = cache_state.cache.lock().await;
|
||||
let cache = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
let json = serde_json::to_string(&artist)
|
||||
.map_err(|e| format!("Failed to serialize artist: {}", e))?;
|
||||
cache.set_artist(artist_id, &locale, &json)?;
|
||||
|
||||
+17
-17
@@ -217,12 +217,8 @@ pub fn run() {
|
||||
// Initialize API server state for remote control
|
||||
let api_server_state = api_server::ApiServerState::new();
|
||||
// Initialize legal settings state (ToS acceptance persistence)
|
||||
let legal_settings_state = config::legal_settings::LegalSettingsState::new(
|
||||
std::sync::Mutex::new(
|
||||
config::legal_settings::LegalSettingsStore::new()
|
||||
.expect("Failed to initialize legal settings")
|
||||
)
|
||||
);
|
||||
let legal_settings_state = config::legal_settings::create_legal_settings_state()
|
||||
.expect("Failed to initialize legal settings");
|
||||
// Initialize updates state
|
||||
let updates_state = updates::UpdatesState::new()
|
||||
.expect("Failed to initialize updates state");
|
||||
@@ -254,9 +250,11 @@ pub fn run() {
|
||||
.store
|
||||
.lock()
|
||||
.ok()
|
||||
.and_then(|store| {
|
||||
store.get_settings().ok().map(|settings| {
|
||||
(settings.output_device.clone(), settings)
|
||||
.and_then(|guard| {
|
||||
guard.as_ref().and_then(|store| {
|
||||
store.get_settings().ok().map(|settings| {
|
||||
(settings.output_device.clone(), settings)
|
||||
})
|
||||
})
|
||||
})
|
||||
.unwrap_or_else(|| {
|
||||
@@ -338,17 +336,17 @@ pub fn run() {
|
||||
.inner()
|
||||
.clone();
|
||||
let should_purge = {
|
||||
let store = match subscription_state.lock() {
|
||||
let guard = match subscription_state.lock() {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
log::warn!("Subscription state lock error: {}", e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
match store.should_purge_offline_cache(now) {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
log::warn!("Failed to evaluate purge condition: {}", e);
|
||||
match guard.as_ref().and_then(|s| s.should_purge_offline_cache(now).ok()) {
|
||||
Some(v) => v,
|
||||
None => {
|
||||
log::warn!("Failed to evaluate purge condition");
|
||||
false
|
||||
}
|
||||
}
|
||||
@@ -366,15 +364,17 @@ pub fn run() {
|
||||
return;
|
||||
}
|
||||
|
||||
let store = match subscription_state.lock() {
|
||||
let guard = match subscription_state.lock() {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
log::warn!("Subscription state lock error: {}", e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
if let Err(e) = store.mark_offline_cache_purged(now) {
|
||||
log::warn!("Failed to persist purge timestamp: {}", e);
|
||||
if let Some(store) = guard.as_ref() {
|
||||
if let Err(e) = store.mark_offline_cache_purged(now) {
|
||||
log::warn!("Failed to persist purge timestamp: {}", e);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -19,11 +19,28 @@ use crate::network::{is_network_path, MountKind, NetworkFs};
|
||||
|
||||
/// Library state shared across commands
|
||||
pub struct LibraryState {
|
||||
pub db: Arc<Mutex<LibraryDatabase>>,
|
||||
pub db: Arc<Mutex<Option<LibraryDatabase>>>,
|
||||
pub scan_progress: Arc<Mutex<ScanProgress>>,
|
||||
pub scan_cancel: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
impl LibraryState {
|
||||
pub fn init_at(&self, base_dir: &std::path::Path) -> Result<(), String> {
|
||||
std::fs::create_dir_all(base_dir)
|
||||
.map_err(|e| format!("Failed to create directory: {}", e))?;
|
||||
let db_path = base_dir.join("library.db");
|
||||
let db = LibraryDatabase::open(&db_path).map_err(|e| e.to_string())?;
|
||||
let mut guard = self.db.blocking_lock();
|
||||
*guard = Some(db);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn teardown(&self) {
|
||||
let mut guard = self.db.blocking_lock();
|
||||
*guard = None;
|
||||
}
|
||||
}
|
||||
|
||||
fn normalize_library_path(path: &Path) -> PathBuf {
|
||||
fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf())
|
||||
}
|
||||
@@ -71,7 +88,8 @@ pub async fn library_add_folder(
|
||||
|
||||
log::info!("Folder network info: is_network={}, fs_type={:?}", is_network, fs_type);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
let id = db.add_folder_with_network_info(&path, is_network, fs_type.as_deref())
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
@@ -88,7 +106,8 @@ pub async fn library_remove_folder(
|
||||
) -> Result<(), String> {
|
||||
log::info!("Command: library_remove_folder {}", path);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.remove_folder(&path).map_err(|e| e.to_string())?;
|
||||
db.delete_tracks_in_folder(&path).map_err(|e| e.to_string())?;
|
||||
Ok(())
|
||||
@@ -109,7 +128,8 @@ pub async fn library_cleanup_missing_files(
|
||||
) -> Result<CleanupResult, String> {
|
||||
log::info!("Command: library_cleanup_missing_files");
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
|
||||
// Get all track paths
|
||||
let tracks = db.get_all_track_paths().map_err(|e| e.to_string())?;
|
||||
@@ -249,7 +269,8 @@ pub async fn library_clear_thumbnails_cache() -> Result<u64, String> {
|
||||
pub async fn library_get_folders(state: State<'_, LibraryState>) -> Result<Vec<String>, String> {
|
||||
log::info!("Command: library_get_folders");
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.get_folders().map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
@@ -261,7 +282,8 @@ pub async fn library_get_folders_with_metadata(
|
||||
) -> Result<Vec<LibraryFolder>, String> {
|
||||
log::info!("Command: library_get_folders_with_metadata");
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
let mut folders = db.get_folders_with_metadata().map_err(|e| e.to_string())?;
|
||||
|
||||
// Refresh network detection for folders without user override
|
||||
@@ -316,7 +338,8 @@ pub async fn library_get_folder(
|
||||
) -> Result<Option<LibraryFolder>, String> {
|
||||
log::info!("Command: library_get_folder {}", id);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.get_folder_by_id(id).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
@@ -333,7 +356,8 @@ pub async fn library_update_folder_settings(
|
||||
) -> Result<LibraryFolder, String> {
|
||||
log::info!("Command: library_update_folder_settings {} alias={:?} enabled={}", id, alias, enabled);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.update_folder_settings(
|
||||
id,
|
||||
alias.as_deref(),
|
||||
@@ -357,7 +381,8 @@ pub async fn library_set_folder_enabled(
|
||||
) -> Result<(), String> {
|
||||
log::info!("Command: library_set_folder_enabled {} enabled={}", id, enabled);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.set_folder_enabled(id, enabled).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
@@ -379,7 +404,8 @@ pub async fn library_update_folder_path(
|
||||
return Err("The selected path is not a folder".to_string());
|
||||
}
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.update_folder_path(id, &new_path).map_err(|e| e.to_string())?;
|
||||
|
||||
// Check if it's a network folder and update network info
|
||||
@@ -453,7 +479,8 @@ pub async fn library_scan(state: State<'_, LibraryState>) -> Result<(), String>
|
||||
|
||||
// Get folders to scan
|
||||
let folders = {
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.get_folders().map_err(|e| e.to_string())?
|
||||
};
|
||||
|
||||
@@ -594,7 +621,8 @@ pub async fn library_scan(state: State<'_, LibraryState>) -> Result<(), String>
|
||||
}
|
||||
track.artwork_path = artwork_path;
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
if let Err(e) = db.insert_track(&track) {
|
||||
all_errors.push(ScanError {
|
||||
file_path: path_str,
|
||||
@@ -691,7 +719,8 @@ async fn process_cue_file(cue_path: &Path, state: &State<'_, LibraryState>) -> R
|
||||
}
|
||||
|
||||
// Insert tracks
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
let group_key = tracks
|
||||
.first()
|
||||
.map(|track| track.album_group_key.clone())
|
||||
@@ -786,7 +815,8 @@ pub async fn library_scan_folder(
|
||||
|
||||
// Get folder info
|
||||
let folder = {
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.get_folder_by_id(folder_id)
|
||||
.map_err(|e| e.to_string())?
|
||||
.ok_or_else(|| format!("Folder with ID {} not found", folder_id))?
|
||||
@@ -817,7 +847,8 @@ pub async fn library_scan_folder(
|
||||
}
|
||||
});
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
let _ = db.update_folder_settings(
|
||||
folder.id,
|
||||
folder.alias.as_deref(),
|
||||
@@ -954,7 +985,8 @@ pub async fn library_scan_folder(
|
||||
}
|
||||
track.artwork_path = artwork_path.clone();
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
let group_key = track.album_group_key.clone();
|
||||
if let Err(e) = db.insert_track(&track) {
|
||||
all_errors.push(ScanError {
|
||||
@@ -987,7 +1019,8 @@ pub async fn library_scan_folder(
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|d| d.as_secs() as i64)
|
||||
.unwrap_or(0);
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
let _ = db.update_folder_scan_time(&folder.path, now);
|
||||
}
|
||||
|
||||
@@ -1018,11 +1051,13 @@ pub async fn library_get_albums(
|
||||
let include_qobuz = download_settings_state
|
||||
.lock()
|
||||
.map_err(|e| format!("Failed to lock download settings: {}", e))?
|
||||
.get_settings()
|
||||
.as_ref()
|
||||
.and_then(|s| s.get_settings().ok())
|
||||
.map(|s| s.show_in_library)
|
||||
.unwrap_or(false);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
|
||||
// Use optimized SQL-based filtering instead of N+1 query pattern
|
||||
let albums = db.get_albums_with_full_filter(
|
||||
@@ -1042,7 +1077,8 @@ pub async fn library_get_album_tracks(
|
||||
) -> Result<Vec<LocalTrack>, String> {
|
||||
log::info!("Command: library_get_album_tracks {}", album_group_key);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.get_album_tracks(&album_group_key)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1059,11 +1095,13 @@ pub async fn library_get_artists(
|
||||
let include_qobuz = download_settings_state
|
||||
.lock()
|
||||
.map_err(|e| format!("Failed to lock download settings: {}", e))?
|
||||
.get_settings()
|
||||
.as_ref()
|
||||
.and_then(|s| s.get_settings().ok())
|
||||
.map(|s| s.show_in_library)
|
||||
.unwrap_or(false);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
|
||||
// Use optimized SQL-based filtering instead of N+1 query pattern
|
||||
let artists = db.get_artists_with_filter(
|
||||
@@ -1089,11 +1127,13 @@ pub async fn library_search(
|
||||
let include_qobuz = download_settings_state
|
||||
.lock()
|
||||
.map_err(|e| format!("Failed to lock download settings: {}", e))?
|
||||
.get_settings()
|
||||
.as_ref()
|
||||
.and_then(|s| s.get_settings().ok())
|
||||
.map(|s| s.show_in_library)
|
||||
.unwrap_or(false);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
|
||||
// Use optimized SQL-based filtering
|
||||
// limit = 0 means no limit (fetch all tracks)
|
||||
@@ -1112,7 +1152,8 @@ pub async fn library_search(
|
||||
pub async fn library_get_stats(state: State<'_, LibraryState>) -> Result<LibraryStats, String> {
|
||||
log::info!("Command: library_get_stats");
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.get_stats().map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
@@ -1120,7 +1161,8 @@ pub async fn library_get_stats(state: State<'_, LibraryState>) -> Result<Library
|
||||
pub async fn library_clear(state: State<'_, LibraryState>) -> Result<(), String> {
|
||||
log::info!("Command: library_clear");
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.clear_all_tracks().map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
@@ -1133,7 +1175,8 @@ pub async fn library_get_track(
|
||||
) -> Result<LocalTrack, String> {
|
||||
log::info!("Command: library_get_track {}", track_id);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.get_track(track_id)
|
||||
.map_err(|e| e.to_string())?
|
||||
.ok_or_else(|| "Track not found".to_string())
|
||||
@@ -1147,7 +1190,8 @@ pub async fn get_track_by_path(
|
||||
) -> Result<Option<LocalTrack>, String> {
|
||||
log::info!("Command: get_track_by_path {}", file_path);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.get_track_by_path(&file_path)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1163,7 +1207,8 @@ pub async fn library_play_track(
|
||||
|
||||
// Get track from database
|
||||
let track = {
|
||||
let db = library_state.db.lock().await;
|
||||
let guard__ = library_state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.get_track(track_id)
|
||||
.map_err(|e| e.to_string())?
|
||||
.ok_or_else(|| "Track not found".to_string())?
|
||||
@@ -1220,7 +1265,8 @@ pub async fn playlist_get_settings(
|
||||
) -> Result<Option<PlaylistSettings>, String> {
|
||||
log::debug!("Command: playlist_get_settings {}", playlist_id);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.get_playlist_settings(playlist_id)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1233,7 +1279,8 @@ pub async fn playlist_save_settings(
|
||||
) -> Result<(), String> {
|
||||
log::info!("Command: playlist_save_settings {}", settings.qobuz_playlist_id);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.save_playlist_settings(&settings)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1248,7 +1295,8 @@ pub async fn playlist_set_sort(
|
||||
) -> Result<(), String> {
|
||||
log::info!("Command: playlist_set_sort {} {} {}", playlist_id, sort_by, sort_order);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.update_playlist_sort(playlist_id, &sort_by, &sort_order)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1287,7 +1335,8 @@ pub async fn playlist_set_artwork(
|
||||
None
|
||||
};
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.update_playlist_artwork(playlist_id, final_path.as_deref())
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1302,7 +1351,8 @@ pub async fn playlist_add_local_track(
|
||||
) -> Result<(), String> {
|
||||
log::info!("Command: playlist_add_local_track {} track {}", playlist_id, local_track_id);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.add_local_track_to_playlist(playlist_id, local_track_id, position)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1316,7 +1366,8 @@ pub async fn playlist_remove_local_track(
|
||||
) -> Result<(), String> {
|
||||
log::info!("Command: playlist_remove_local_track {} track {}", playlist_id, local_track_id);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.remove_local_track_from_playlist(playlist_id, local_track_id)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1329,7 +1380,8 @@ pub async fn playlist_get_local_tracks(
|
||||
) -> Result<Vec<LocalTrack>, String> {
|
||||
log::info!("Command: playlist_get_local_tracks {}", playlist_id);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.get_playlist_local_tracks(playlist_id)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1342,7 +1394,8 @@ pub async fn playlist_get_local_tracks_with_position(
|
||||
) -> Result<Vec<crate::library::PlaylistLocalTrack>, String> {
|
||||
log::debug!("Command: playlist_get_local_tracks_with_position {}", playlist_id);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.get_playlist_local_tracks_with_position(playlist_id)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1354,7 +1407,8 @@ pub async fn playlist_get_all_local_track_counts(
|
||||
) -> Result<std::collections::HashMap<u64, u32>, String> {
|
||||
log::debug!("Command: playlist_get_all_local_track_counts");
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.get_all_playlist_local_track_counts()
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1367,7 +1421,8 @@ pub async fn playlist_clear_local_tracks(
|
||||
) -> Result<(), String> {
|
||||
log::info!("Command: playlist_clear_local_tracks {}", playlist_id);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.clear_playlist_local_tracks(playlist_id)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1379,7 +1434,8 @@ pub async fn playlist_get_all_settings(
|
||||
) -> Result<Vec<PlaylistSettings>, String> {
|
||||
log::debug!("Command: playlist_get_all_settings");
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.get_all_playlist_settings()
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1393,7 +1449,8 @@ pub async fn playlist_set_hidden(
|
||||
) -> Result<(), String> {
|
||||
log::info!("Command: playlist_set_hidden {} {}", playlist_id, hidden);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.set_playlist_hidden(playlist_id, hidden)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1407,7 +1464,8 @@ pub async fn playlist_set_favorite(
|
||||
) -> Result<(), String> {
|
||||
log::info!("Command: playlist_set_favorite {} {}", playlist_id, favorite);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.set_playlist_favorite(playlist_id, favorite)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1419,7 +1477,8 @@ pub async fn playlist_get_favorites(
|
||||
) -> Result<Vec<u64>, String> {
|
||||
log::info!("Command: playlist_get_favorites");
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.get_favorite_playlist_ids()
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1433,7 +1492,8 @@ pub async fn playlist_set_position(
|
||||
) -> Result<(), String> {
|
||||
log::info!("Command: playlist_set_position {} {}", playlist_id, position);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.set_playlist_position(playlist_id, position)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1446,7 +1506,8 @@ pub async fn playlist_reorder(
|
||||
) -> Result<(), String> {
|
||||
log::info!("Command: playlist_reorder {:?}", playlist_ids);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.reorder_playlists(&playlist_ids)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1459,7 +1520,8 @@ pub async fn playlist_get_stats(
|
||||
) -> Result<Option<PlaylistStats>, String> {
|
||||
log::debug!("Command: playlist_get_stats {}", playlist_id);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.get_playlist_stats(playlist_id)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1471,7 +1533,8 @@ pub async fn playlist_get_all_stats(
|
||||
) -> Result<Vec<PlaylistStats>, String> {
|
||||
log::debug!("Command: playlist_get_all_stats");
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.get_all_playlist_stats()
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1484,7 +1547,8 @@ pub async fn playlist_increment_play_count(
|
||||
) -> Result<PlaylistStats, String> {
|
||||
log::info!("Command: playlist_increment_play_count {}", playlist_id);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.increment_playlist_play_count(playlist_id)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1500,7 +1564,8 @@ pub async fn playlist_get_custom_order(
|
||||
) -> Result<Vec<(i64, bool, i32)>, String> {
|
||||
log::info!("Command: playlist_get_custom_order {}", playlist_id);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.get_playlist_custom_order(playlist_id)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1514,7 +1579,8 @@ pub async fn playlist_init_custom_order(
|
||||
) -> Result<(), String> {
|
||||
log::info!("Command: playlist_init_custom_order {} ({} tracks)", playlist_id, track_ids.len());
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.init_playlist_custom_order(playlist_id, &track_ids)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1528,7 +1594,8 @@ pub async fn playlist_set_custom_order(
|
||||
) -> Result<(), String> {
|
||||
log::info!("Command: playlist_set_custom_order {} ({} tracks)", playlist_id, orders.len());
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.set_playlist_custom_order(playlist_id, &orders)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1544,7 +1611,8 @@ pub async fn playlist_move_track(
|
||||
) -> Result<(), String> {
|
||||
log::info!("Command: playlist_move_track {} track {} -> pos {}", playlist_id, track_id, new_position);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.move_playlist_track(playlist_id, track_id, is_local, new_position)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1557,7 +1625,8 @@ pub async fn playlist_has_custom_order(
|
||||
) -> Result<bool, String> {
|
||||
log::info!("Command: playlist_has_custom_order {}", playlist_id);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.has_playlist_custom_order(playlist_id)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1570,7 +1639,8 @@ pub async fn playlist_clear_custom_order(
|
||||
) -> Result<(), String> {
|
||||
log::info!("Command: playlist_clear_custom_order {}", playlist_id);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.clear_playlist_custom_order(playlist_id)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1600,7 +1670,8 @@ pub async fn library_fetch_missing_artwork(
|
||||
|
||||
// Get all albums without artwork
|
||||
let albums_without_artwork: Vec<(String, String, String)> = {
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.get_albums_without_artwork()
|
||||
.map_err(|e| e.to_string())?
|
||||
};
|
||||
@@ -1611,7 +1682,8 @@ pub async fn library_fetch_missing_artwork(
|
||||
// Try to fetch from Discogs
|
||||
if let Some(artwork_path) = discogs.fetch_artwork(&artist, &album, &artwork_cache).await {
|
||||
// Update all tracks in this album with the artwork
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
if db.update_album_group_artwork(&group_key, &artwork_path).is_ok() {
|
||||
updated_count += 1;
|
||||
log::info!("Updated artwork for {} - {}", artist, album);
|
||||
@@ -1641,7 +1713,8 @@ pub async fn library_fetch_album_artwork(
|
||||
let artwork_cache = get_artwork_cache_dir();
|
||||
|
||||
if let Some(artwork_path) = discogs.fetch_artwork(&artist, &album, &artwork_cache).await {
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
if let Some(group_key) = db
|
||||
.find_album_group_key(&album, &artist)
|
||||
.map_err(|e| e.to_string())?
|
||||
@@ -1683,7 +1756,8 @@ pub async fn library_set_album_artwork(
|
||||
let cached_path = MetadataExtractor::cache_artwork_file(source_path, &artwork_cache)
|
||||
.ok_or_else(|| "Failed to cache artwork file".to_string())?;
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.update_album_group_artwork(&album_group_key, &cached_path)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
@@ -1712,7 +1786,8 @@ pub async fn library_get_album_settings(
|
||||
) -> Result<Option<crate::library::AlbumSettings>, String> {
|
||||
log::info!("Command: library_get_album_settings {}", album_group_key);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.get_album_settings(&album_group_key)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1725,7 +1800,8 @@ pub async fn library_set_album_hidden(
|
||||
) -> Result<(), String> {
|
||||
log::info!("Command: library_set_album_hidden {} = {}", album_group_key, hidden);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.set_album_hidden(&album_group_key, hidden)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -1819,7 +1895,8 @@ pub async fn library_update_album_metadata(
|
||||
.map_err(|e| format!("Failed to write sidecar: {}", e))?;
|
||||
sidecar_result?;
|
||||
|
||||
let mut db = state.db.lock().await;
|
||||
let mut guard__ = state.db.lock().await;
|
||||
let db = guard__.as_mut().ok_or("No active session - please log in")?;
|
||||
let existing_tracks = db
|
||||
.get_album_tracks(&request.album_group_key)
|
||||
.map_err(|e| e.to_string())?;
|
||||
@@ -1872,7 +1949,8 @@ pub async fn library_write_album_metadata_to_files(
|
||||
return Err("Album track list is empty.".to_string());
|
||||
}
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
let existing_tracks = db
|
||||
.get_album_tracks(&request.album_group_key)
|
||||
.map_err(|e| e.to_string())?;
|
||||
@@ -1882,7 +1960,7 @@ pub async fn library_write_album_metadata_to_files(
|
||||
{
|
||||
return Err("Writing tags to files is not supported for CUE-based albums. Use sidecar mode instead.".to_string());
|
||||
}
|
||||
drop(db);
|
||||
drop(guard__);
|
||||
|
||||
let album_dir = PathBuf::from(request.album_group_key.trim());
|
||||
if !album_dir.is_dir() {
|
||||
@@ -2001,7 +2079,8 @@ pub async fn library_write_album_metadata_to_files(
|
||||
.await;
|
||||
|
||||
// Update DB from the requested values.
|
||||
let mut db = state.db.lock().await;
|
||||
let mut guard__ = state.db.lock().await;
|
||||
let db = guard__.as_mut().ok_or("No active session - please log in")?;
|
||||
let existing_tracks = db
|
||||
.get_album_tracks(&request.album_group_key)
|
||||
.map_err(|e| e.to_string())?;
|
||||
@@ -2046,7 +2125,8 @@ pub async fn library_refresh_album_metadata_from_files(
|
||||
return Err("Album ID is required.".to_string());
|
||||
}
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
let existing_tracks = db.get_album_tracks(&album_group_key).map_err(|e| e.to_string())?;
|
||||
if existing_tracks.is_empty() {
|
||||
return Err("Album not found.".to_string());
|
||||
@@ -2059,7 +2139,7 @@ pub async fn library_refresh_album_metadata_from_files(
|
||||
"Refreshing metadata from files is not supported for CUE-based albums.".to_string(),
|
||||
);
|
||||
}
|
||||
drop(db);
|
||||
drop(guard__);
|
||||
|
||||
let album_dir = PathBuf::from(album_group_key.trim());
|
||||
if !album_dir.is_dir() {
|
||||
@@ -2111,7 +2191,8 @@ pub async fn library_refresh_album_metadata_from_files(
|
||||
.map_err(|e| format!("Failed to refresh metadata: {}", e))?;
|
||||
let updates = refresh?;
|
||||
|
||||
let mut db = state.db.lock().await;
|
||||
let mut guard__ = state.db.lock().await;
|
||||
let db = guard__.as_mut().ok_or("No active session - please log in")?;
|
||||
db.update_tracks_metadata_by_id(&updates)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
@@ -2124,7 +2205,8 @@ pub async fn library_get_hidden_albums(
|
||||
) -> Result<Vec<String>, String> {
|
||||
log::info!("Command: library_get_hidden_albums");
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.get_hidden_albums()
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -2157,7 +2239,8 @@ pub async fn library_backfill_downloads(
|
||||
|
||||
// Get all ready cached tracks directly from offline cache DB
|
||||
let cached_tracks = {
|
||||
let cache_db = offline_cache_state.db.lock().await;
|
||||
let cache_db_opt__ = offline_cache_state.db.lock().await;
|
||||
let cache_db = cache_db_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
|
||||
let mut stmt = cache_db
|
||||
.conn()
|
||||
@@ -2185,7 +2268,8 @@ pub async fn library_backfill_downloads(
|
||||
|
||||
report.total_downloads = cached_tracks.len();
|
||||
|
||||
let library_db = state.db.lock().await;
|
||||
let library_db_opt__ = state.db.lock().await;
|
||||
let library_db = library_db_opt__.as_ref().ok_or("No active session - please log in")?;
|
||||
|
||||
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
|
||||
@@ -2280,7 +2364,8 @@ pub async fn library_get_artist_image(
|
||||
artist_name: String,
|
||||
state: State<'_, LibraryState>,
|
||||
) -> Result<Option<ArtistImageInfo>, String> {
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.get_artist_image(&artist_name).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
@@ -2290,7 +2375,8 @@ pub async fn library_get_artist_images(
|
||||
artist_names: Vec<String>,
|
||||
state: State<'_, LibraryState>,
|
||||
) -> Result<Vec<ArtistImageInfo>, String> {
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
let mut results = Vec::new();
|
||||
for name in artist_names {
|
||||
if let Ok(Some(info)) = db.get_artist_image(&name) {
|
||||
@@ -2305,7 +2391,8 @@ pub async fn library_get_artist_images(
|
||||
pub async fn library_get_canonical_names(
|
||||
state: State<'_, LibraryState>,
|
||||
) -> Result<std::collections::HashMap<String, String>, String> {
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.get_all_canonical_names().map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
@@ -2318,7 +2405,8 @@ pub async fn library_cache_artist_image(
|
||||
canonical_name: Option<String>,
|
||||
state: State<'_, LibraryState>,
|
||||
) -> Result<(), String> {
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.cache_artist_image_with_canonical(
|
||||
&artist_name,
|
||||
Some(&image_url),
|
||||
@@ -2363,7 +2451,8 @@ pub async fn library_set_custom_artist_image(
|
||||
|
||||
log::info!("Copied artist artwork for '{}' to: {}", artist_name, dest_path.display());
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.cache_artist_image(&artist_name, None, "custom", Some(&dest_path.to_string_lossy()))
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -2399,7 +2488,8 @@ pub async fn playlist_analyze_local_content(
|
||||
) -> Result<PlaylistAnalysisResult, String> {
|
||||
log::info!("Command: playlist_analyze_local_content for playlist {}", playlist_id);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
let total_tracks = tracks.len() as u32;
|
||||
let mut local_count = 0u32;
|
||||
|
||||
@@ -2453,7 +2543,8 @@ pub async fn playlist_get_local_content_status(
|
||||
) -> Result<crate::library::database::LocalContentStatus, String> {
|
||||
log::info!("Command: playlist_get_local_content_status for playlist {}", playlist_id);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
let settings = db.get_playlist_settings(playlist_id)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
@@ -2471,7 +2562,8 @@ pub async fn playlist_track_is_local(
|
||||
album: String,
|
||||
state: State<'_, LibraryState>,
|
||||
) -> Result<bool, String> {
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
|
||||
// First try Qobuz track ID
|
||||
let has_by_id = db.has_local_track_by_qobuz_id(qobuz_track_id)
|
||||
@@ -2495,7 +2587,8 @@ pub async fn playlist_get_local_track_id(
|
||||
album: String,
|
||||
state: State<'_, LibraryState>,
|
||||
) -> Result<Option<i64>, String> {
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
|
||||
// First try Qobuz track ID
|
||||
if let Some(id) = db.get_local_track_id_by_qobuz_id(qobuz_track_id)
|
||||
@@ -2515,7 +2608,8 @@ pub async fn playlist_get_tracks_with_local_copies(
|
||||
track_ids: Vec<u64>,
|
||||
state: State<'_, LibraryState>,
|
||||
) -> Result<Vec<u64>, String> {
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
|
||||
let local_ids = db.get_tracks_with_local_copies(&track_ids)
|
||||
.map_err(|e| e.to_string())?;
|
||||
@@ -2531,7 +2625,8 @@ pub async fn playlist_get_offline_available(
|
||||
) -> Result<Vec<u64>, String> {
|
||||
log::info!("Command: playlist_get_offline_available (include_partial: {})", include_partial);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
let playlists = db.get_playlists_by_local_content(include_partial)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
@@ -2583,7 +2678,8 @@ pub async fn library_get_tracks_by_ids(
|
||||
) -> Result<Vec<LocalTrack>, String> {
|
||||
log::info!("Command: library_get_tracks_by_ids ({} tracks)", track_ids.len());
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
let mut tracks = Vec::new();
|
||||
|
||||
for track_id in track_ids {
|
||||
@@ -2642,7 +2738,8 @@ pub async fn create_playlist_folder(
|
||||
) -> Result<PlaylistFolder, String> {
|
||||
log::info!("Command: create_playlist_folder {}", name);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.create_playlist_folder(
|
||||
&name,
|
||||
icon_type.as_deref(),
|
||||
@@ -2659,7 +2756,8 @@ pub async fn get_playlist_folders(
|
||||
) -> Result<Vec<PlaylistFolder>, String> {
|
||||
log::debug!("Command: get_playlist_folders");
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.get_all_playlist_folders().map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
@@ -2707,7 +2805,8 @@ pub async fn update_playlist_folder(
|
||||
None
|
||||
};
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.update_playlist_folder(
|
||||
&id,
|
||||
name.as_deref(),
|
||||
@@ -2728,7 +2827,8 @@ pub async fn delete_playlist_folder(
|
||||
) -> Result<(), String> {
|
||||
log::info!("Command: delete_playlist_folder {}", id);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.delete_playlist_folder(&id).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
@@ -2740,7 +2840,8 @@ pub async fn reorder_playlist_folders(
|
||||
) -> Result<(), String> {
|
||||
log::info!("Command: reorder_playlist_folders ({} folders)", folder_ids.len());
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.reorder_playlist_folders(&folder_ids).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
@@ -2757,7 +2858,8 @@ pub async fn move_playlist_to_folder(
|
||||
folder_id
|
||||
);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let guard__ = state.db.lock().await;
|
||||
let db = guard__.as_ref().ok_or("No active session - please log in")?;
|
||||
db.move_playlist_to_folder(playlist_id, folder_id.as_deref())
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
Reference in new issue
Block a user