From 366f8c523958bcec95aecef7b95cf48482352e85 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Mon, 19 Jan 2015 23:25:04 +0100 Subject: [PATCH] Fixed: Remove on Activity page should now work for Blackhole items. --- .../Blackhole/TorrentBlackholeFixture.cs | 52 ++++++++++++++++++ .../Blackhole/UsenetBlackholeFixture.cs | 54 +++++++++++++++++++ .../TorrentBlackhole/TorrentBlackhole.cs | 23 +++++++- .../UsenetBlackhole/UsenetBlackhole.cs | 23 +++++++- 4 files changed, 150 insertions(+), 2 deletions(-) diff --git a/src/NzbDrone.Core.Test/Download/DownloadClientTests/Blackhole/TorrentBlackholeFixture.cs b/src/NzbDrone.Core.Test/Download/DownloadClientTests/Blackhole/TorrentBlackholeFixture.cs index ee52bb84e..2685c9613 100644 --- a/src/NzbDrone.Core.Test/Download/DownloadClientTests/Blackhole/TorrentBlackholeFixture.cs +++ b/src/NzbDrone.Core.Test/Download/DownloadClientTests/Blackhole/TorrentBlackholeFixture.cs @@ -118,6 +118,58 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole items.First().Status.Should().Be(DownloadItemStatus.Downloading); } + [Test] + public void RemoveItem_should_delete_file() + { + GivenCompletedItem(); + + Mocker.GetMock() + .Setup(c => c.FileExists(It.IsAny())) + .Returns(true); + + Subject.RemoveItem("_Droned.S01E01.Pilot.1080p.WEB-DL-DRONE_0", true); + + Mocker.GetMock() + .Verify(c => c.DeleteFile(It.IsAny()), Times.Once()); + } + + [Test] + public void RemoveItem_should_delete_directory() + { + GivenCompletedItem(); + + Subject.RemoveItem("_Droned.S01E01.Pilot.1080p.WEB-DL-DRONE_0", true); + + Mocker.GetMock() + .Verify(c => c.DeleteFolder(It.IsAny(), true), Times.Once()); + } + + [Test] + public void RemoveItem_should_throw_if_unknown_item() + { + Assert.Throws(() => Subject.RemoveItem("_Droned.S01E01.Pilot.1080p.WEB-DL-DRONE_0", true)); + + Mocker.GetMock() + .Verify(c => c.DeleteFile(It.IsAny()), Times.Never()); + + Mocker.GetMock() + .Verify(c => c.DeleteFolder(It.IsAny(), true), Times.Never()); + } + + [Test] + public void RemoveItem_should_throw_if_deleteData_is_false() + { + GivenCompletedItem(); + + Assert.Throws(() => Subject.RemoveItem("_Droned.S01E01.Pilot.1080p.WEB-DL-DRONE_0", false)); + + Mocker.GetMock() + .Verify(c => c.DeleteFile(It.IsAny()), Times.Never()); + + Mocker.GetMock() + .Verify(c => c.DeleteFolder(It.IsAny(), true), Times.Never()); + } + [Test] public void should_return_status_with_outputdirs() { diff --git a/src/NzbDrone.Core.Test/Download/DownloadClientTests/Blackhole/UsenetBlackholeFixture.cs b/src/NzbDrone.Core.Test/Download/DownloadClientTests/Blackhole/UsenetBlackholeFixture.cs index 4521e157b..06635874f 100644 --- a/src/NzbDrone.Core.Test/Download/DownloadClientTests/Blackhole/UsenetBlackholeFixture.cs +++ b/src/NzbDrone.Core.Test/Download/DownloadClientTests/Blackhole/UsenetBlackholeFixture.cs @@ -1,3 +1,5 @@ + +using System; using System.IO; using System.Net; using System.Linq; @@ -118,6 +120,58 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole result.Status.Should().Be(DownloadItemStatus.Downloading); } + [Test] + public void RemoveItem_should_delete_file() + { + GivenCompletedItem(); + + Mocker.GetMock() + .Setup(c => c.FileExists(It.IsAny())) + .Returns(true); + + Subject.RemoveItem("_Droned.S01E01.Pilot.1080p.WEB-DL-DRONE_0", true); + + Mocker.GetMock() + .Verify(c => c.DeleteFile(It.IsAny()), Times.Once()); + } + + [Test] + public void RemoveItem_should_delete_directory() + { + GivenCompletedItem(); + + Subject.RemoveItem("_Droned.S01E01.Pilot.1080p.WEB-DL-DRONE_0", true); + + Mocker.GetMock() + .Verify(c => c.DeleteFolder(It.IsAny(), true), Times.Once()); + } + + [Test] + public void RemoveItem_should_throw_if_unknown_item() + { + Assert.Throws(() => Subject.RemoveItem("_Droned.S01E01.Pilot.1080p.WEB-DL-DRONE_0", true)); + + Mocker.GetMock() + .Verify(c => c.DeleteFile(It.IsAny()), Times.Never()); + + Mocker.GetMock() + .Verify(c => c.DeleteFolder(It.IsAny(), true), Times.Never()); + } + + [Test] + public void RemoveItem_should_throw_if_deleteData_is_false() + { + GivenCompletedItem(); + + Assert.Throws(() => Subject.RemoveItem("_Droned.S01E01.Pilot.1080p.WEB-DL-DRONE_0", false)); + + Mocker.GetMock() + .Verify(c => c.DeleteFile(It.IsAny()), Times.Never()); + + Mocker.GetMock() + .Verify(c => c.DeleteFolder(It.IsAny(), true), Times.Never()); + } + [Test] public void should_return_status_with_outputdirs() { diff --git a/src/NzbDrone.Core/Download/Clients/TorrentBlackhole/TorrentBlackhole.cs b/src/NzbDrone.Core/Download/Clients/TorrentBlackhole/TorrentBlackhole.cs index c65608253..2622f694e 100644 --- a/src/NzbDrone.Core/Download/Clients/TorrentBlackhole/TorrentBlackhole.cs +++ b/src/NzbDrone.Core/Download/Clients/TorrentBlackhole/TorrentBlackhole.cs @@ -125,7 +125,28 @@ namespace NzbDrone.Core.Download.Clients.TorrentBlackhole public override void RemoveItem(string downloadId, bool deleteData) { - throw new NotSupportedException(); + 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); + } } 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 c18981eca..1af5bedda 100644 --- a/src/NzbDrone.Core/Download/Clients/UsenetBlackhole/UsenetBlackhole.cs +++ b/src/NzbDrone.Core/Download/Clients/UsenetBlackhole/UsenetBlackhole.cs @@ -123,7 +123,28 @@ namespace NzbDrone.Core.Download.Clients.UsenetBlackhole public override void RemoveItem(string downloadId, bool deleteData) { - throw new NotSupportedException(); + 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); + } } public override DownloadClientStatus GetStatus()