Fix album tag consistency handler: open each file once for all field changes
This commit is contained in:
parent
d75893bc30
commit
1e54ff54ac
1 changed files with 28 additions and 24 deletions
|
|
@ -1313,41 +1313,45 @@ class RepairWorker:
|
||||||
from mutagen import File as MutagenFile
|
from mutagen import File as MutagenFile
|
||||||
from core.repair_jobs.album_tag_consistency import _read_tag, _write_tag
|
from core.repair_jobs.album_tag_consistency import _read_tag, _write_tag
|
||||||
|
|
||||||
|
# Build field → canonical value map
|
||||||
|
canonical_map = {inc['field']: inc['canonical'] for inc in inconsistencies}
|
||||||
|
|
||||||
fixed_files = 0
|
fixed_files = 0
|
||||||
errors = 0
|
errors = 0
|
||||||
changes = []
|
changes = []
|
||||||
|
|
||||||
for inc in inconsistencies:
|
for track_info in tracks:
|
||||||
field = inc['field']
|
track_file = track_info.get('file_path', '')
|
||||||
canonical = inc['canonical']
|
if not track_file:
|
||||||
|
continue
|
||||||
|
|
||||||
for track_info in tracks:
|
download_folder = None
|
||||||
track_file = track_info.get('file_path', '')
|
if self._config_manager:
|
||||||
if not track_file:
|
download_folder = self._config_manager.get('soulseek.download_path', '')
|
||||||
|
resolved = _resolve_file_path(track_file, self.transfer_folder, download_folder)
|
||||||
|
if not resolved or not os.path.exists(resolved):
|
||||||
|
continue
|
||||||
|
|
||||||
|
try:
|
||||||
|
audio = MutagenFile(resolved, easy=False)
|
||||||
|
if audio is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Resolve path
|
# Apply all field fixes in one open/save cycle
|
||||||
download_folder = None
|
file_changed = False
|
||||||
if self._config_manager:
|
for field, canonical in canonical_map.items():
|
||||||
download_folder = self._config_manager.get('soulseek.download_path', '')
|
|
||||||
resolved = _resolve_file_path(track_file, self.transfer_folder, download_folder)
|
|
||||||
if not resolved or not os.path.exists(resolved):
|
|
||||||
continue
|
|
||||||
|
|
||||||
try:
|
|
||||||
audio = MutagenFile(resolved, easy=False)
|
|
||||||
if audio is None:
|
|
||||||
continue
|
|
||||||
|
|
||||||
current = _read_tag(audio, field)
|
current = _read_tag(audio, field)
|
||||||
if current and current != canonical:
|
if current and current != canonical:
|
||||||
if _write_tag(audio, field, canonical):
|
if _write_tag(audio, field, canonical):
|
||||||
audio.save()
|
file_changed = True
|
||||||
fixed_files += 1
|
|
||||||
changes.append(f'{field}: "{current}" → "{canonical}" in {os.path.basename(resolved)}')
|
changes.append(f'{field}: "{current}" → "{canonical}" in {os.path.basename(resolved)}')
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"Error fixing tag consistency for {resolved}: {e}")
|
if file_changed:
|
||||||
errors += 1
|
audio.save()
|
||||||
|
fixed_files += 1
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error fixing tag consistency for {resolved}: {e}")
|
||||||
|
errors += 1
|
||||||
|
|
||||||
if fixed_files > 0:
|
if fixed_files > 0:
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue