From 7dff9bc6961cc779007d2a83872f74c1a7803ee6 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sat, 15 Jun 2019 00:48:03 -0700 Subject: [PATCH] Fixed: Errors logged during import when existing episode file is partial removed in the DB Fixes #3159 --- .../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/EpisodeImport/Specifications/UpgradeSpecificationFixture.cs b/src/NzbDrone.Core.Test/MediaFiles/EpisodeImport/Specifications/UpgradeSpecificationFixture.cs index 2f621f31f..4a862400d 100644 --- a/src/NzbDrone.Core.Test/MediaFiles/EpisodeImport/Specifications/UpgradeSpecificationFixture.cs +++ b/src/NzbDrone.Core.Test/MediaFiles/EpisodeImport/Specifications/UpgradeSpecificationFixture.cs @@ -302,5 +302,18 @@ namespace NzbDrone.Core.Test.MediaFiles.EpisodeImport.Specifications Subject.IsSatisfiedBy(_localEpisode, null).Accepted.Should().BeTrue(); } + + [Test] + public void should_return_true_if_episode_file_is_null() + { + _localEpisode.Episodes = Builder.CreateListOfSize(2) + .All() + .With(e => e.EpisodeFileId = 1) + .With(e => e.EpisodeFile = new LazyLoaded(null)) + .Build() + .ToList(); + + Subject.IsSatisfiedBy(_localEpisode, null).Accepted.Should().BeTrue(); + } } } diff --git a/src/NzbDrone.Core.Test/MediaFiles/UpgradeMediaFileServiceFixture.cs b/src/NzbDrone.Core.Test/MediaFiles/UpgradeMediaFileServiceFixture.cs index 755800253..d55a4a0a2 100644 --- a/src/NzbDrone.Core.Test/MediaFiles/UpgradeMediaFileServiceFixture.cs +++ b/src/NzbDrone.Core.Test/MediaFiles/UpgradeMediaFileServiceFixture.cs @@ -194,5 +194,20 @@ namespace NzbDrone.Core.Test.MediaFiles Mocker.GetMock().Verify(v => v.Delete(_localEpisode.Episodes.Single().EpisodeFile.Value, DeleteMediaFileReason.Upgrade), Times.Never()); } + + [Test] + public void should_import_if_existing_file_doesnt_exist_in_db() + { + _localEpisode.Episodes = Builder.CreateListOfSize(1) + .All() + .With(e => e.EpisodeFileId = 1) + .With(e => e.EpisodeFile = new LazyLoaded(null)) + .Build() + .ToList(); + + Subject.UpgradeEpisodeFile(_episodeFile, _localEpisode); + + Mocker.GetMock().Verify(v => v.Delete(_localEpisode.Episodes.Single().EpisodeFile.Value, It.IsAny()), Times.Never()); + } } } diff --git a/src/NzbDrone.Core/MediaFiles/EpisodeImport/Specifications/UpgradeSpecification.cs b/src/NzbDrone.Core/MediaFiles/EpisodeImport/Specifications/UpgradeSpecification.cs index f42ae48d5..a031edd4a 100644 --- a/src/NzbDrone.Core/MediaFiles/EpisodeImport/Specifications/UpgradeSpecification.cs +++ b/src/NzbDrone.Core/MediaFiles/EpisodeImport/Specifications/UpgradeSpecification.cs @@ -29,6 +29,13 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport.Specifications foreach (var episode in localEpisode.Episodes.Where(e => e.EpisodeFileId > 0)) { var episodeFile = episode.EpisodeFile.Value; + + if (episodeFile == null) + { + _logger.Trace("Unable to get episode file details from the DB. EpisodeId: {0} EpisodeFileId: {1}", episode.Id, episode.EpisodeFileId); + continue; + } + var qualityCompare = qualityComparer.Compare(localEpisode.Quality.Quality, episodeFile.Quality.Quality); if (qualityCompare < 0) diff --git a/src/NzbDrone.Core/MediaFiles/UpgradeMediaFileService.cs b/src/NzbDrone.Core/MediaFiles/UpgradeMediaFileService.cs index 2bcd7b25a..9844f6c07 100644 --- a/src/NzbDrone.Core/MediaFiles/UpgradeMediaFileService.cs +++ b/src/NzbDrone.Core/MediaFiles/UpgradeMediaFileService.cs @@ -40,6 +40,7 @@ namespace NzbDrone.Core.MediaFiles var existingFiles = localEpisode.Episodes .Where(e => e.EpisodeFileId > 0) .Select(e => e.EpisodeFile.Value) + .Where(e => e != null) .GroupBy(e => e.Id) .ToList();