From c4b7e04a96f7073620ce7ed0eb865c90b5853465 Mon Sep 17 00:00:00 2001 From: ta264 Date: Wed, 4 Sep 2019 21:57:11 +0100 Subject: [PATCH] Fixed: NRE when tagging an album with omitted media --- .../MediaFiles/AudioTagServiceFixture.cs | 24 +++++++++++++++---- .../MediaFiles/AudioTagService.cs | 3 ++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/NzbDrone.Core.Test/MediaFiles/AudioTagServiceFixture.cs b/src/NzbDrone.Core.Test/MediaFiles/AudioTagServiceFixture.cs index a10476cdb..0e5c85e90 100644 --- a/src/NzbDrone.Core.Test/MediaFiles/AudioTagServiceFixture.cs +++ b/src/NzbDrone.Core.Test/MediaFiles/AudioTagServiceFixture.cs @@ -325,7 +325,7 @@ namespace NzbDrone.Core.Test.MediaFiles.AudioTagServiceFixture tag.OriginalReleaseDate.HasValue.Should().BeFalse(); } - private TrackFile GivenPopulatedTrackfile() + private TrackFile GivenPopulatedTrackfile(int mediumOffset) { var meta = Builder.CreateNew().Build(); var artist = Builder.CreateNew() @@ -337,6 +337,8 @@ namespace NzbDrone.Core.Test.MediaFiles.AudioTagServiceFixture .Build(); var media = Builder.CreateListOfSize(2).Build() as List; + media.ForEach(x => x.Number += mediumOffset); + var release = Builder.CreateNew() .With(x => x.Album = album) .With(x => x.Media = media) @@ -349,9 +351,9 @@ namespace NzbDrone.Core.Test.MediaFiles.AudioTagServiceFixture .With(x => x.AlbumRelease = release) .With(x => x.ArtistMetadata = meta) .TheFirst(5) - .With(x => x.MediumNumber = 1) + .With(x => x.MediumNumber = 1 + mediumOffset) .TheNext(5) - .With(x => x.MediumNumber = 2) + .With(x => x.MediumNumber = 2 + mediumOffset) .Build() as List; release.Tracks = tracks; @@ -366,12 +368,24 @@ namespace NzbDrone.Core.Test.MediaFiles.AudioTagServiceFixture [Test] public void get_metadata_should_not_fail_with_missing_country() { - var file = GivenPopulatedTrackfile(); + var file = GivenPopulatedTrackfile(0); var tag = Subject.GetTrackMetadata(file); tag.MusicBrainzReleaseCountry.Should().BeNull(); } + [Test] + public void should_not_fail_if_media_has_been_omitted() + { + // make sure that we aren't relying on index of items in + // Media being the same as the medium number + + var file = GivenPopulatedTrackfile(100); + var tag = Subject.GetTrackMetadata(file); + + tag.Media.Should().NotBeNull(); + } + [TestCase("nin.mp3")] public void write_tags_should_update_trackfile_size_and_modified(string filename) { @@ -381,7 +395,7 @@ namespace NzbDrone.Core.Test.MediaFiles.AudioTagServiceFixture GivenFileCopy(filename); - var file = GivenPopulatedTrackfile(); + var file = GivenPopulatedTrackfile(0); file.Path = copiedFile; Subject.WriteTags(file, false, true); diff --git a/src/NzbDrone.Core/MediaFiles/AudioTagService.cs b/src/NzbDrone.Core/MediaFiles/AudioTagService.cs index f4b1ec50e..90ade9763 100644 --- a/src/NzbDrone.Core/MediaFiles/AudioTagService.cs +++ b/src/NzbDrone.Core/MediaFiles/AudioTagService.cs @@ -107,7 +107,8 @@ namespace NzbDrone.Core.MediaFiles Album = album.Title, Disc = (uint)track.MediumNumber, DiscCount = (uint)release.Media.Count, - Media = release.Media[track.MediumNumber - 1].Format, + // We may have omitted media so index in the list isn't the same as medium number + Media = release.Media.SingleOrDefault(x => x.Number == track.MediumNumber).Format, Date = release.ReleaseDate, Year = (uint)album.ReleaseDate?.Year, OriginalReleaseDate = album.ReleaseDate,