From 16fcba02ba8151d7237d829c8bda6ff656cd5f2c Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Wed, 30 Dec 2020 14:01:01 -0800 Subject: [PATCH] Fixed: Update path before importing to ensure it hasn't changed Closes #684 --- .../ImportFixture.cs | 5 +- .../Download/CompletedDownloadService.cs | 52 +++++++++++++------ ...Service.cs => ProvideImportItemService.cs} | 0 3 files changed, 41 insertions(+), 16 deletions(-) rename src/NzbDrone.Core/Download/{PrepareImportService.cs => ProvideImportItemService.cs} (100%) diff --git a/src/NzbDrone.Core.Test/Download/CompletedDownloadServiceTests/ImportFixture.cs b/src/NzbDrone.Core.Test/Download/CompletedDownloadServiceTests/ImportFixture.cs index 74e030a8e..e4c3764d2 100644 --- a/src/NzbDrone.Core.Test/Download/CompletedDownloadServiceTests/ImportFixture.cs +++ b/src/NzbDrone.Core.Test/Download/CompletedDownloadServiceTests/ImportFixture.cs @@ -38,7 +38,6 @@ namespace NzbDrone.Core.Test.Download.CompletedDownloadServiceTests _trackedDownload = Builder.CreateNew() .With(c => c.State = TrackedDownloadState.Downloading) - .With(c => c.ImportItem = completed) .With(c => c.DownloadItem = completed) .With(c => c.RemoteBook = remoteBook) .Build(); @@ -65,6 +64,10 @@ namespace NzbDrone.Core.Test.Download.CompletedDownloadServiceTests Mocker.GetMock() .Setup(s => s.FindByDownloadId(It.IsAny())) .Returns(new List()); + + Mocker.GetMock() + .Setup(s => s.ProvideImportItem(It.IsAny(), It.IsAny())) + .Returns((i, p) => i); } private Book CreateBook(int id) diff --git a/src/NzbDrone.Core/Download/CompletedDownloadService.cs b/src/NzbDrone.Core/Download/CompletedDownloadService.cs index 0f947ccb6..b2078a96c 100644 --- a/src/NzbDrone.Core/Download/CompletedDownloadService.cs +++ b/src/NzbDrone.Core/Download/CompletedDownloadService.cs @@ -27,21 +27,21 @@ namespace NzbDrone.Core.Download { private readonly IEventAggregator _eventAggregator; private readonly IHistoryService _historyService; + private readonly IProvideImportItemService _provideImportItemService; private readonly IDownloadedBooksImportService _downloadedTracksImportService; - private readonly IProvideImportItemService _importItemService; private readonly ITrackedDownloadAlreadyImported _trackedDownloadAlreadyImported; private readonly Logger _logger; public CompletedDownloadService(IEventAggregator eventAggregator, IHistoryService historyService, - IProvideImportItemService importItemService, + IProvideImportItemService provideImportItemService, IDownloadedBooksImportService downloadedTracksImportService, ITrackedDownloadAlreadyImported trackedDownloadAlreadyImported, Logger logger) { _eventAggregator = eventAggregator; _historyService = historyService; - _importItemService = importItemService; + _provideImportItemService = provideImportItemService; _downloadedTracksImportService = downloadedTracksImportService; _trackedDownloadAlreadyImported = trackedDownloadAlreadyImported; _logger = logger; @@ -54,7 +54,7 @@ namespace NzbDrone.Core.Download return; } - trackedDownload.ImportItem = _importItemService.ProvideImportItem(trackedDownload.DownloadItem, trackedDownload.ImportItem); + SetImportItem(trackedDownload); // Only process tracked downloads that are still downloading if (trackedDownload.State != TrackedDownloadState.Downloading) @@ -70,18 +70,8 @@ namespace NzbDrone.Core.Download return; } - var downloadItemOutputPath = trackedDownload.ImportItem.OutputPath; - - if (downloadItemOutputPath.IsEmpty) - { - trackedDownload.Warn("Download doesn't contain intermediate path, Skipping."); - return; - } - - if ((OsInfo.IsWindows && !downloadItemOutputPath.IsWindowsPath) || - (OsInfo.IsNotWindows && !downloadItemOutputPath.IsUnixPath)) + if (!ValidatePath(trackedDownload)) { - trackedDownload.Warn("[{0}] is not a valid local path. You may need a Remote Path Mapping.", downloadItemOutputPath); return; } @@ -90,6 +80,13 @@ namespace NzbDrone.Core.Download public void Import(TrackedDownload trackedDownload) { + SetImportItem(trackedDownload); + + if (!ValidatePath(trackedDownload)) + { + return; + } + trackedDownload.State = TrackedDownloadState.Importing; var outputPath = trackedDownload.ImportItem.OutputPath.FullPath; @@ -190,5 +187,30 @@ namespace NzbDrone.Core.Download _logger.Debug("Not all books have been imported for {0}", trackedDownload.DownloadItem.Title); return false; } + + private void SetImportItem(TrackedDownload trackedDownload) + { + trackedDownload.ImportItem = _provideImportItemService.ProvideImportItem(trackedDownload.DownloadItem, trackedDownload.ImportItem); + } + + private bool ValidatePath(TrackedDownload trackedDownload) + { + var downloadItemOutputPath = trackedDownload.ImportItem.OutputPath; + + if (downloadItemOutputPath.IsEmpty) + { + trackedDownload.Warn("Download doesn't contain intermediate path, Skipping."); + return false; + } + + if ((OsInfo.IsWindows && !downloadItemOutputPath.IsWindowsPath) || + (OsInfo.IsNotWindows && !downloadItemOutputPath.IsUnixPath)) + { + trackedDownload.Warn("[{0}] is not a valid local path. You may need a Remote Path Mapping.", downloadItemOutputPath); + return false; + } + + return true; + } } } diff --git a/src/NzbDrone.Core/Download/PrepareImportService.cs b/src/NzbDrone.Core/Download/ProvideImportItemService.cs similarity index 100% rename from src/NzbDrone.Core/Download/PrepareImportService.cs rename to src/NzbDrone.Core/Download/ProvideImportItemService.cs