From 4bc0ffa74d06659f0658b1a901a6ae3765aca921 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Fri, 11 Jan 2019 19:34:35 -0800 Subject: [PATCH] Improve renaming of series folder within the same root folder --- src/NzbDrone.Common/Disk/DiskProviderBase.cs | 8 ++++++++ src/NzbDrone.Common/Disk/IDiskProvider.cs | 1 + src/NzbDrone.Core/Tv/MoveSeriesService.cs | 18 +++++++++++++++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/NzbDrone.Common/Disk/DiskProviderBase.cs b/src/NzbDrone.Common/Disk/DiskProviderBase.cs index 916bcceb9..eb9fa82c0 100644 --- a/src/NzbDrone.Common/Disk/DiskProviderBase.cs +++ b/src/NzbDrone.Common/Disk/DiskProviderBase.cs @@ -227,6 +227,14 @@ namespace NzbDrone.Common.Disk MoveFileInternal(source, destination); } + public void MoveFolder(string source, string destination) + { + Ensure.That(source, () => source).IsValidPath(); + Ensure.That(destination, () => destination).IsValidPath(); + + Directory.Move(source, destination); + } + protected virtual void MoveFileInternal(string source, string destination) { File.Move(source, destination); diff --git a/src/NzbDrone.Common/Disk/IDiskProvider.cs b/src/NzbDrone.Common/Disk/IDiskProvider.cs index f98529ead..20f36c67d 100644 --- a/src/NzbDrone.Common/Disk/IDiskProvider.cs +++ b/src/NzbDrone.Common/Disk/IDiskProvider.cs @@ -28,6 +28,7 @@ namespace NzbDrone.Common.Disk void DeleteFile(string path); void CopyFile(string source, string destination, bool overwrite = false); void MoveFile(string source, string destination, bool overwrite = false); + void MoveFolder(string source, string destination); bool TryCreateHardLink(string source, string destination); void DeleteFolder(string path, bool recursive); string ReadAllText(string filePath); diff --git a/src/NzbDrone.Core/Tv/MoveSeriesService.cs b/src/NzbDrone.Core/Tv/MoveSeriesService.cs index 75769f7ec..6797a1529 100644 --- a/src/NzbDrone.Core/Tv/MoveSeriesService.cs +++ b/src/NzbDrone.Core/Tv/MoveSeriesService.cs @@ -1,10 +1,12 @@ using System.IO; using NLog; using NzbDrone.Common.Disk; +using NzbDrone.Common.Extensions; using NzbDrone.Common.Instrumentation.Extensions; using NzbDrone.Core.Messaging.Commands; using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.Organizer; +using NzbDrone.Core.RootFolders; using NzbDrone.Core.Tv.Commands; using NzbDrone.Core.Tv.Events; @@ -16,6 +18,7 @@ namespace NzbDrone.Core.Tv private readonly IBuildFileNames _filenameBuilder; private readonly IDiskProvider _diskProvider; private readonly IDiskTransferService _diskTransferService; + private readonly IRootFolderService _rootFolderService; private readonly IEventAggregator _eventAggregator; private readonly Logger _logger; @@ -23,6 +26,7 @@ namespace NzbDrone.Core.Tv IBuildFileNames filenameBuilder, IDiskProvider diskProvider, IDiskTransferService diskTransferService, + IRootFolderService rootFolderService, IEventAggregator eventAggregator, Logger logger) { @@ -30,6 +34,7 @@ namespace NzbDrone.Core.Tv _filenameBuilder = filenameBuilder; _diskProvider = diskProvider; _diskTransferService = diskTransferService; + _rootFolderService = rootFolderService; _eventAggregator = eventAggregator; _logger = logger; } @@ -53,7 +58,18 @@ namespace NzbDrone.Core.Tv try { - _diskTransferService.TransferFolder(sourcePath, destinationPath, TransferMode.Move); + var sourceRootFolder = _rootFolderService.GetBestRootFolderPath(sourcePath); + var destinationRootFolder = _rootFolderService.GetBestRootFolderPath(destinationPath); + + if (sourceRootFolder.PathEquals(destinationRootFolder) && !_diskProvider.FolderExists(destinationPath)) + { + _diskProvider.MoveFolder(sourcePath, destinationPath); + } + else + { + _diskTransferService.TransferFolder(sourcePath, destinationPath, TransferMode.Move); + } + _logger.ProgressInfo("{0} moved successfully to {1}", series.Title, series.Path); _eventAggregator.PublishEvent(new SeriesMovedEvent(series, sourcePath, destinationPath));