feat(library): add LocalLibrary tag editor backend
cx
This commit is contained in:
1 parent
302fd200ae
commit
5d812f4966
5 files changed
+792
-16
No files matched your search
@@ -1,9 +1,11 @@
|
||||
//! Tauri commands for local library
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::Arc;
|
||||
use serde::Deserialize;
|
||||
use tauri::State;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
@@ -69,7 +71,7 @@ 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 mut db = state.db.lock().await;
|
||||
let id = db.add_folder_with_network_info(&path, is_network, fs_type.as_deref())
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
@@ -86,7 +88,7 @@ pub async fn library_remove_folder(
|
||||
) -> Result<(), String> {
|
||||
log::info!("Command: library_remove_folder {}", path);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let mut db = state.db.lock().await;
|
||||
db.remove_folder(&path).map_err(|e| e.to_string())?;
|
||||
db.delete_tracks_in_folder(&path).map_err(|e| e.to_string())?;
|
||||
Ok(())
|
||||
@@ -96,7 +98,7 @@ pub async fn library_remove_folder(
|
||||
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 mut db = state.db.lock().await;
|
||||
db.get_folders().map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
@@ -108,7 +110,7 @@ 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 mut db = state.db.lock().await;
|
||||
let mut folders = db.get_folders_with_metadata().map_err(|e| e.to_string())?;
|
||||
|
||||
// Refresh network detection for folders without user override
|
||||
@@ -163,7 +165,7 @@ pub async fn library_get_folder(
|
||||
) -> Result<Option<LibraryFolder>, String> {
|
||||
log::info!("Command: library_get_folder {}", id);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let mut db = state.db.lock().await;
|
||||
db.get_folder_by_id(id).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
@@ -180,7 +182,7 @@ 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 mut db = state.db.lock().await;
|
||||
db.update_folder_settings(
|
||||
id,
|
||||
alias.as_deref(),
|
||||
@@ -204,7 +206,7 @@ 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 mut db = state.db.lock().await;
|
||||
db.set_folder_enabled(id, enabled).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
@@ -226,7 +228,7 @@ 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 mut db = state.db.lock().await;
|
||||
db.update_folder_path(id, &new_path).map_err(|e| e.to_string())?;
|
||||
|
||||
// Check if it's a network folder and update network info
|
||||
@@ -323,6 +325,7 @@ pub async fn library_scan(state: State<'_, LibraryState>) -> Result<(), String>
|
||||
|
||||
let scanner = LibraryScanner::new();
|
||||
let mut all_errors: Vec<ScanError> = Vec::new();
|
||||
let mut sidecar_cache: HashMap<String, Option<crate::library::AlbumTagSidecar>> = HashMap::new();
|
||||
|
||||
for folder in &folders {
|
||||
log::info!("Scanning folder: {}", folder);
|
||||
@@ -418,6 +421,7 @@ pub async fn library_scan(state: State<'_, LibraryState>) -> Result<(), String>
|
||||
|
||||
match MetadataExtractor::extract(&canonical_path) {
|
||||
Ok(mut track) => {
|
||||
apply_sidecar_override_if_present(&mut track, &mut sidecar_cache);
|
||||
// Try to extract embedded artwork, fallback to cached folder artwork
|
||||
let artwork_cache = get_artwork_cache_dir();
|
||||
let mut artwork_path =
|
||||
@@ -500,6 +504,22 @@ async fn process_cue_file(cue_path: &Path, state: &State<'_, LibraryState>) -> R
|
||||
// Convert CUE to tracks
|
||||
let mut tracks = cue_to_tracks(&cue, properties.duration_secs, format, &properties);
|
||||
|
||||
// Apply sidecar overrides if present (matches by file_path + cue_start_secs)
|
||||
if let Some(group_key) = tracks
|
||||
.first()
|
||||
.map(|track| track.album_group_key.trim().to_string())
|
||||
.filter(|k| !k.is_empty())
|
||||
{
|
||||
let album_dir = Path::new(&group_key);
|
||||
if album_dir.is_dir() {
|
||||
if let Ok(Some(sidecar)) = crate::library::read_album_sidecar(album_dir) {
|
||||
for track in tracks.iter_mut() {
|
||||
crate::library::apply_sidecar_to_track(track, &sidecar);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let artwork_cache = get_artwork_cache_dir();
|
||||
let mut artwork_path = MetadataExtractor::extract_artwork(audio_path.as_path(), &artwork_cache);
|
||||
if artwork_path.is_none() {
|
||||
@@ -520,7 +540,7 @@ async fn process_cue_file(cue_path: &Path, state: &State<'_, LibraryState>) -> R
|
||||
}
|
||||
|
||||
// Insert tracks
|
||||
let db = state.db.lock().await;
|
||||
let mut db = state.db.lock().await;
|
||||
let group_key = tracks
|
||||
.first()
|
||||
.map(|track| track.album_group_key.clone())
|
||||
@@ -537,6 +557,59 @@ async fn process_cue_file(cue_path: &Path, state: &State<'_, LibraryState>) -> R
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn apply_sidecar_override_if_present(
|
||||
track: &mut LocalTrack,
|
||||
cache: &mut HashMap<String, Option<crate::library::AlbumTagSidecar>>,
|
||||
) {
|
||||
let group_key = track.album_group_key.trim();
|
||||
if group_key.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
let cached = cache.entry(group_key.to_string()).or_insert_with(|| {
|
||||
let album_dir = Path::new(group_key);
|
||||
if !album_dir.is_dir() {
|
||||
return None;
|
||||
}
|
||||
|
||||
match crate::library::read_album_sidecar(album_dir) {
|
||||
Ok(sidecar) => sidecar,
|
||||
Err(err) => {
|
||||
log::warn!(
|
||||
"Failed to read LocalLibrary sidecar for {}: {}",
|
||||
album_dir.display(),
|
||||
err
|
||||
);
|
||||
None
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if let Some(sidecar) = cached.as_ref() {
|
||||
crate::library::apply_sidecar_to_track(track, sidecar);
|
||||
}
|
||||
}
|
||||
|
||||
fn compute_track_artist_match(tracks: &[LocalTrack]) -> Option<String> {
|
||||
let mut artists: HashSet<String> = HashSet::new();
|
||||
for track in tracks {
|
||||
let value = track
|
||||
.album_artist
|
||||
.as_deref()
|
||||
.unwrap_or(track.artist.as_str())
|
||||
.trim();
|
||||
if value.is_empty() {
|
||||
continue;
|
||||
}
|
||||
artists.insert(value.to_string());
|
||||
if artists.len() > 1 {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
artists.into_iter().next()
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn library_get_scan_progress(
|
||||
state: State<'_, LibraryState>,
|
||||
@@ -620,6 +693,7 @@ pub async fn library_scan_folder(
|
||||
|
||||
let scanner = LibraryScanner::new();
|
||||
let mut all_errors: Vec<ScanError> = Vec::new();
|
||||
let mut sidecar_cache: HashMap<String, Option<crate::library::AlbumTagSidecar>> = HashMap::new();
|
||||
|
||||
log::info!("Scanning single folder: {}", folder.path);
|
||||
|
||||
@@ -713,6 +787,7 @@ pub async fn library_scan_folder(
|
||||
|
||||
match MetadataExtractor::extract(&canonical_path) {
|
||||
Ok(mut track) => {
|
||||
apply_sidecar_override_if_present(&mut track, &mut sidecar_cache);
|
||||
let artwork_cache = get_artwork_cache_dir();
|
||||
let mut artwork_path = MetadataExtractor::extract_artwork(&canonical_path, &artwork_cache);
|
||||
if artwork_path.is_none() {
|
||||
@@ -796,7 +871,7 @@ pub async fn library_get_albums(
|
||||
.map(|s| s.show_in_library)
|
||||
.unwrap_or(false);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let mut db = state.db.lock().await;
|
||||
|
||||
// Use optimized SQL-based filtering instead of N+1 query pattern
|
||||
let albums = db.get_albums_with_full_filter(
|
||||
@@ -816,7 +891,7 @@ 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 mut db = state.db.lock().await;
|
||||
db.get_album_tracks(&album_group_key)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -837,7 +912,7 @@ pub async fn library_get_artists(
|
||||
.map(|s| s.show_in_library)
|
||||
.unwrap_or(false);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let mut db = state.db.lock().await;
|
||||
|
||||
// Use optimized SQL-based filtering instead of N+1 query pattern
|
||||
let artists = db.get_artists_with_filter(
|
||||
@@ -867,7 +942,7 @@ pub async fn library_search(
|
||||
.map(|s| s.show_in_library)
|
||||
.unwrap_or(false);
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let mut db = state.db.lock().await;
|
||||
|
||||
// Use optimized SQL-based filtering
|
||||
// limit = 0 means no limit (fetch all tracks)
|
||||
@@ -886,7 +961,7 @@ 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 mut db = state.db.lock().await;
|
||||
db.get_stats().map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
@@ -894,7 +969,7 @@ 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 mut db = state.db.lock().await;
|
||||
db.clear_all_tracks().map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
@@ -1418,6 +1493,384 @@ pub async fn library_set_album_hidden(
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct LibraryAlbumTrackMetadataUpdate {
|
||||
pub id: i64,
|
||||
pub file_path: String,
|
||||
pub cue_start_secs: Option<f64>,
|
||||
pub title: String,
|
||||
pub disc_number: Option<u32>,
|
||||
pub track_number: Option<u32>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct LibraryAlbumMetadataUpdateRequest {
|
||||
pub album_group_key: String,
|
||||
pub album_title: String,
|
||||
pub album_artist: String,
|
||||
pub year: Option<u32>,
|
||||
pub genre: Option<String>,
|
||||
pub catalog_number: Option<String>,
|
||||
pub tracks: Vec<LibraryAlbumTrackMetadataUpdate>,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn library_update_album_metadata(
|
||||
request: LibraryAlbumMetadataUpdateRequest,
|
||||
state: State<'_, LibraryState>,
|
||||
) -> Result<(), String> {
|
||||
log::info!(
|
||||
"Command: library_update_album_metadata {}",
|
||||
request.album_group_key
|
||||
);
|
||||
|
||||
if request.album_group_key.trim().is_empty() {
|
||||
return Err("Album ID is required.".to_string());
|
||||
}
|
||||
if request.album_title.trim().is_empty() {
|
||||
return Err("Album title is required.".to_string());
|
||||
}
|
||||
if request.tracks.is_empty() {
|
||||
return Err("Album track list is empty.".to_string());
|
||||
}
|
||||
|
||||
let album_dir = PathBuf::from(request.album_group_key.trim());
|
||||
if !album_dir.is_dir() {
|
||||
return Err("Album folder not found on disk.".to_string());
|
||||
}
|
||||
|
||||
// Write sidecar first (persistence), then update DB.
|
||||
let sidecar_result = tokio::task::spawn_blocking({
|
||||
let album_dir = album_dir.clone();
|
||||
let request = request.clone();
|
||||
move || -> Result<(), String> {
|
||||
let album = crate::library::AlbumMetadataOverride {
|
||||
album_title: Some(request.album_title.trim().to_string()),
|
||||
album_artist: Some(request.album_artist.trim().to_string())
|
||||
.filter(|s| !s.is_empty()),
|
||||
year: request.year,
|
||||
genre: request.genre.as_ref().map(|s| s.trim().to_string()).filter(|s| !s.is_empty()),
|
||||
catalog_number: request
|
||||
.catalog_number
|
||||
.as_ref()
|
||||
.map(|s| s.trim().to_string())
|
||||
.filter(|s| !s.is_empty()),
|
||||
};
|
||||
|
||||
let tracks = request
|
||||
.tracks
|
||||
.iter()
|
||||
.map(|t| crate::library::TrackMetadataOverride {
|
||||
file_path: t.file_path.clone(),
|
||||
cue_start_secs: t.cue_start_secs,
|
||||
title: Some(t.title.trim().to_string()),
|
||||
disc_number: t.disc_number,
|
||||
track_number: t.track_number,
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let sidecar = crate::library::AlbumTagSidecar::new(album, tracks);
|
||||
crate::library::write_album_sidecar(&album_dir, &sidecar)
|
||||
.map_err(|e| format!("Failed to write sidecar: {}", e))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
})
|
||||
.await
|
||||
.map_err(|e| format!("Failed to write sidecar: {}", e))?;
|
||||
sidecar_result?;
|
||||
|
||||
let mut db = state.db.lock().await;
|
||||
let existing_tracks = db
|
||||
.get_album_tracks(&request.album_group_key)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
let track_artist_match = compute_track_artist_match(&existing_tracks);
|
||||
let track_updates = request
|
||||
.tracks
|
||||
.iter()
|
||||
.map(|t| crate::library::AlbumTrackUpdate {
|
||||
id: t.id,
|
||||
title: t.title.clone(),
|
||||
disc_number: t.disc_number,
|
||||
track_number: t.track_number,
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
db.update_album_group_metadata(
|
||||
&request.album_group_key,
|
||||
&request.album_title,
|
||||
&request.album_artist,
|
||||
request.year,
|
||||
request.genre.as_deref(),
|
||||
request.catalog_number.as_deref(),
|
||||
track_artist_match.as_deref(),
|
||||
&track_updates,
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn library_write_album_metadata_to_files(
|
||||
request: LibraryAlbumMetadataUpdateRequest,
|
||||
state: State<'_, LibraryState>,
|
||||
) -> Result<(), String> {
|
||||
log::info!(
|
||||
"Command: library_write_album_metadata_to_files {}",
|
||||
request.album_group_key
|
||||
);
|
||||
|
||||
if request.album_group_key.trim().is_empty() {
|
||||
return Err("Album ID is required.".to_string());
|
||||
}
|
||||
if request.album_title.trim().is_empty() {
|
||||
return Err("Album title is required.".to_string());
|
||||
}
|
||||
if request.tracks.is_empty() {
|
||||
return Err("Album track list is empty.".to_string());
|
||||
}
|
||||
|
||||
let db = state.db.lock().await;
|
||||
let existing_tracks = db
|
||||
.get_album_tracks(&request.album_group_key)
|
||||
.map_err(|e| e.to_string())?;
|
||||
if existing_tracks
|
||||
.iter()
|
||||
.any(|t| t.cue_file_path.is_some() || t.cue_start_secs.is_some())
|
||||
{
|
||||
return Err("Writing tags to files is not supported for CUE-based albums. Use sidecar mode instead.".to_string());
|
||||
}
|
||||
drop(db);
|
||||
|
||||
let album_dir = PathBuf::from(request.album_group_key.trim());
|
||||
if !album_dir.is_dir() {
|
||||
return Err("Album folder not found on disk.".to_string());
|
||||
}
|
||||
|
||||
// Write embedded tags for each track file.
|
||||
let write_result = tokio::task::spawn_blocking({
|
||||
let request = request.clone();
|
||||
move || -> Result<(), String> {
|
||||
use lofty::{Accessor, AudioFile, ItemKey, Tag, TagExt, TaggedFileExt, TagType};
|
||||
|
||||
// Ensure we only write each file once.
|
||||
let mut by_file: HashMap<String, &LibraryAlbumTrackMetadataUpdate> = HashMap::new();
|
||||
for track in &request.tracks {
|
||||
by_file.entry(track.file_path.clone()).or_insert(track);
|
||||
}
|
||||
|
||||
for (file_path, track) in by_file {
|
||||
let path = Path::new(&file_path);
|
||||
if !path.is_file() {
|
||||
return Err("One or more audio files were not found on disk.".to_string());
|
||||
}
|
||||
|
||||
let mut tagged_file =
|
||||
lofty::read_from_path(path).map_err(|_| "Failed to read audio file tags.".to_string())?;
|
||||
|
||||
let primary_type = tagged_file.primary_tag_type();
|
||||
if tagged_file.primary_tag_mut().is_none() && tagged_file.first_tag_mut().is_none() {
|
||||
tagged_file.insert_tag(Tag::new(primary_type));
|
||||
}
|
||||
|
||||
{
|
||||
let tag = if let Some(tag) = tagged_file.primary_tag_mut() {
|
||||
tag
|
||||
} else if let Some(tag) = tagged_file.first_tag_mut() {
|
||||
tag
|
||||
} else {
|
||||
return Err("Failed to access audio file tags.".to_string());
|
||||
};
|
||||
|
||||
tag.set_title(track.title.trim().to_string());
|
||||
tag.set_album(request.album_title.trim().to_string());
|
||||
tag.set_artist(request.album_artist.trim().to_string());
|
||||
|
||||
if let Some(no) = track.track_number {
|
||||
tag.set_track(no);
|
||||
}
|
||||
if let Some(disc) = track.disc_number {
|
||||
tag.set_disk(disc);
|
||||
}
|
||||
|
||||
// Album artist (not part of Accessor).
|
||||
if request.album_artist.trim().is_empty() {
|
||||
tag.remove_key(&ItemKey::AlbumArtist);
|
||||
} else {
|
||||
tag.insert_text(ItemKey::AlbumArtist, request.album_artist.trim().to_string());
|
||||
}
|
||||
|
||||
// Year / Genre
|
||||
if let Some(year) = request.year {
|
||||
tag.set_year(year);
|
||||
} else {
|
||||
tag.remove_year();
|
||||
}
|
||||
|
||||
if let Some(ref genre) = request.genre {
|
||||
let g = genre.trim();
|
||||
if g.is_empty() {
|
||||
tag.remove_genre();
|
||||
} else {
|
||||
tag.set_genre(g.to_string());
|
||||
}
|
||||
} else {
|
||||
tag.remove_genre();
|
||||
}
|
||||
|
||||
if let Some(ref cat) = request.catalog_number {
|
||||
let c = cat.trim();
|
||||
if c.is_empty() {
|
||||
tag.remove_key(&ItemKey::CatalogNumber);
|
||||
} else {
|
||||
tag.insert_text(ItemKey::CatalogNumber, c.to_string());
|
||||
}
|
||||
} else {
|
||||
tag.remove_key(&ItemKey::CatalogNumber);
|
||||
}
|
||||
}
|
||||
|
||||
tagged_file
|
||||
.save_to_path(path)
|
||||
.map_err(|_| "Failed to write tags to audio files. Check that the album folder is mounted read-write and you have permissions.".to_string())?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
})
|
||||
.await
|
||||
.map_err(|e| format!("Failed to write tags: {}", e))?;
|
||||
write_result?;
|
||||
|
||||
// Remove sidecar (direct-edit mode disables sidecar persistence).
|
||||
let _ = tokio::task::spawn_blocking({
|
||||
let album_dir = album_dir.clone();
|
||||
move || crate::library::delete_album_sidecar(&album_dir)
|
||||
})
|
||||
.await;
|
||||
|
||||
// Update DB from the requested values.
|
||||
let mut db = state.db.lock().await;
|
||||
let existing_tracks = db
|
||||
.get_album_tracks(&request.album_group_key)
|
||||
.map_err(|e| e.to_string())?;
|
||||
let track_artist_match = compute_track_artist_match(&existing_tracks);
|
||||
let track_updates = request
|
||||
.tracks
|
||||
.iter()
|
||||
.map(|t| crate::library::AlbumTrackUpdate {
|
||||
id: t.id,
|
||||
title: t.title.clone(),
|
||||
disc_number: t.disc_number,
|
||||
track_number: t.track_number,
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
db.update_album_group_metadata(
|
||||
&request.album_group_key,
|
||||
&request.album_title,
|
||||
&request.album_artist,
|
||||
request.year,
|
||||
request.genre.as_deref(),
|
||||
request.catalog_number.as_deref(),
|
||||
track_artist_match.as_deref(),
|
||||
&track_updates,
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn library_refresh_album_metadata_from_files(
|
||||
album_group_key: String,
|
||||
state: State<'_, LibraryState>,
|
||||
) -> Result<(), String> {
|
||||
log::info!(
|
||||
"Command: library_refresh_album_metadata_from_files {}",
|
||||
album_group_key
|
||||
);
|
||||
|
||||
if album_group_key.trim().is_empty() {
|
||||
return Err("Album ID is required.".to_string());
|
||||
}
|
||||
|
||||
let db = state.db.lock().await;
|
||||
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());
|
||||
}
|
||||
if existing_tracks
|
||||
.iter()
|
||||
.any(|t| t.cue_file_path.is_some() || t.cue_start_secs.is_some())
|
||||
{
|
||||
return Err(
|
||||
"Refreshing metadata from files is not supported for CUE-based albums.".to_string(),
|
||||
);
|
||||
}
|
||||
drop(db);
|
||||
|
||||
let album_dir = PathBuf::from(album_group_key.trim());
|
||||
if !album_dir.is_dir() {
|
||||
return Err("Album folder not found on disk.".to_string());
|
||||
}
|
||||
|
||||
// Delete sidecar, then refresh DB from embedded tags.
|
||||
let refresh = tokio::task::spawn_blocking({
|
||||
let existing_tracks = existing_tracks.clone();
|
||||
let album_dir = album_dir.clone();
|
||||
move || -> Result<Vec<crate::library::TrackMetadataUpdateFull>, String> {
|
||||
let _ = crate::library::delete_album_sidecar(&album_dir);
|
||||
|
||||
let mut updates: Vec<crate::library::TrackMetadataUpdateFull> = Vec::new();
|
||||
for track in existing_tracks {
|
||||
let path = Path::new(&track.file_path);
|
||||
if !path.is_file() {
|
||||
return Err("One or more audio files were not found on disk.".to_string());
|
||||
}
|
||||
|
||||
let extracted =
|
||||
MetadataExtractor::extract(path).map_err(|_| "Failed to read audio file tags.".to_string())?;
|
||||
|
||||
let album_group_title = if extracted.album_group_title.trim().is_empty() {
|
||||
extracted.album.clone()
|
||||
} else {
|
||||
extracted.album_group_title.clone()
|
||||
};
|
||||
|
||||
updates.push(crate::library::TrackMetadataUpdateFull {
|
||||
id: track.id,
|
||||
title: extracted.title,
|
||||
artist: extracted.artist,
|
||||
album: extracted.album,
|
||||
album_artist: extracted.album_artist,
|
||||
album_group_title,
|
||||
track_number: extracted.track_number,
|
||||
disc_number: extracted.disc_number,
|
||||
year: extracted.year,
|
||||
genre: extracted.genre,
|
||||
catalog_number: extracted.catalog_number,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(updates)
|
||||
}
|
||||
})
|
||||
.await
|
||||
.map_err(|e| format!("Failed to refresh metadata: {}", e))?;
|
||||
let updates = refresh?;
|
||||
|
||||
let mut db = state.db.lock().await;
|
||||
db.update_tracks_metadata_by_id(&updates)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn library_get_hidden_albums(
|
||||
state: State<'_, LibraryState>,
|
||||
|
||||
@@ -5,6 +5,29 @@ use std::path::Path;
|
||||
|
||||
use crate::library::{AudioFormat, LibraryError, LocalAlbum, LocalArtist, LocalTrack};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AlbumTrackUpdate {
|
||||
pub id: i64,
|
||||
pub title: String,
|
||||
pub disc_number: Option<u32>,
|
||||
pub track_number: Option<u32>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TrackMetadataUpdateFull {
|
||||
pub id: i64,
|
||||
pub title: String,
|
||||
pub artist: String,
|
||||
pub album: String,
|
||||
pub album_artist: Option<String>,
|
||||
pub album_group_title: String,
|
||||
pub track_number: Option<u32>,
|
||||
pub disc_number: Option<u32>,
|
||||
pub year: Option<u32>,
|
||||
pub genre: Option<String>,
|
||||
pub catalog_number: Option<String>,
|
||||
}
|
||||
|
||||
/// Library database wrapper
|
||||
pub struct LibraryDatabase {
|
||||
conn: Connection,
|
||||
@@ -1250,6 +1273,145 @@ impl LibraryDatabase {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn update_album_group_metadata(
|
||||
&mut self,
|
||||
group_key: &str,
|
||||
album_title: &str,
|
||||
album_artist: &str,
|
||||
year: Option<u32>,
|
||||
genre: Option<&str>,
|
||||
catalog_number: Option<&str>,
|
||||
track_artist_match: Option<&str>,
|
||||
track_updates: &[AlbumTrackUpdate],
|
||||
) -> Result<(), LibraryError> {
|
||||
let tx = self
|
||||
.conn
|
||||
.transaction()
|
||||
.map_err(|e| LibraryError::Database(e.to_string()))?;
|
||||
|
||||
let normalized_album_artist = {
|
||||
let trimmed = album_artist.trim();
|
||||
if trimmed.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(trimmed.to_string())
|
||||
}
|
||||
};
|
||||
|
||||
tx.execute(
|
||||
r#"
|
||||
UPDATE local_tracks
|
||||
SET
|
||||
album = ?1,
|
||||
album_group_title = ?2,
|
||||
album_artist = ?3,
|
||||
year = ?4,
|
||||
genre = ?5,
|
||||
catalog_number = ?6
|
||||
WHERE COALESCE(album_group_key, album || '|' || COALESCE(album_artist, artist)) = ?7
|
||||
"#,
|
||||
params![
|
||||
album_title.trim(),
|
||||
album_title.trim(),
|
||||
normalized_album_artist,
|
||||
year,
|
||||
genre.map(|s| s.trim()).filter(|s| !s.is_empty()),
|
||||
catalog_number.map(|s| s.trim()).filter(|s| !s.is_empty()),
|
||||
group_key
|
||||
],
|
||||
)
|
||||
.map_err(|e| LibraryError::Database(e.to_string()))?;
|
||||
|
||||
if let Some(match_artist) = track_artist_match {
|
||||
let match_trim = match_artist.trim();
|
||||
if !match_trim.is_empty() && !album_artist.trim().is_empty() {
|
||||
tx.execute(
|
||||
r#"
|
||||
UPDATE local_tracks
|
||||
SET artist = ?1
|
||||
WHERE COALESCE(album_group_key, album || '|' || COALESCE(album_artist, artist)) = ?2
|
||||
AND artist = ?3
|
||||
"#,
|
||||
params![album_artist.trim(), group_key, match_trim],
|
||||
)
|
||||
.map_err(|e| LibraryError::Database(e.to_string()))?;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
let mut stmt = tx
|
||||
.prepare("UPDATE local_tracks SET title = ?1, disc_number = ?2, track_number = ?3 WHERE id = ?4")
|
||||
.map_err(|e| LibraryError::Database(e.to_string()))?;
|
||||
|
||||
for update in track_updates {
|
||||
stmt.execute(params![
|
||||
update.title.trim(),
|
||||
update.disc_number,
|
||||
update.track_number,
|
||||
update.id
|
||||
])
|
||||
.map_err(|e| LibraryError::Database(e.to_string()))?;
|
||||
}
|
||||
}
|
||||
|
||||
tx.commit()
|
||||
.map_err(|e| LibraryError::Database(e.to_string()))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn update_tracks_metadata_by_id(
|
||||
&mut self,
|
||||
updates: &[TrackMetadataUpdateFull],
|
||||
) -> Result<(), LibraryError> {
|
||||
let tx = self
|
||||
.conn
|
||||
.transaction()
|
||||
.map_err(|e| LibraryError::Database(e.to_string()))?;
|
||||
|
||||
{
|
||||
let mut stmt = tx
|
||||
.prepare(
|
||||
r#"
|
||||
UPDATE local_tracks
|
||||
SET
|
||||
title = ?1,
|
||||
artist = ?2,
|
||||
album = ?3,
|
||||
album_artist = ?4,
|
||||
album_group_title = ?5,
|
||||
track_number = ?6,
|
||||
disc_number = ?7,
|
||||
year = ?8,
|
||||
genre = ?9,
|
||||
catalog_number = ?10
|
||||
WHERE id = ?11
|
||||
"#,
|
||||
)
|
||||
.map_err(|e| LibraryError::Database(e.to_string()))?;
|
||||
|
||||
for update in updates {
|
||||
stmt.execute(params![
|
||||
update.title.trim(),
|
||||
update.artist.trim(),
|
||||
update.album.trim(),
|
||||
update.album_artist.as_ref().map(|s| s.trim().to_string()),
|
||||
update.album_group_title.trim(),
|
||||
update.track_number,
|
||||
update.disc_number,
|
||||
update.year,
|
||||
update.genre.as_ref().map(|s| s.trim().to_string()),
|
||||
update.catalog_number.as_ref().map(|s| s.trim().to_string()),
|
||||
update.id
|
||||
])
|
||||
.map_err(|e| LibraryError::Database(e.to_string()))?;
|
||||
}
|
||||
}
|
||||
|
||||
tx.commit()
|
||||
.map_err(|e| LibraryError::Database(e.to_string()))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn find_album_group_key(
|
||||
&self,
|
||||
album: &str,
|
||||
|
||||
@@ -10,14 +10,19 @@ pub mod errors;
|
||||
pub mod metadata;
|
||||
pub mod models;
|
||||
pub mod scanner;
|
||||
pub mod tag_sidecar;
|
||||
pub mod thumbnails;
|
||||
|
||||
pub use commands::LibraryState;
|
||||
pub use cue_parser::{cue_to_tracks, CueParser, CueSheet, CueTrack};
|
||||
pub use database::{LibraryDatabase, LibraryFolder, LibraryStats, PlaylistFolder, PlaylistSettings, PlaylistStats};
|
||||
pub use database::{
|
||||
AlbumTrackUpdate, LibraryDatabase, LibraryFolder, LibraryStats, PlaylistFolder, PlaylistSettings,
|
||||
PlaylistStats, TrackMetadataUpdateFull,
|
||||
};
|
||||
pub use errors::LibraryError;
|
||||
pub use metadata::MetadataExtractor;
|
||||
pub use models::*;
|
||||
pub use tag_sidecar::*;
|
||||
pub use scanner::{LibraryScanner, ScanResult};
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
@@ -0,0 +1,153 @@
|
||||
//! LocalLibrary album tag sidecar support.
|
||||
//!
|
||||
//! Sidecar files live next to album folders (default `.qbz.json`) and store
|
||||
//! album-level + per-track metadata overrides.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use crate::library::{LibraryError, LocalTrack};
|
||||
|
||||
const SIDECAR_FILE_NAME: &str = ".qbz.json";
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AlbumMetadataOverride {
|
||||
pub album_title: Option<String>,
|
||||
pub album_artist: Option<String>,
|
||||
pub year: Option<u32>,
|
||||
pub genre: Option<String>,
|
||||
pub catalog_number: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct TrackMetadataOverride {
|
||||
pub file_path: String,
|
||||
pub cue_start_secs: Option<f64>,
|
||||
pub title: Option<String>,
|
||||
pub disc_number: Option<u32>,
|
||||
pub track_number: Option<u32>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AlbumTagSidecar {
|
||||
pub version: u32,
|
||||
pub updated_at: i64,
|
||||
pub album: AlbumMetadataOverride,
|
||||
pub tracks: Vec<TrackMetadataOverride>,
|
||||
}
|
||||
|
||||
impl AlbumTagSidecar {
|
||||
pub fn new(album: AlbumMetadataOverride, tracks: Vec<TrackMetadataOverride>) -> Self {
|
||||
let now = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map(|d| d.as_secs() as i64)
|
||||
.unwrap_or(0);
|
||||
|
||||
Self {
|
||||
version: 1,
|
||||
updated_at: now,
|
||||
album,
|
||||
tracks,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sidecar_path(album_dir: &Path) -> PathBuf {
|
||||
album_dir.join(SIDECAR_FILE_NAME)
|
||||
}
|
||||
|
||||
pub fn read_album_sidecar(album_dir: &Path) -> Result<Option<AlbumTagSidecar>, LibraryError> {
|
||||
let path = sidecar_path(album_dir);
|
||||
if !path.exists() {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let bytes = fs::read(&path).map_err(LibraryError::Io)?;
|
||||
let sidecar: AlbumTagSidecar =
|
||||
serde_json::from_slice(&bytes).map_err(|e| LibraryError::Metadata(e.to_string()))?;
|
||||
Ok(Some(sidecar))
|
||||
}
|
||||
|
||||
pub fn write_album_sidecar(album_dir: &Path, sidecar: &AlbumTagSidecar) -> Result<(), LibraryError> {
|
||||
fs::create_dir_all(album_dir).map_err(LibraryError::Io)?;
|
||||
|
||||
let target = sidecar_path(album_dir);
|
||||
let tmp = album_dir.join(format!("{}.tmp", SIDECAR_FILE_NAME));
|
||||
let content =
|
||||
serde_json::to_vec_pretty(sidecar).map_err(|e| LibraryError::Metadata(e.to_string()))?;
|
||||
|
||||
fs::write(&tmp, content).map_err(LibraryError::Io)?;
|
||||
fs::rename(&tmp, &target).map_err(LibraryError::Io)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn delete_album_sidecar(album_dir: &Path) -> Result<(), LibraryError> {
|
||||
let path = sidecar_path(album_dir);
|
||||
if !path.exists() {
|
||||
return Ok(());
|
||||
}
|
||||
fs::remove_file(&path).map_err(LibraryError::Io)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn apply_sidecar_to_track(track: &mut LocalTrack, sidecar: &AlbumTagSidecar) {
|
||||
if let Some(title) = sidecar.album.album_title.as_ref().and_then(|s| normalize(s)) {
|
||||
track.album = title.clone();
|
||||
track.album_group_title = title.clone();
|
||||
}
|
||||
|
||||
if let Some(album_artist) = sidecar.album.album_artist.as_ref().and_then(|s| normalize(s)) {
|
||||
track.album_artist = Some(album_artist.clone());
|
||||
}
|
||||
|
||||
if let Some(year) = sidecar.album.year {
|
||||
track.year = Some(year);
|
||||
}
|
||||
|
||||
if let Some(genre) = sidecar.album.genre.as_ref().and_then(|s| normalize(s)) {
|
||||
track.genre = Some(genre.clone());
|
||||
}
|
||||
|
||||
if let Some(cat) = sidecar
|
||||
.album
|
||||
.catalog_number
|
||||
.as_ref()
|
||||
.and_then(|s| normalize(s))
|
||||
{
|
||||
track.catalog_number = Some(cat.clone());
|
||||
}
|
||||
|
||||
if let Some(entry) = sidecar.tracks.iter().find(|t| {
|
||||
t.file_path == track.file_path
|
||||
&& match (t.cue_start_secs, track.cue_start_secs) {
|
||||
(Some(a), Some(b)) => (a - b).abs() < 0.001,
|
||||
(None, None) => true,
|
||||
_ => false,
|
||||
}
|
||||
}) {
|
||||
if let Some(title) = entry.title.as_ref().and_then(|s| normalize(s)) {
|
||||
track.title = title.clone();
|
||||
}
|
||||
if let Some(disc) = entry.disc_number {
|
||||
track.disc_number = Some(disc);
|
||||
}
|
||||
if let Some(no) = entry.track_number {
|
||||
track.track_number = Some(no);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn normalize(value: &str) -> Option<String> {
|
||||
let trimmed = value.trim();
|
||||
if trimmed.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(trimmed.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user