diff --git a/src/NzbDrone.Core.Test/Download/DownloadClientTests/Blackhole/TorrentBlackholeFixture.cs b/src/NzbDrone.Core.Test/Download/DownloadClientTests/Blackhole/TorrentBlackholeFixture.cs index dafda04fb..a5c4f02c0 100644 --- a/src/NzbDrone.Core.Test/Download/DownloadClientTests/Blackhole/TorrentBlackholeFixture.cs +++ b/src/NzbDrone.Core.Test/Download/DownloadClientTests/Blackhole/TorrentBlackholeFixture.cs @@ -151,6 +151,10 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole { GivenCompletedItem(); + Mocker.GetMock() + .Setup(c => c.FolderExists(It.IsAny())) + .Returns(true); + Subject.RemoveItem("_Droned.S01E01.Pilot.1080p.WEB-DL-DRONE_0", true); Mocker.GetMock() @@ -158,9 +162,9 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole } [Test] - public void RemoveItem_should_throw_if_unknown_item() + public void RemoveItem_should_ignore_if_unknown_item() { - Assert.Throws(() => Subject.RemoveItem("_Droned.S01E01.Pilot.1080p.WEB-DL-DRONE_0", true)); + Subject.RemoveItem("_Droned.S01E01.Pilot.1080p.WEB-DL-DRONE_0", true); Mocker.GetMock() .Verify(c => c.DeleteFile(It.IsAny()), Times.Never()); diff --git a/src/NzbDrone.Core.Test/Download/DownloadClientTests/Blackhole/UsenetBlackholeFixture.cs b/src/NzbDrone.Core.Test/Download/DownloadClientTests/Blackhole/UsenetBlackholeFixture.cs index d70a43bc8..3dc4a93f9 100644 --- a/src/NzbDrone.Core.Test/Download/DownloadClientTests/Blackhole/UsenetBlackholeFixture.cs +++ b/src/NzbDrone.Core.Test/Download/DownloadClientTests/Blackhole/UsenetBlackholeFixture.cs @@ -148,6 +148,10 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole { GivenCompletedItem(); + Mocker.GetMock() + .Setup(c => c.FolderExists(It.IsAny())) + .Returns(true); + Subject.RemoveItem("_Droned.S01E01.Pilot.1080p.WEB-DL-DRONE_0", true); Mocker.GetMock() @@ -155,9 +159,9 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole } [Test] - public void RemoveItem_should_throw_if_unknown_item() + public void RemoveItem_should_ignore_if_unknown_item() { - Assert.Throws(() => Subject.RemoveItem("_Droned.S01E01.Pilot.1080p.WEB-DL-DRONE_0", true)); + Subject.RemoveItem("_Droned.S01E01.Pilot.1080p.WEB-DL-DRONE_0", true); Mocker.GetMock() .Verify(c => c.DeleteFile(It.IsAny()), Times.Never()); diff --git a/src/NzbDrone.Core.Test/Download/DownloadClientTests/NzbgetTests/NzbgetFixture.cs b/src/NzbDrone.Core.Test/Download/DownloadClientTests/NzbgetTests/NzbgetFixture.cs index d65350e62..4b176ad73 100644 --- a/src/NzbDrone.Core.Test/Download/DownloadClientTests/NzbgetTests/NzbgetFixture.cs +++ b/src/NzbDrone.Core.Test/Download/DownloadClientTests/NzbgetTests/NzbgetFixture.cs @@ -140,6 +140,22 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.NzbgetTests Subject.GetItems().Should().BeEmpty(); } + [Test] + public void RemoveItem_should_delete_folder() + { + GivenQueue(null); + GivenHistory(_completed); + + Mocker.GetMock() + .Setup(v => v.FolderExists(It.IsAny())) + .Returns(true); + + Subject.RemoveItem("id", true); + + Mocker.GetMock() + .Verify(v => v.DeleteFolder(It.IsAny(), true), Times.Once()); + } + [Test] public void queued_item_should_have_required_properties() { diff --git a/src/NzbDrone.Core/Download/Clients/Nzbget/Nzbget.cs b/src/NzbDrone.Core/Download/Clients/Nzbget/Nzbget.cs index 1048c13bc..1730de4b3 100644 --- a/src/NzbDrone.Core/Download/Clients/Nzbget/Nzbget.cs +++ b/src/NzbDrone.Core/Download/Clients/Nzbget/Nzbget.cs @@ -202,6 +202,11 @@ namespace NzbDrone.Core.Download.Clients.Nzbget public override void RemoveItem(string downloadId, bool deleteData) { + if (deleteData) + { + DeleteItemData(downloadId); + } + _proxy.RemoveItem(downloadId, Settings); } diff --git a/src/NzbDrone.Core/Download/Clients/TorrentBlackhole/TorrentBlackhole.cs b/src/NzbDrone.Core/Download/Clients/TorrentBlackhole/TorrentBlackhole.cs index f386addc6..d838487fd 100644 --- a/src/NzbDrone.Core/Download/Clients/TorrentBlackhole/TorrentBlackhole.cs +++ b/src/NzbDrone.Core/Download/Clients/TorrentBlackhole/TorrentBlackhole.cs @@ -129,28 +129,12 @@ namespace NzbDrone.Core.Download.Clients.TorrentBlackhole public override void RemoveItem(string downloadId, bool deleteData) { - var downloadItem = GetItems().FirstOrDefault(v => v.DownloadId == downloadId); - - if (downloadItem == null) - { - throw new ArgumentException(string.Format("Cannot remove DownloadItem {0} because it was not found.", downloadId)); - } - if (!deleteData) { throw new NotSupportedException("Blackhole cannot remove DownloadItem without deleting the data as well, ignoring."); } - var outputPath = downloadItem.OutputPath.FullPath; - - if (_diskProvider.FileExists(outputPath)) - { - _diskProvider.DeleteFile(outputPath); - } - else - { - _diskProvider.DeleteFolder(outputPath, true); - } + DeleteItemData(downloadId); } public override DownloadClientStatus GetStatus() diff --git a/src/NzbDrone.Core/Download/Clients/UsenetBlackhole/UsenetBlackhole.cs b/src/NzbDrone.Core/Download/Clients/UsenetBlackhole/UsenetBlackhole.cs index ed765d52a..3d7ac5b0d 100644 --- a/src/NzbDrone.Core/Download/Clients/UsenetBlackhole/UsenetBlackhole.cs +++ b/src/NzbDrone.Core/Download/Clients/UsenetBlackhole/UsenetBlackhole.cs @@ -122,28 +122,12 @@ namespace NzbDrone.Core.Download.Clients.UsenetBlackhole public override void RemoveItem(string downloadId, bool deleteData) { - var downloadItem = GetItems().FirstOrDefault(v => v.DownloadId == downloadId); - - if (downloadItem == null) - { - throw new ArgumentException(string.Format("Cannot remove DownloadItem {0} because it was not found.", downloadId)); - } - if (!deleteData) { throw new NotSupportedException("Blackhole cannot remove DownloadItem without deleting the data as well, ignoring."); } - var outputPath = downloadItem.OutputPath.FullPath; - - if (_diskProvider.FileExists(outputPath)) - { - _diskProvider.DeleteFile(outputPath); - } - else - { - _diskProvider.DeleteFolder(outputPath, true); - } + DeleteItemData(downloadId); } public override DownloadClientStatus GetStatus() diff --git a/src/NzbDrone.Core/Download/DownloadClientBase.cs b/src/NzbDrone.Core/Download/DownloadClientBase.cs index 95eb6c895..5ef4ec582 100644 --- a/src/NzbDrone.Core/Download/DownloadClientBase.cs +++ b/src/NzbDrone.Core/Download/DownloadClientBase.cs @@ -1,14 +1,16 @@ using System; +using System.Linq; using System.Collections.Generic; +using FluentValidation.Results; +using NLog; using NzbDrone.Common.Disk; +using NzbDrone.Common.Extensions; +using NzbDrone.Core.Configuration; using NzbDrone.Core.Indexers; using NzbDrone.Core.Parser.Model; +using NzbDrone.Core.RemotePathMappings; using NzbDrone.Core.ThingiProvider; -using NzbDrone.Core.Configuration; -using NLog; -using FluentValidation.Results; using NzbDrone.Core.Validation; -using NzbDrone.Core.RemotePathMappings; namespace NzbDrone.Core.Download { @@ -74,6 +76,51 @@ namespace NzbDrone.Core.Download public abstract void RemoveItem(string downloadId, bool deleteData); public abstract DownloadClientStatus GetStatus(); + protected virtual void DeleteItemData(string downloadId) + { + if (downloadId.IsNullOrWhiteSpace()) + { + return; + } + + var item = GetItems().FirstOrDefault(v => v.DownloadId == downloadId); + if (item == null) + { + _logger.Trace("DownloadItem {0} in {1} history not found, skipping delete data.", downloadId, Name); + return; + } + + if (item.OutputPath == null) + { + _logger.Trace("[{0}] Doesn't have an outputPath, skipping delete data.", item.Title); + return; + } + + try + { + if (_diskProvider.FolderExists(item.OutputPath.FullPath)) + { + _logger.Debug("[{0}] Deleting folder '{1}'.", item.Title, item.OutputPath); + + _diskProvider.DeleteFolder(item.OutputPath.FullPath, true); + } + else if (_diskProvider.FileExists(item.OutputPath.FullPath)) + { + _logger.Debug("[{0}] Deleting file '{1}'.", item.Title, item.OutputPath); + + _diskProvider.DeleteFile(item.OutputPath.FullPath); + } + else + { + _logger.Trace("[{0}] File or folder '{1}' doesn't exist, skipping cleanup.", item.Title, item.OutputPath); + } + } + catch (Exception ex) + { + _logger.WarnException(string.Format("[{0}] Error occurred while trying to delete data from '{1}'.", item.Title, item.OutputPath), ex); + } + } + public ValidationResult Test() { var failures = new List();