diff --git a/src/NzbDrone.Core.Test/Download/DownloadClientTests/Blackhole/TorrentBlackholeFixture.cs b/src/NzbDrone.Core.Test/Download/DownloadClientTests/Blackhole/TorrentBlackholeFixture.cs index 2685c9613..dafda04fb 100644 --- a/src/NzbDrone.Core.Test/Download/DownloadClientTests/Blackhole/TorrentBlackholeFixture.cs +++ b/src/NzbDrone.Core.Test/Download/DownloadClientTests/Blackhole/TorrentBlackholeFixture.cs @@ -1,15 +1,16 @@ +using System; using System.IO; -using System.Net; using System.Linq; +using System.Net; +using FluentAssertions; using Moq; using NUnit.Framework; -using FluentAssertions; -using NzbDrone.Test.Common; using NzbDrone.Common.Disk; using NzbDrone.Common.Http; using NzbDrone.Core.Download; using NzbDrone.Core.Download.Clients.TorrentBlackhole; -using System; +using NzbDrone.Core.MediaFiles.TorrentInfo; +using NzbDrone.Test.Common; namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole { @@ -33,6 +34,14 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole TorrentFolder = _blackholeFolder, WatchFolder = _completedDownloadFolder }; + + Mocker.GetMock() + .Setup(c => c.OpenWriteStream(It.IsAny())) + .Returns(() => new FileStream(GetTempFilePath(), FileMode.Create)); + + Mocker.GetMock() + .Setup(c => c.GetHashFromTorrentFile(It.IsAny())) + .Returns("myhash"); } protected void GivenFailedDownload() @@ -86,7 +95,9 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole Subject.Download(remoteEpisode); - Mocker.GetMock().Verify(c => c.DownloadFile(_downloadUrl, _filePath), Times.Once()); + Mocker.GetMock().Verify(c => c.Get(It.Is(v => v.Url.ToString() == _downloadUrl)), Times.Once()); + Mocker.GetMock().Verify(c => c.OpenWriteStream(_filePath), Times.Once()); + Mocker.GetMock().Verify(c => c.DownloadFile(It.IsAny(), It.IsAny()), Times.Never()); } [Test] @@ -100,7 +111,9 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole Subject.Download(remoteEpisode); - Mocker.GetMock().Verify(c => c.DownloadFile(It.IsAny(), expectedFilename), Times.Once()); + Mocker.GetMock().Verify(c => c.Get(It.Is(v => v.Url.ToString() == _downloadUrl)), Times.Once()); + Mocker.GetMock().Verify(c => c.OpenWriteStream(expectedFilename), Times.Once()); + Mocker.GetMock().Verify(c => c.DownloadFile(It.IsAny(), It.IsAny()), Times.Never()); } [Test] diff --git a/src/NzbDrone.Core.Test/Download/DownloadClientTests/Blackhole/UsenetBlackholeFixture.cs b/src/NzbDrone.Core.Test/Download/DownloadClientTests/Blackhole/UsenetBlackholeFixture.cs index 06635874f..d70a43bc8 100644 --- a/src/NzbDrone.Core.Test/Download/DownloadClientTests/Blackhole/UsenetBlackholeFixture.cs +++ b/src/NzbDrone.Core.Test/Download/DownloadClientTests/Blackhole/UsenetBlackholeFixture.cs @@ -1,16 +1,16 @@ using System; using System.IO; -using System.Net; using System.Linq; +using System.Net; +using FluentAssertions; using Moq; using NUnit.Framework; -using FluentAssertions; -using NzbDrone.Test.Common; using NzbDrone.Common.Disk; using NzbDrone.Common.Http; using NzbDrone.Core.Download; using NzbDrone.Core.Download.Clients.UsenetBlackhole; +using NzbDrone.Test.Common; namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole { @@ -35,12 +35,16 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole NzbFolder = _blackholeFolder, WatchFolder = _completedDownloadFolder }; + + Mocker.GetMock() + .Setup(c => c.OpenWriteStream(It.IsAny())) + .Returns(() => new FileStream(GetTempFilePath(), FileMode.Create)); } protected void GivenFailedDownload() { Mocker.GetMock() - .Setup(c => c.DownloadFile(It.IsAny(), It.IsAny())) + .Setup(c => c.Get(It.IsAny())) .Throws(new WebException()); } @@ -89,7 +93,9 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole Subject.Download(remoteEpisode); - Mocker.GetMock().Verify(c => c.DownloadFile(_downloadUrl, _filePath), Times.Once()); + Mocker.GetMock().Verify(c => c.Get(It.Is(v => v.Url.ToString() == _downloadUrl)), Times.Once()); + Mocker.GetMock().Verify(c => c.OpenWriteStream(_filePath), Times.Once()); + Mocker.GetMock().Verify(c => c.DownloadFile(It.IsAny(), It.IsAny()), Times.Never()); } [Test] @@ -103,7 +109,9 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole Subject.Download(remoteEpisode); - Mocker.GetMock().Verify(c => c.DownloadFile(It.IsAny(), expectedFilename), Times.Once()); + Mocker.GetMock().Verify(c => c.Get(It.Is(v => v.Url.ToString() == _downloadUrl)), Times.Once()); + Mocker.GetMock().Verify(c => c.OpenWriteStream(expectedFilename), Times.Once()); + Mocker.GetMock().Verify(c => c.DownloadFile(It.IsAny(), It.IsAny()), Times.Never()); } [Test] diff --git a/src/NzbDrone.Core/Download/Clients/TorrentBlackhole/TorrentBlackhole.cs b/src/NzbDrone.Core/Download/Clients/TorrentBlackhole/TorrentBlackhole.cs index 2622f694e..fbb21c09d 100644 --- a/src/NzbDrone.Core/Download/Clients/TorrentBlackhole/TorrentBlackhole.cs +++ b/src/NzbDrone.Core/Download/Clients/TorrentBlackhole/TorrentBlackhole.cs @@ -1,62 +1,58 @@ using System; +using System.Collections.Generic; using System.IO; using System.Linq; -using System.Collections.Generic; +using FluentValidation.Results; +using NLog; using NzbDrone.Common.Disk; using NzbDrone.Common.Extensions; using NzbDrone.Common.Http; using NzbDrone.Core.Configuration; -using NzbDrone.Core.Indexers; +using NzbDrone.Core.MediaFiles; +using NzbDrone.Core.MediaFiles.TorrentInfo; using NzbDrone.Core.Organizer; -using NzbDrone.Core.Parser; using NzbDrone.Core.Parser.Model; -using NzbDrone.Core.MediaFiles; -using NLog; -using FluentValidation.Results; using NzbDrone.Core.RemotePathMappings; namespace NzbDrone.Core.Download.Clients.TorrentBlackhole { - public class TorrentBlackhole : DownloadClientBase + public class TorrentBlackhole : TorrentClientBase { private readonly IDiskScanService _diskScanService; - private readonly IHttpClient _httpClient; public TorrentBlackhole(IDiskScanService diskScanService, + ITorrentFileInfoReader torrentFileInfoReader, IHttpClient httpClient, IConfigService configService, IDiskProvider diskProvider, - IParsingService parsingService, IRemotePathMappingService remotePathMappingService, Logger logger) - : base(configService, diskProvider, remotePathMappingService, logger) + : base(torrentFileInfoReader, httpClient, configService, diskProvider, remotePathMappingService, logger) { _diskScanService = diskScanService; - _httpClient = httpClient; } - public override DownloadProtocol Protocol + protected override string AddFromMagnetLink(RemoteEpisode remoteEpisode, string hash, string magnetLink) { - get - { - return DownloadProtocol.Torrent; - } + throw new NotSupportedException("Blackhole does not support magnet links."); } - public override string Download(RemoteEpisode remoteEpisode) + protected override string AddFromTorrentFile(RemoteEpisode remoteEpisode, string hash, string filename, byte[] fileContent) { - var url = remoteEpisode.Release.DownloadUrl; var title = remoteEpisode.Release.Title; title = FileNameBuilder.CleanFileName(title); - var filename = Path.Combine(Settings.TorrentFolder, String.Format("{0}.torrent", title)); + var filepath = Path.Combine(Settings.TorrentFolder, String.Format("{0}.torrent", title)); + + using (var stream = _diskProvider.OpenWriteStream(filepath)) + { + stream.Write(fileContent, 0, fileContent.Length); + } - _logger.Debug("Downloading torrent from: {0} to: {1}", url, filename); - _httpClient.DownloadFile(url, filename); - _logger.Debug("Torrent Download succeeded, saved to: {0}", filename); + _logger.Debug("Torrent Download succeeded, saved to: {0}", filepath); - return null; + return hash; } public override IEnumerable GetItems() diff --git a/src/NzbDrone.Core/Download/Clients/UsenetBlackhole/UsenetBlackhole.cs b/src/NzbDrone.Core/Download/Clients/UsenetBlackhole/UsenetBlackhole.cs index 1af5bedda..218785b2f 100644 --- a/src/NzbDrone.Core/Download/Clients/UsenetBlackhole/UsenetBlackhole.cs +++ b/src/NzbDrone.Core/Download/Clients/UsenetBlackhole/UsenetBlackhole.cs @@ -1,25 +1,23 @@ using System; +using System.Collections.Generic; using System.IO; using System.Linq; -using System.Collections.Generic; +using FluentValidation.Results; +using NLog; using NzbDrone.Common.Disk; using NzbDrone.Common.Extensions; using NzbDrone.Common.Http; using NzbDrone.Core.Configuration; -using NzbDrone.Core.Indexers; +using NzbDrone.Core.MediaFiles; using NzbDrone.Core.Organizer; using NzbDrone.Core.Parser.Model; -using NzbDrone.Core.MediaFiles; -using NLog; -using FluentValidation.Results; using NzbDrone.Core.RemotePathMappings; namespace NzbDrone.Core.Download.Clients.UsenetBlackhole { - public class UsenetBlackhole : DownloadClientBase + public class UsenetBlackhole : UsenetClientBase { private readonly IDiskScanService _diskScanService; - private readonly IHttpClient _httpClient; public UsenetBlackhole(IDiskScanService diskScanService, IHttpClient httpClient, @@ -27,32 +25,25 @@ namespace NzbDrone.Core.Download.Clients.UsenetBlackhole IDiskProvider diskProvider, IRemotePathMappingService remotePathMappingService, Logger logger) - : base(configService, diskProvider, remotePathMappingService, logger) + : base(httpClient, configService, diskProvider, remotePathMappingService, logger) { _diskScanService = diskScanService; - _httpClient = httpClient; } - public override DownloadProtocol Protocol + protected override string AddFromNzbFile(RemoteEpisode remoteEpisode, string filename, byte[] fileContent) { - get - { - return DownloadProtocol.Usenet; - } - } - - public override string Download(RemoteEpisode remoteEpisode) - { - var url = remoteEpisode.Release.DownloadUrl; var title = remoteEpisode.Release.Title; title = FileNameBuilder.CleanFileName(title); - var filename = Path.Combine(Settings.NzbFolder, title + ".nzb"); + var filepath = Path.Combine(Settings.NzbFolder, title + ".nzb"); + + using (var stream = _diskProvider.OpenWriteStream(filepath)) + { + stream.Write(fileContent, 0, fileContent.Length); + } - _logger.Debug("Downloading NZB from: {0} to: {1}", url, filename); - _httpClient.DownloadFile(url, filename); - _logger.Debug("NZB Download succeeded, saved to: {0}", filename); + _logger.Debug("NZB Download succeeded, saved to: {0}", filepath); return null; } diff --git a/src/NzbDrone.Core/Download/TorrentClientBase.cs b/src/NzbDrone.Core/Download/TorrentClientBase.cs index 824d9bb2e..582f2e911 100644 --- a/src/NzbDrone.Core/Download/TorrentClientBase.cs +++ b/src/NzbDrone.Core/Download/TorrentClientBase.cs @@ -135,7 +135,7 @@ namespace NzbDrone.Core.Download if (hash != actualHash) { _logger.Warn( - "{0} did not return the expected InfoHash for '{1}', NzbDrone could potential lose track of the download in progress.", + "{0} did not return the expected InfoHash for '{1}', NzbDrone could potentially lose track of the download in progress.", Definition.Implementation, remoteEpisode.Release.DownloadUrl); } @@ -167,7 +167,7 @@ namespace NzbDrone.Core.Download if (hash != actualHash) { _logger.Warn( - "{0} did not return the expected InfoHash for '{1}', NzbDrone could potential lose track of the download in progress.", + "{0} did not return the expected InfoHash for '{1}', NzbDrone could potentially lose track of the download in progress.", Definition.Implementation, remoteEpisode.Release.DownloadUrl); }