From 52247232d283ecf6b9cc9d5f89e5816b999c4ff7 Mon Sep 17 00:00:00 2001 From: chodeus <190988615+chodeus@users.noreply.github.com> Date: Wed, 18 Nov 2026 10:49:03 +0800 Subject: [PATCH] fix(bandcamp): write MusicBrainz track ids where Lidarr 4.0 keeps them --- .../Core/PostProcessing/PreImportTagger.cs | 3 +- .../Core/Utilities/MusicBrainzTrackIds.cs | 48 +++++++ .../Clients/Bandcamp/BandcampDownloadProxy.cs | 13 +- .../Sleezer.Tests/MusicBrainzTrackIdsTests.cs | 122 ++++++++++++++++++ .../PreImportTaggerWholeDownloadTests.cs | 3 +- tests/Sleezer.Tests/Sleezer.Tests.csproj | 3 + 6 files changed, 188 insertions(+), 4 deletions(-) create mode 100644 src/Sleezer/Core/Utilities/MusicBrainzTrackIds.cs create mode 100644 tests/Sleezer.Tests/MusicBrainzTrackIdsTests.cs diff --git a/src/Sleezer/Core/PostProcessing/PreImportTagger.cs b/src/Sleezer/Core/PostProcessing/PreImportTagger.cs index d49ccf1c..160ca3a7 100644 --- a/src/Sleezer/Core/PostProcessing/PreImportTagger.cs +++ b/src/Sleezer/Core/PostProcessing/PreImportTagger.cs @@ -589,7 +589,8 @@ FingerprintVerdict VerdictOf(LocalTrack file, Track track) => } } - // Lidarr writes the recording id as the MusicBrainz track id (AudioTagService.GetTrackMetadata). + // Reads the plugin's merged TagLib 2.2, whose MusicBrainzTrackId is the recording frame Lidarr writes; + // TagLib 2.5 (Lidarr 4.0) names that frame MusicBrainzRecordingId, so keep this off the host's TagLib. private static (string? Album, string? ReleaseId, string? RecordingId) WrittenIdentity(string path) { using TagLib.File file = TagLib.File.Create(path); diff --git a/src/Sleezer/Core/Utilities/MusicBrainzTrackIds.cs b/src/Sleezer/Core/Utilities/MusicBrainzTrackIds.cs new file mode 100644 index 00000000..2b6c07e5 --- /dev/null +++ b/src/Sleezer/Core/Utilities/MusicBrainzTrackIds.cs @@ -0,0 +1,48 @@ +using System; +using System.Reflection; +using System.Threading; +using NLog; +using NzbDrone.Core.MediaFiles; + +namespace NzbDrone.Plugin.Sleezer.Core.Utilities +{ + // Lidarr 4.0 moved the recording id to MusicBrainzRecordingId and dropped MusicBrainzReleaseTrackId. + // Never name either property directly: the plugin builds against the older AudioTag and must load on both. + public static class MusicBrainzTrackIds + { + private static readonly Logger _logger = LogManager.GetCurrentClassLogger(); + private static int _warnedUnknownShape; + + /// Sets the ids where the running Lidarr's AudioTag keeps them, else warns once and sets neither; a null id keeps the tag's value. + public static void Set(AudioTag tag, string? recordingId, string? releaseTrackId) => SetOn(tag, recordingId, releaseTrackId); + + internal static void SetOn(object tag, string? recordingId, string? releaseTrackId) + { + var type = tag.GetType(); + var trackId = StringProperty(type, "MusicBrainzTrackId"); + var recording = StringProperty(type, "MusicBrainzRecordingId"); + var releaseTrack = StringProperty(type, "MusicBrainzReleaseTrackId"); + + if (trackId != null && recording != null) + { + SetOrKeep(tag, trackId, releaseTrackId); + SetOrKeep(tag, recording, recordingId); + } + else if (trackId != null && releaseTrack != null) + { + SetOrKeep(tag, trackId, recordingId); + SetOrKeep(tag, releaseTrack, releaseTrackId); + } + else if (Interlocked.Exchange(ref _warnedUnknownShape, 1) == 0) + { + // Not a throw: the caller's retag must still write every other field. + _logger.Warn("{0} keeps the MusicBrainz recording and release track ids in properties this plugin does not know; retagged files keep their own", type.FullName); + } + } + + private static PropertyInfo? StringProperty(Type type, string name) => + type.GetProperty(name) is { CanRead: true, CanWrite: true } property && property.PropertyType == typeof(string) ? property : null; + + private static void SetOrKeep(object tag, PropertyInfo property, string? value) => property.SetValue(tag, value ?? property.GetValue(tag)); + } +} diff --git a/src/Sleezer/Download/Clients/Bandcamp/BandcampDownloadProxy.cs b/src/Sleezer/Download/Clients/Bandcamp/BandcampDownloadProxy.cs index e97e6121..89d37e60 100644 --- a/src/Sleezer/Download/Clients/Bandcamp/BandcampDownloadProxy.cs +++ b/src/Sleezer/Download/Clients/Bandcamp/BandcampDownloadProxy.cs @@ -9,6 +9,7 @@ using NzbDrone.Common.Http; using NzbDrone.Core.Http.Bandcamp; using NzbDrone.Core.MediaFiles; +using NzbDrone.Plugin.Sleezer.Core.Utilities; namespace NzbDrone.Core.Download.Clients.Bandcamp { @@ -360,6 +361,8 @@ private void NormalizeExtractedMetadata(string outputDir, BandcampDownloadItem i return; } + Exception? firstFailure = null; + var failed = 0; for (var i = 0; i < audioFiles.Count; i++) { try @@ -368,9 +371,16 @@ private void NormalizeExtractedMetadata(string outputDir, BandcampDownloadItem i } catch (Exception ex) { + firstFailure ??= ex; + failed++; _logger.Debug(ex, "Bandcamp download proxy [{0}]: Failed to normalize tags for {1}", item.DownloadId, audioFiles[i].Path); } } + + if (firstFailure != null) + { + _logger.Warn(firstFailure, "Bandcamp download proxy [{0}]: Failed to normalize tags for {1} of {2} file(s); they keep the store's own tags", item.DownloadId, failed, audioFiles.Count); + } } internal static void ApplyCanonicalTagsToFile(string path, @@ -404,8 +414,7 @@ internal static void ApplyCanonicalTagsToFile(string path, tag.MusicBrainzArtistId = context.ArtistMusicBrainzId ?? tag.MusicBrainzArtistId; tag.MusicBrainzReleaseArtistId = context.PreferredRelease?.ReleaseArtistMusicBrainzId ?? context.ArtistMusicBrainzId ?? tag.MusicBrainzReleaseArtistId; tag.MusicBrainzReleaseGroupId = context.AlbumMusicBrainzId ?? tag.MusicBrainzReleaseGroupId; - tag.MusicBrainzTrackId = trackContext?.RecordingMusicBrainzId ?? tag.MusicBrainzTrackId; - tag.MusicBrainzReleaseTrackId = trackContext?.ReleaseTrackMusicBrainzId ?? tag.MusicBrainzReleaseTrackId; + MusicBrainzTrackIds.Set(tag, recordingId: trackContext?.RecordingMusicBrainzId, releaseTrackId: trackContext?.ReleaseTrackMusicBrainzId); tag.MusicBrainzAlbumComment = context.AlbumDisambiguation ?? tag.MusicBrainzAlbumComment; tag.Write(path); } diff --git a/tests/Sleezer.Tests/MusicBrainzTrackIdsTests.cs b/tests/Sleezer.Tests/MusicBrainzTrackIdsTests.cs new file mode 100644 index 00000000..a7350a19 --- /dev/null +++ b/tests/Sleezer.Tests/MusicBrainzTrackIdsTests.cs @@ -0,0 +1,122 @@ +using System; +using System.IO; +using NzbDrone.Core.MediaFiles; +using NzbDrone.Plugin.Sleezer.Core.Utilities; +using Xunit; + +namespace Sleezer.Tests; + +// Lidarr 4.0 dropped AudioTag.MusicBrainzReleaseTrackId, so Bandcamp retagging named a member the host no longer had. +public class MusicBrainzTrackIdsTests : IDisposable +{ + private const string Recording = "2f3e1c0a-6a54-4c3b-9d1e-0c8f2b7a9e41"; + private const string ReleaseTrack = "9b7d4e2c-1f8a-4b6e-a3c5-7d2e9f0b1c63"; + private readonly string _dir = Path.Combine(Path.GetTempPath(), "sleezer-mbids-" + Path.GetRandomFileName()); + + public MusicBrainzTrackIdsTests() => Directory.CreateDirectory(_dir); + + public void Dispose() + { + if (Directory.Exists(_dir)) Directory.Delete(_dir, recursive: true); + GC.SuppressFinalize(this); + } + + // Same property shape as Lidarr 4.0's AudioTag; files written through the real one were checked outside this suite. + private sealed class RecordingIdAudioTag : AudioTag + { + public string? MusicBrainzRecordingId { get; set; } + } + + private sealed class UnknownTag + { + public string? MusicBrainzTrackId { get; set; } = ReleaseTrack; + } + + private sealed class GuidRecordingTag + { + public string? MusicBrainzTrackId { get; set; } = ReleaseTrack; + + public Guid MusicBrainzRecordingId { get; set; } + } + + [Fact] + public void Before_lidarr_4_the_track_id_holds_the_recording() + { + var tag = new AudioTag(); + + MusicBrainzTrackIds.Set(tag, Recording, ReleaseTrack); + + Assert.Equal(Recording, tag.MusicBrainzTrackId); + Assert.Equal(ReleaseTrack, tag.MusicBrainzReleaseTrackId); + } + + [Fact] + public void With_a_recording_id_property_the_track_id_holds_the_release_track() + { + var tag = new RecordingIdAudioTag(); + + MusicBrainzTrackIds.Set(tag, Recording, ReleaseTrack); + + Assert.Equal(ReleaseTrack, tag.MusicBrainzTrackId); + Assert.Equal(Recording, tag.MusicBrainzRecordingId); + Assert.Null(tag.MusicBrainzReleaseTrackId); + } + + [Fact] + public void Missing_ids_keep_the_tag_values_before_lidarr_4() + { + var tag = new AudioTag { MusicBrainzTrackId = Recording, MusicBrainzReleaseTrackId = ReleaseTrack }; + + MusicBrainzTrackIds.Set(tag, null, null); + + Assert.Equal(Recording, tag.MusicBrainzTrackId); + Assert.Equal(ReleaseTrack, tag.MusicBrainzReleaseTrackId); + } + + [Fact] + public void Missing_ids_keep_the_tag_values_with_a_recording_id_property() + { + var tag = new RecordingIdAudioTag { MusicBrainzTrackId = ReleaseTrack, MusicBrainzRecordingId = Recording }; + + MusicBrainzTrackIds.Set(tag, null, null); + + Assert.Equal(ReleaseTrack, tag.MusicBrainzTrackId); + Assert.Equal(Recording, tag.MusicBrainzRecordingId); + } + + [Fact] + public void An_unknown_tag_shape_is_left_alone_without_throwing() + { + var tag = new UnknownTag(); + + MusicBrainzTrackIds.SetOn(tag, Recording, null); + + Assert.Equal(ReleaseTrack, tag.MusicBrainzTrackId); + } + + [Fact] + public void A_recording_id_property_of_another_type_is_left_alone_without_throwing() + { + var tag = new GuidRecordingTag(); + + MusicBrainzTrackIds.SetOn(tag, Recording, null); + + Assert.Equal(ReleaseTrack, tag.MusicBrainzTrackId); + Assert.Equal(Guid.Empty, tag.MusicBrainzRecordingId); + } + + [Fact] + public void Before_lidarr_4_the_ids_written_to_the_file_read_back() + { + string path = Path.Combine(_dir, "track.flac"); + File.Copy(Path.Combine(AppContext.BaseDirectory, "Fixtures", "silence.flac"), path); + var tag = new AudioTag(path); + + MusicBrainzTrackIds.Set(tag, Recording, ReleaseTrack); + tag.Write(path); + + var read = new AudioTag(path); + Assert.Equal(Recording, read.MusicBrainzTrackId); + Assert.Equal(ReleaseTrack, read.MusicBrainzReleaseTrackId); + } +} diff --git a/tests/Sleezer.Tests/PreImportTaggerWholeDownloadTests.cs b/tests/Sleezer.Tests/PreImportTaggerWholeDownloadTests.cs index a1fa85eb..4f05c7ac 100644 --- a/tests/Sleezer.Tests/PreImportTaggerWholeDownloadTests.cs +++ b/tests/Sleezer.Tests/PreImportTaggerWholeDownloadTests.cs @@ -69,12 +69,13 @@ private static string AlbumTag(string path) } // As Lidarr's writer: the album title and the release id are the keys it groups a download by. + // The recording goes in by frame name, as every Lidarr writes it; TagLib 2.5 renamed the property that reads it. private static void TagAsWanted(TrackFile trackFile) { using TagLib.File file = TagLib.File.Create(trackFile.Path); file.Tag.Album = "Wanted Album"; file.Tag.MusicBrainzReleaseId = WantedReleaseId; - file.Tag.MusicBrainzTrackId = trackFile.Tracks.Value[0].ForeignRecordingId; + ((TagLib.Ogg.XiphComment)file.GetTag(TagLib.TagTypes.Xiph, true)).SetField("MUSICBRAINZ_TRACKID", trackFile.Tracks.Value[0].ForeignRecordingId); file.Save(); } diff --git a/tests/Sleezer.Tests/Sleezer.Tests.csproj b/tests/Sleezer.Tests/Sleezer.Tests.csproj index 0ac4babf..8958157a 100644 --- a/tests/Sleezer.Tests/Sleezer.Tests.csproj +++ b/tests/Sleezer.Tests/Sleezer.Tests.csproj @@ -101,6 +101,9 @@ + +