From 95f66117e4546f66806b1f561fd49dc1385324fc Mon Sep 17 00:00:00 2001 From: Qstick Date: Sun, 10 May 2020 12:34:16 -0400 Subject: [PATCH] Fixed: DownloadedMovieScan API should delete source folder if ImportMode is Move --- .../DownloadedMoviesImportServiceFixture.cs | 87 +++++++++++++++++++ .../DownloadedMovieImportService.cs | 7 +- 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/src/NzbDrone.Core.Test/MediaFiles/DownloadedMoviesImportServiceFixture.cs b/src/NzbDrone.Core.Test/MediaFiles/DownloadedMoviesImportServiceFixture.cs index ae746b347..31c3afe38 100644 --- a/src/NzbDrone.Core.Test/MediaFiles/DownloadedMoviesImportServiceFixture.cs +++ b/src/NzbDrone.Core.Test/MediaFiles/DownloadedMoviesImportServiceFixture.cs @@ -7,6 +7,7 @@ using Moq; using NUnit.Framework; using NzbDrone.Common.Disk; using NzbDrone.Core.Download; +using NzbDrone.Core.Download.TrackedDownloads; using NzbDrone.Core.MediaFiles; using NzbDrone.Core.MediaFiles.MovieImport; using NzbDrone.Core.Movies; @@ -24,6 +25,8 @@ namespace NzbDrone.Core.Test.MediaFiles private string[] _subFolders = new[] { "c:\\root\\foldername".AsOsAgnostic() }; private string[] _videoFiles = new[] { "c:\\root\\foldername\\47.ronin.2013.ext".AsOsAgnostic() }; + private TrackedDownload _trackedDownload; + [SetUp] public void Setup() { @@ -45,6 +48,22 @@ namespace NzbDrone.Core.Test.MediaFiles Mocker.GetMock() .Setup(s => s.Import(It.IsAny>(), true, null, ImportMode.Auto)) .Returns(new List()); + + var downloadItem = Builder.CreateNew() + .With(v => v.DownloadId = "sab1") + .With(v => v.Status = DownloadItemStatus.Downloading) + .Build(); + + var remoteMovie = Builder.CreateNew() + .With(v => v.Movie = new Movie()) + .Build(); + + _trackedDownload = new TrackedDownload + { + DownloadItem = downloadItem, + RemoteMovie = remoteMovie, + State = TrackedDownloadState.Downloading + }; } private void GivenValidMovie() @@ -54,6 +73,29 @@ namespace NzbDrone.Core.Test.MediaFiles .Returns(Builder.CreateNew().Build()); } + private void GivenSuccessfulImport() + { + var localMovie = new LocalMovie(); + + var imported = new List(); + imported.Add(new ImportDecision(localMovie)); + + Mocker.GetMock() + .Setup(s => s.GetImportDecisions(It.IsAny>(), It.IsAny(), It.IsAny(), null, true, true)) + .Returns(imported); + + Mocker.GetMock() + .Setup(s => s.Import(It.IsAny>(), It.IsAny(), It.IsAny(), It.IsAny())) + .Returns(imported.Select(i => new ImportResult(i)).ToList()) + .Callback(() => WasImportedResponse()); + } + + private void WasImportedResponse() + { + Mocker.GetMock().Setup(c => c.GetVideoFiles(It.IsAny(), It.IsAny())) + .Returns(new string[0]); + } + [Test] public void should_search_for_series_using_folder_name() { @@ -123,6 +165,51 @@ namespace NzbDrone.Core.Test.MediaFiles .Verify(v => v.GetFolderSize(It.IsAny()), Times.Never()); } + [Test] + public void should_not_delete_folder_after_import() + { + GivenValidMovie(); + + GivenSuccessfulImport(); + + _trackedDownload.DownloadItem.CanMoveFiles = false; + + Subject.ProcessPath(_droneFactory, ImportMode.Auto, _trackedDownload.RemoteMovie.Movie, _trackedDownload.DownloadItem); + + Mocker.GetMock() + .Verify(v => v.DeleteFolder(It.IsAny(), true), Times.Never()); + } + + [Test] + public void should_delete_folder_if_importmode_move() + { + GivenValidMovie(); + + GivenSuccessfulImport(); + + _trackedDownload.DownloadItem.CanMoveFiles = false; + + Subject.ProcessPath(_droneFactory, ImportMode.Move, _trackedDownload.RemoteMovie.Movie, _trackedDownload.DownloadItem); + + Mocker.GetMock() + .Verify(v => v.DeleteFolder(It.IsAny(), true), Times.Once()); + } + + [Test] + public void should_not_delete_folder_if_importmode_copy() + { + GivenValidMovie(); + + GivenSuccessfulImport(); + + _trackedDownload.DownloadItem.CanMoveFiles = true; + + Subject.ProcessPath(_droneFactory, ImportMode.Copy, _trackedDownload.RemoteMovie.Movie, _trackedDownload.DownloadItem); + + Mocker.GetMock() + .Verify(v => v.DeleteFolder(It.IsAny(), true), Times.Never()); + } + [Test] public void should_not_delete_folder_if_files_were_imported_and_video_files_remain() { diff --git a/src/NzbDrone.Core/MediaFiles/DownloadedMovieImportService.cs b/src/NzbDrone.Core/MediaFiles/DownloadedMovieImportService.cs index adf464edb..64baf15ee 100644 --- a/src/NzbDrone.Core/MediaFiles/DownloadedMovieImportService.cs +++ b/src/NzbDrone.Core/MediaFiles/DownloadedMovieImportService.cs @@ -208,7 +208,12 @@ namespace NzbDrone.Core.MediaFiles var decisions = _importDecisionMaker.GetImportDecisions(videoFiles.ToList(), movie, downloadClientItem, folderInfo, true); var importResults = _importApprovedMovie.Import(decisions, true, downloadClientItem, importMode); - if ((downloadClientItem == null || downloadClientItem.CanBeRemoved) && + if (importMode == ImportMode.Auto) + { + importMode = (downloadClientItem == null || downloadClientItem.CanMoveFiles) ? ImportMode.Move : ImportMode.Copy; + } + + if (importMode == ImportMode.Move && importResults.Any(i => i.Result == ImportResultType.Imported) && ShouldDeleteFolder(directoryInfo, movie)) {