From 37d5a3f2ad40998bf6f10efdc5c6156a50bc0a8c Mon Sep 17 00:00:00 2001 From: Daniel Smith Date: Tue, 28 Feb 2017 16:01:12 +0000 Subject: [PATCH] Fixed: Clear EpisodeFile records from database if Series folder is missing, but root folder appears to be mounted. --- .../DiskScanServiceTests/ScanFixture.cs | 84 ++++++++++++++----- .../MediaFiles/DiskScanService.cs | 29 ++++--- .../Events/SeriesScanSkippedEvent.cs | 3 +- 3 files changed, 83 insertions(+), 33 deletions(-) diff --git a/src/NzbDrone.Core.Test/MediaFiles/DiskScanServiceTests/ScanFixture.cs b/src/NzbDrone.Core.Test/MediaFiles/DiskScanServiceTests/ScanFixture.cs index 5bb18e455..137856434 100644 --- a/src/NzbDrone.Core.Test/MediaFiles/DiskScanServiceTests/ScanFixture.cs +++ b/src/NzbDrone.Core.Test/MediaFiles/DiskScanServiceTests/ScanFixture.cs @@ -5,6 +5,7 @@ using Moq; using NUnit.Framework; using NzbDrone.Common.Disk; +using NzbDrone.Core.Configuration; using NzbDrone.Core.MediaFiles; using NzbDrone.Core.MediaFiles.EpisodeImport; using NzbDrone.Core.Test.Framework; @@ -38,7 +39,7 @@ private void GivenParentFolderExists() Mocker.GetMock() .Setup(s => s.GetDirectories(It.IsAny())) - .Returns(new string[] { @"C:\Test\TV\Series2".AsOsAgnostic() }); + .Returns(new[] { @"C:\Test\TV\Series2".AsOsAgnostic() }); } private void GivenFiles(IEnumerable files) @@ -50,13 +51,13 @@ private void GivenFiles(IEnumerable files) [Test] public void should_not_scan_if_series_root_folder_does_not_exist() - { + { Subject.Scan(_series); ExceptionVerification.ExpectedWarns(1); Mocker.GetMock() - .Verify(v => v.Clean(It.IsAny(), It.IsAny>()), Times.Never()); + .Verify(v => v.Clean(It.IsAny(), It.IsAny>()), Times.Never()); } [Test] @@ -78,6 +79,63 @@ public void should_not_scan_if_series_root_folder_is_empty() .Verify(v => v.Clean(It.IsAny(), new List()), Times.Never()); } + [Test] + public void should_clean_but_not_import_if_series_folder_does_not_exist() + { + GivenParentFolderExists(); + + Mocker.GetMock() + .Setup(s => s.FolderExists(@"C:\Test\TV\Series")) + .Returns(false); + + Subject.Scan(_series); + + Mocker.GetMock() + .Verify(v => v.Clean(It.IsAny(), It.IsAny>()), Times.Once()); + + Mocker.GetMock() + .Verify(v => v.GetImportDecisions(It.IsAny>(), _series), Times.Never()); + } + + [Test] + public void should_create_and_clean_but_not_import_if_series_folder_does_not_exist_but_create_folder_enabled() + { + GivenParentFolderExists(); + + Mocker.GetMock() + .Setup(s => s.CreateEmptySeriesFolders) + .Returns(true); + + Mocker.GetMock() + .Setup(s => s.FolderExists(@"C:\Test\TV\Series")) + .Returns(false); + + Subject.Scan(_series); + + Mocker.GetMock() + .Verify(v => v.Clean(It.IsAny(), It.IsAny>()), Times.Once()); + + Mocker.GetMock() + .Verify(v => v.GetImportDecisions(It.IsAny>(), _series), Times.Never()); + } + + [Test] + public void should_find_files_at_root_of_series_folder() + { + GivenParentFolderExists(); + + GivenFiles(new List + { + Path.Combine(_series.Path, "file1.mkv").AsOsAgnostic(), + Path.Combine(_series.Path, "s01e01.mkv").AsOsAgnostic() + }); + + Subject.Scan(_series); + + Mocker.GetMock() + .Verify(v => v.GetImportDecisions(It.Is>(l => l.Count == 2), _series), Times.Once()); + } + [Test] public void should_not_scan_extras_subfolder() { @@ -94,6 +152,9 @@ public void should_not_scan_extras_subfolder() Subject.Scan(_series); + Mocker.GetMock() + .Verify(v => v.GetFiles(It.IsAny(), It.IsAny()), Times.Once()); + Mocker.GetMock() .Verify(v => v.GetImportDecisions(It.Is>(l => l.Count == 1), _series), Times.Once()); } @@ -229,23 +290,6 @@ public void should_scan_dotHack_folder() .Verify(v => v.GetImportDecisions(It.Is>(l => l.Count == 2), _series), Times.Once()); } - [Test] - public void should_find_files_at_root_of_series_folder() - { - GivenParentFolderExists(); - - GivenFiles(new List - { - Path.Combine(_series.Path, "file1.mkv").AsOsAgnostic(), - Path.Combine(_series.Path, "s01e01.mkv").AsOsAgnostic() - }); - - Subject.Scan(_series); - - Mocker.GetMock() - .Verify(v => v.GetImportDecisions(It.Is>(l => l.Count == 2), _series), Times.Once()); - } - [Test] public void should_exclude_osx_metadata_files() { diff --git a/src/NzbDrone.Core/MediaFiles/DiskScanService.cs b/src/NzbDrone.Core/MediaFiles/DiskScanService.cs index bf7ac5f0c..e769e38ca 100644 --- a/src/NzbDrone.Core/MediaFiles/DiskScanService.cs +++ b/src/NzbDrone.Core/MediaFiles/DiskScanService.cs @@ -78,15 +78,14 @@ public void Scan(Series series) { _logger.Warn("Series' root folder ({0}) is empty.", rootFolder); _eventAggregator.PublishEvent(new SeriesScanSkippedEvent(series, SeriesScanSkippedReason.RootFolderIsEmpty)); - return; + return; } _logger.ProgressInfo("Scanning disk for {0}", series.Title); - + if (!_diskProvider.FolderExists(series.Path)) { - if (_configService.CreateEmptySeriesFolders && - _diskProvider.FolderExists(rootFolder)) + if (_configService.CreateEmptySeriesFolders) { _logger.Debug("Creating missing series folder: {0}", series.Path); _diskProvider.CreateFolder(series.Path); @@ -96,27 +95,35 @@ public void Scan(Series series) { _logger.Debug("Series folder doesn't exist: {0}", series.Path); } - - _eventAggregator.PublishEvent(new SeriesScanSkippedEvent(series, SeriesScanSkippedReason.SeriesFolderDoesNotExist)); + CleanMediaFiles(series, new List()); + CompletedScanning(series); return; } var videoFilesStopwatch = Stopwatch.StartNew(); var mediaFileList = FilterFiles(series, GetVideoFiles(series.Path)).ToList(); - videoFilesStopwatch.Stop(); _logger.Trace("Finished getting episode files for: {0} [{1}]", series, videoFilesStopwatch.Elapsed); - _logger.Debug("{0} Cleaning up media files in DB", series); - _mediaFileTableCleanupService.Clean(series, mediaFileList); - + CleanMediaFiles(series, mediaFileList); + var decisionsStopwatch = Stopwatch.StartNew(); var decisions = _importDecisionMaker.GetImportDecisions(mediaFileList, series); decisionsStopwatch.Stop(); _logger.Trace("Import decisions complete for: {0} [{1}]", series, decisionsStopwatch.Elapsed); - _importApprovedEpisodes.Import(decisions, false); + CompletedScanning(series); + } + + private void CleanMediaFiles(Series series, List mediaFileList) + { + _logger.Debug("{0} Cleaning up media files in DB", series); + _mediaFileTableCleanupService.Clean(series, mediaFileList); + } + + private void CompletedScanning(Series series) + { _logger.Info("Completed scanning disk for {0}", series.Title); _eventAggregator.PublishEvent(new SeriesScannedEvent(series)); } diff --git a/src/NzbDrone.Core/MediaFiles/Events/SeriesScanSkippedEvent.cs b/src/NzbDrone.Core/MediaFiles/Events/SeriesScanSkippedEvent.cs index 47e8976c5..765207bd5 100644 --- a/src/NzbDrone.Core/MediaFiles/Events/SeriesScanSkippedEvent.cs +++ b/src/NzbDrone.Core/MediaFiles/Events/SeriesScanSkippedEvent.cs @@ -18,7 +18,6 @@ public SeriesScanSkippedEvent(Series series, SeriesScanSkippedReason reason) public enum SeriesScanSkippedReason { RootFolderDoesNotExist, - RootFolderIsEmpty, - SeriesFolderDoesNotExist + RootFolderIsEmpty } }