feat(downloads): smart library integration with visibility toggle
Implement intelligent library insertion: - Downloads ALWAYS inserted to library DB (persistence) - Visibility controlled by toggle in Settings - Downloads NOT added to scan folders (separate from local files) Backend changes: 1. Simplified post_process_track to always insert 2. Created get_albums_with_filter() method 3. Added source filter: exclude 'qobuz_download' when toggle off 4. Wire download_settings_state to library_get_albums command 5. Read show_in_library setting and pass to DB query Database filtering: - When toggle ON: show all albums (local + downloads) - When toggle OFF: filter out source='qobuz_download' - Hidden albums filter still works independently Design benefits: - Downloads folder never scanned by Local Library - Tracks always in DB for potential future features - Clean separation: local files vs Qobuz downloads - Toggle only affects UI visibility, not data persistence cp
This commit is contained in:
1 parent
49da0832b9
commit
01be29133a
3 files changed
+34
-21
No files matched your search
@@ -20,10 +20,9 @@ async fn post_process_track(
|
|||||||
current_path: &str,
|
current_path: &str,
|
||||||
download_root: &str,
|
download_root: &str,
|
||||||
qobuz_client: &crate::api::QobuzClient,
|
qobuz_client: &crate::api::QobuzClient,
|
||||||
show_in_library: bool,
|
library_db: Arc<tokio::sync::Mutex<crate::library::database::LibraryDatabase>>,
|
||||||
library_db: Option<Arc<tokio::sync::Mutex<crate::library::database::LibraryDatabase>>>,
|
|
||||||
) -> Result<String, String> {
|
) -> Result<String, String> {
|
||||||
log::info!("Post-processing track {} (show_in_library: {})", track_id, show_in_library);
|
log::info!("Post-processing track {}", track_id);
|
||||||
|
|
||||||
// 1. Fetch complete metadata from Qobuz
|
// 1. Fetch complete metadata from Qobuz
|
||||||
let metadata = fetch_complete_metadata(track_id, qobuz_client).await?;
|
let metadata = fetch_complete_metadata(track_id, qobuz_client).await?;
|
||||||
@@ -42,10 +41,8 @@ async fn post_process_track(
|
|||||||
// 4. Organize file into artist/album structure
|
// 4. Organize file into artist/album structure
|
||||||
let new_path = organize_download(track_id, current_path, download_root, &metadata)?;
|
let new_path = organize_download(track_id, current_path, download_root, &metadata)?;
|
||||||
|
|
||||||
// 5. Insert into local library if enabled
|
// 5. ALWAYS insert into local library DB (visibility controlled by toggle)
|
||||||
if show_in_library {
|
let lib_guard = library_db.lock().await;
|
||||||
if let Some(lib_db) = library_db {
|
|
||||||
let lib_guard = lib_db.lock().await;
|
|
||||||
match lib_guard.insert_qobuz_download_direct(
|
match lib_guard.insert_qobuz_download_direct(
|
||||||
track_id,
|
track_id,
|
||||||
&metadata.title,
|
&metadata.title,
|
||||||
@@ -56,10 +53,8 @@ async fn post_process_track(
|
|||||||
None, // bit_depth not in CompleteTrackMetadata
|
None, // bit_depth not in CompleteTrackMetadata
|
||||||
None, // sample_rate not in CompleteTrackMetadata
|
None, // sample_rate not in CompleteTrackMetadata
|
||||||
) {
|
) {
|
||||||
Ok(_) => log::info!("Track {} added to local library", track_id),
|
Ok(_) => log::info!("Track {} inserted to local library DB", track_id),
|
||||||
Err(e) => log::error!("Failed to add track {} to library: {}", track_id, e),
|
Err(e) => log::error!("Failed to insert track {} to library DB: {}", track_id, e),
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log::info!("Track {} organized to: {}", track_id, new_path);
|
log::info!("Track {} organized to: {}", track_id, new_path);
|
||||||
@@ -192,9 +187,6 @@ pub async fn download_track(
|
|||||||
// Post-processing: metadata, tagging, artwork, organization
|
// Post-processing: metadata, tagging, artwork, organization
|
||||||
log::info!("Starting post-processing for track {}", track_id);
|
log::info!("Starting post-processing for track {}", track_id);
|
||||||
|
|
||||||
// Check if we should add to library (hardcoded to false for now - will be from settings)
|
|
||||||
let show_in_library = false; // TODO: Get from settings
|
|
||||||
|
|
||||||
let file_path_str = file_path.to_string_lossy().to_string();
|
let file_path_str = file_path.to_string_lossy().to_string();
|
||||||
let qobuz_client = client.lock().await;
|
let qobuz_client = client.lock().await;
|
||||||
match post_process_track(
|
match post_process_track(
|
||||||
@@ -202,8 +194,7 @@ pub async fn download_track(
|
|||||||
&file_path_str,
|
&file_path_str,
|
||||||
&download_root,
|
&download_root,
|
||||||
&*qobuz_client,
|
&*qobuz_client,
|
||||||
show_in_library,
|
library_db.clone(),
|
||||||
Some(library_db.clone()),
|
|
||||||
).await {
|
).await {
|
||||||
Ok(new_path) => {
|
Ok(new_path) => {
|
||||||
// Update database with new path
|
// Update database with new path
|
||||||
|
|||||||
@@ -339,16 +339,15 @@ pub async fn library_get_albums(
|
|||||||
log::info!("Command: library_get_albums");
|
log::info!("Command: library_get_albums");
|
||||||
|
|
||||||
// Get download settings to check if we should include Qobuz downloads
|
// Get download settings to check if we should include Qobuz downloads
|
||||||
let _include_qobuz = download_settings_state
|
let include_qobuz = download_settings_state
|
||||||
.lock()
|
.lock()
|
||||||
.map_err(|e| format!("Failed to lock download settings: {}", e))?
|
.map_err(|e| format!("Failed to lock download settings: {}", e))?
|
||||||
.get_settings()
|
.get_settings()
|
||||||
.map(|s| s.show_in_library)
|
.map(|s| s.show_in_library)
|
||||||
.unwrap_or(false);
|
.unwrap_or(false);
|
||||||
|
|
||||||
// TODO: Use include_qobuz parameter once database method supports filtering
|
|
||||||
let db = state.db.lock().await;
|
let db = state.db.lock().await;
|
||||||
db.get_albums(include_hidden.unwrap_or(false))
|
db.get_albums_with_filter(include_hidden.unwrap_or(false), include_qobuz)
|
||||||
.map_err(|e| e.to_string())
|
.map_err(|e| e.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -405,7 +405,23 @@ impl LibraryDatabase {
|
|||||||
|
|
||||||
/// Get all albums with optional hidden filter
|
/// Get all albums with optional hidden filter
|
||||||
pub fn get_albums(&self, include_hidden: bool) -> Result<Vec<LocalAlbum>, LibraryError> {
|
pub fn get_albums(&self, include_hidden: bool) -> Result<Vec<LocalAlbum>, LibraryError> {
|
||||||
|
self.get_albums_with_filter(include_hidden, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get all albums with optional filters for hidden and Qobuz downloads
|
||||||
|
pub fn get_albums_with_filter(
|
||||||
|
&self,
|
||||||
|
include_hidden: bool,
|
||||||
|
include_qobuz_downloads: bool,
|
||||||
|
) -> Result<Vec<LocalAlbum>, LibraryError> {
|
||||||
|
let source_filter = if include_qobuz_downloads {
|
||||||
|
""
|
||||||
|
} else {
|
||||||
|
"AND (source IS NULL OR source != 'qobuz_download')"
|
||||||
|
};
|
||||||
|
|
||||||
let query = if include_hidden {
|
let query = if include_hidden {
|
||||||
|
format!(
|
||||||
r#"
|
r#"
|
||||||
SELECT
|
SELECT
|
||||||
group_key,
|
group_key,
|
||||||
@@ -434,11 +450,15 @@ impl LibraryDatabase {
|
|||||||
bit_depth,
|
bit_depth,
|
||||||
sample_rate
|
sample_rate
|
||||||
FROM local_tracks
|
FROM local_tracks
|
||||||
|
WHERE 1=1 {}
|
||||||
)
|
)
|
||||||
GROUP BY group_key
|
GROUP BY group_key
|
||||||
ORDER BY artist, title
|
ORDER BY artist, title
|
||||||
"#
|
"#,
|
||||||
|
source_filter
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
|
format!(
|
||||||
r#"
|
r#"
|
||||||
SELECT
|
SELECT
|
||||||
group_key,
|
group_key,
|
||||||
@@ -467,13 +487,16 @@ impl LibraryDatabase {
|
|||||||
bit_depth,
|
bit_depth,
|
||||||
sample_rate
|
sample_rate
|
||||||
FROM local_tracks
|
FROM local_tracks
|
||||||
|
WHERE 1=1 {}
|
||||||
)
|
)
|
||||||
WHERE group_key NOT IN (
|
WHERE group_key NOT IN (
|
||||||
SELECT album_group_key FROM album_settings WHERE hidden = 1
|
SELECT album_group_key FROM album_settings WHERE hidden = 1
|
||||||
)
|
)
|
||||||
GROUP BY group_key
|
GROUP BY group_key
|
||||||
ORDER BY artist, title
|
ORDER BY artist, title
|
||||||
"#
|
"#,
|
||||||
|
source_filter
|
||||||
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut stmt = self
|
let mut stmt = self
|
||||||
|
|||||||
Reference in new issue
Block a user