From 855a8dbf85e9c3106ab1ecd43e93b42cfc4752d5 Mon Sep 17 00:00:00 2001 From: Qstick Date: Tue, 10 Sep 2019 23:30:58 -0400 Subject: [PATCH] Fixed: Errors logged during import when existing episode file is partial removed in the DB Co-Authored-By: Mark McDowall --- .../Specifications/UpgradeSpecificationFixture.cs | 13 +++++++++++++ .../MediaFiles/UpgradeMediaFileServiceFixture.cs | 15 +++++++++++++++ .../Specifications/UpgradeSpecification.cs | 7 +++++++ .../MediaFiles/UpgradeMediaFileService.cs | 1 + 4 files changed, 36 insertions(+) diff --git a/src/NzbDrone.Core.Test/MediaFiles/TrackImport/Specifications/UpgradeSpecificationFixture.cs b/src/NzbDrone.Core.Test/MediaFiles/TrackImport/Specifications/UpgradeSpecificationFixture.cs index 0be4ba3a7..b648af0da 100644 --- a/src/NzbDrone.Core.Test/MediaFiles/TrackImport/Specifications/UpgradeSpecificationFixture.cs +++ b/src/NzbDrone.Core.Test/MediaFiles/TrackImport/Specifications/UpgradeSpecificationFixture.cs @@ -224,5 +224,18 @@ namespace NzbDrone.Core.Test.MediaFiles.TrackImport.Specifications Subject.IsSatisfiedBy(_localTrack).Accepted.Should().BeTrue(); } + + [Test] + public void should_return_true_if_track_file_is_null() + { + _localTrack.Tracks = Builder.CreateListOfSize(2) + .All() + .With(e => e.TrackFileId = 1) + .With(e => e.TrackFile = new LazyLoaded(null)) + .Build() + .ToList(); + + Subject.IsSatisfiedBy(_localTrack).Accepted.Should().BeTrue(); + } } } diff --git a/src/NzbDrone.Core.Test/MediaFiles/UpgradeMediaFileServiceFixture.cs b/src/NzbDrone.Core.Test/MediaFiles/UpgradeMediaFileServiceFixture.cs index 0d5bc318a..39ab2d558 100644 --- a/src/NzbDrone.Core.Test/MediaFiles/UpgradeMediaFileServiceFixture.cs +++ b/src/NzbDrone.Core.Test/MediaFiles/UpgradeMediaFileServiceFixture.cs @@ -181,5 +181,20 @@ namespace NzbDrone.Core.Test.MediaFiles Subject.UpgradeTrackFile(_trackFile, _localTrack).OldFiles.Count.Should().Be(2); } + + [Test] + public void should_import_if_existing_file_doesnt_exist_in_db() + { + _localTrack.Tracks = Builder.CreateListOfSize(1) + .All() + .With(e => e.TrackFileId = 1) + .With(e => e.TrackFile = new LazyLoaded(null)) + .Build() + .ToList(); + + Subject.UpgradeTrackFile(_trackFile, _localTrack); + + Mocker.GetMock().Verify(v => v.Delete(_localTrack.Tracks.Single().TrackFile.Value, It.IsAny()), Times.Never()); + } } } diff --git a/src/NzbDrone.Core/MediaFiles/TrackImport/Specifications/UpgradeSpecification.cs b/src/NzbDrone.Core/MediaFiles/TrackImport/Specifications/UpgradeSpecification.cs index e749a00a5..7dd26ade8 100644 --- a/src/NzbDrone.Core/MediaFiles/TrackImport/Specifications/UpgradeSpecification.cs +++ b/src/NzbDrone.Core/MediaFiles/TrackImport/Specifications/UpgradeSpecification.cs @@ -27,6 +27,13 @@ namespace NzbDrone.Core.MediaFiles.TrackImport.Specifications foreach (var track in localTrack.Tracks.Where(e => e.TrackFileId > 0)) { var trackFile = track.TrackFile.Value; + + if (trackFile == null) + { + _logger.Trace("Unable to get track file details from the DB. TrackId: {0} TrackFileId: {1}", track.Id, track.TrackFileId); + continue; + } + var qualityCompare = qualityComparer.Compare(localTrack.Quality.Quality, trackFile.Quality.Quality); if (qualityCompare < 0) diff --git a/src/NzbDrone.Core/MediaFiles/UpgradeMediaFileService.cs b/src/NzbDrone.Core/MediaFiles/UpgradeMediaFileService.cs index ac7a010ea..ad4ee13ff 100644 --- a/src/NzbDrone.Core/MediaFiles/UpgradeMediaFileService.cs +++ b/src/NzbDrone.Core/MediaFiles/UpgradeMediaFileService.cs @@ -43,6 +43,7 @@ namespace NzbDrone.Core.MediaFiles var existingFiles = localTrack.Tracks .Where(e => e.TrackFileId > 0) .Select(e => e.TrackFile.Value) + .Where(e => e != null) .GroupBy(e => e.Id) .ToList();