Fixed: Blackhole clients cache nzb/torrent in memory before writing to the blackhole folder.

pull/4/head
Taloth Saldono 10 years ago
parent 96469be7f0
commit 36ac4f0a8d

@ -1,15 +1,16 @@
using System;
using System.IO; using System.IO;
using System.Net;
using System.Linq; using System.Linq;
using System.Net;
using FluentAssertions;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
using FluentAssertions;
using NzbDrone.Test.Common;
using NzbDrone.Common.Disk; using NzbDrone.Common.Disk;
using NzbDrone.Common.Http; using NzbDrone.Common.Http;
using NzbDrone.Core.Download; using NzbDrone.Core.Download;
using NzbDrone.Core.Download.Clients.TorrentBlackhole; using NzbDrone.Core.Download.Clients.TorrentBlackhole;
using System; using NzbDrone.Core.MediaFiles.TorrentInfo;
using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
{ {
@ -33,6 +34,14 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
TorrentFolder = _blackholeFolder, TorrentFolder = _blackholeFolder,
WatchFolder = _completedDownloadFolder WatchFolder = _completedDownloadFolder
}; };
Mocker.GetMock<IDiskProvider>()
.Setup(c => c.OpenWriteStream(It.IsAny<string>()))
.Returns(() => new FileStream(GetTempFilePath(), FileMode.Create));
Mocker.GetMock<ITorrentFileInfoReader>()
.Setup(c => c.GetHashFromTorrentFile(It.IsAny<byte[]>()))
.Returns("myhash");
} }
protected void GivenFailedDownload() protected void GivenFailedDownload()
@ -86,7 +95,9 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
Subject.Download(remoteEpisode); Subject.Download(remoteEpisode);
Mocker.GetMock<IHttpClient>().Verify(c => c.DownloadFile(_downloadUrl, _filePath), Times.Once()); Mocker.GetMock<IHttpClient>().Verify(c => c.Get(It.Is<HttpRequest>(v => v.Url.ToString() == _downloadUrl)), Times.Once());
Mocker.GetMock<IDiskProvider>().Verify(c => c.OpenWriteStream(_filePath), Times.Once());
Mocker.GetMock<IHttpClient>().Verify(c => c.DownloadFile(It.IsAny<string>(), It.IsAny<string>()), Times.Never());
} }
[Test] [Test]
@ -100,7 +111,9 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
Subject.Download(remoteEpisode); Subject.Download(remoteEpisode);
Mocker.GetMock<IHttpClient>().Verify(c => c.DownloadFile(It.IsAny<string>(), expectedFilename), Times.Once()); Mocker.GetMock<IHttpClient>().Verify(c => c.Get(It.Is<HttpRequest>(v => v.Url.ToString() == _downloadUrl)), Times.Once());
Mocker.GetMock<IDiskProvider>().Verify(c => c.OpenWriteStream(expectedFilename), Times.Once());
Mocker.GetMock<IHttpClient>().Verify(c => c.DownloadFile(It.IsAny<string>(), It.IsAny<string>()), Times.Never());
} }
[Test] [Test]

@ -1,16 +1,16 @@
using System; using System;
using System.IO; using System.IO;
using System.Net;
using System.Linq; using System.Linq;
using System.Net;
using FluentAssertions;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
using FluentAssertions;
using NzbDrone.Test.Common;
using NzbDrone.Common.Disk; using NzbDrone.Common.Disk;
using NzbDrone.Common.Http; using NzbDrone.Common.Http;
using NzbDrone.Core.Download; using NzbDrone.Core.Download;
using NzbDrone.Core.Download.Clients.UsenetBlackhole; using NzbDrone.Core.Download.Clients.UsenetBlackhole;
using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
{ {
@ -35,12 +35,16 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
NzbFolder = _blackholeFolder, NzbFolder = _blackholeFolder,
WatchFolder = _completedDownloadFolder WatchFolder = _completedDownloadFolder
}; };
Mocker.GetMock<IDiskProvider>()
.Setup(c => c.OpenWriteStream(It.IsAny<string>()))
.Returns(() => new FileStream(GetTempFilePath(), FileMode.Create));
} }
protected void GivenFailedDownload() protected void GivenFailedDownload()
{ {
Mocker.GetMock<IHttpClient>() Mocker.GetMock<IHttpClient>()
.Setup(c => c.DownloadFile(It.IsAny<string>(), It.IsAny<string>())) .Setup(c => c.Get(It.IsAny<HttpRequest>()))
.Throws(new WebException()); .Throws(new WebException());
} }
@ -89,7 +93,9 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
Subject.Download(remoteEpisode); Subject.Download(remoteEpisode);
Mocker.GetMock<IHttpClient>().Verify(c => c.DownloadFile(_downloadUrl, _filePath), Times.Once()); Mocker.GetMock<IHttpClient>().Verify(c => c.Get(It.Is<HttpRequest>(v => v.Url.ToString() == _downloadUrl)), Times.Once());
Mocker.GetMock<IDiskProvider>().Verify(c => c.OpenWriteStream(_filePath), Times.Once());
Mocker.GetMock<IHttpClient>().Verify(c => c.DownloadFile(It.IsAny<string>(), It.IsAny<string>()), Times.Never());
} }
[Test] [Test]
@ -103,7 +109,9 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
Subject.Download(remoteEpisode); Subject.Download(remoteEpisode);
Mocker.GetMock<IHttpClient>().Verify(c => c.DownloadFile(It.IsAny<string>(), expectedFilename), Times.Once()); Mocker.GetMock<IHttpClient>().Verify(c => c.Get(It.Is<HttpRequest>(v => v.Url.ToString() == _downloadUrl)), Times.Once());
Mocker.GetMock<IDiskProvider>().Verify(c => c.OpenWriteStream(expectedFilename), Times.Once());
Mocker.GetMock<IHttpClient>().Verify(c => c.DownloadFile(It.IsAny<string>(), It.IsAny<string>()), Times.Never());
} }
[Test] [Test]

@ -1,62 +1,58 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Collections.Generic; using FluentValidation.Results;
using NLog;
using NzbDrone.Common.Disk; using NzbDrone.Common.Disk;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http; using NzbDrone.Common.Http;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
using NzbDrone.Core.Indexers; using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.MediaFiles.TorrentInfo;
using NzbDrone.Core.Organizer; using NzbDrone.Core.Organizer;
using NzbDrone.Core.Parser;
using NzbDrone.Core.Parser.Model; using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.MediaFiles;
using NLog;
using FluentValidation.Results;
using NzbDrone.Core.RemotePathMappings; using NzbDrone.Core.RemotePathMappings;
namespace NzbDrone.Core.Download.Clients.TorrentBlackhole namespace NzbDrone.Core.Download.Clients.TorrentBlackhole
{ {
public class TorrentBlackhole : DownloadClientBase<TorrentBlackholeSettings> public class TorrentBlackhole : TorrentClientBase<TorrentBlackholeSettings>
{ {
private readonly IDiskScanService _diskScanService; private readonly IDiskScanService _diskScanService;
private readonly IHttpClient _httpClient;
public TorrentBlackhole(IDiskScanService diskScanService, public TorrentBlackhole(IDiskScanService diskScanService,
ITorrentFileInfoReader torrentFileInfoReader,
IHttpClient httpClient, IHttpClient httpClient,
IConfigService configService, IConfigService configService,
IDiskProvider diskProvider, IDiskProvider diskProvider,
IParsingService parsingService,
IRemotePathMappingService remotePathMappingService, IRemotePathMappingService remotePathMappingService,
Logger logger) Logger logger)
: base(configService, diskProvider, remotePathMappingService, logger) : base(torrentFileInfoReader, httpClient, configService, diskProvider, remotePathMappingService, logger)
{ {
_diskScanService = diskScanService; _diskScanService = diskScanService;
_httpClient = httpClient;
} }
public override DownloadProtocol Protocol protected override string AddFromMagnetLink(RemoteEpisode remoteEpisode, string hash, string magnetLink)
{ {
get throw new NotSupportedException("Blackhole does not support magnet links.");
{
return DownloadProtocol.Torrent;
}
} }
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; var title = remoteEpisode.Release.Title;
title = FileNameBuilder.CleanFileName(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); _logger.Debug("Torrent Download succeeded, saved to: {0}", filepath);
_httpClient.DownloadFile(url, filename);
_logger.Debug("Torrent Download succeeded, saved to: {0}", filename);
return null; return hash;
} }
public override IEnumerable<DownloadClientItem> GetItems() public override IEnumerable<DownloadClientItem> GetItems()

@ -1,25 +1,23 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Collections.Generic; using FluentValidation.Results;
using NLog;
using NzbDrone.Common.Disk; using NzbDrone.Common.Disk;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http; using NzbDrone.Common.Http;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
using NzbDrone.Core.Indexers; using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.Organizer; using NzbDrone.Core.Organizer;
using NzbDrone.Core.Parser.Model; using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.MediaFiles;
using NLog;
using FluentValidation.Results;
using NzbDrone.Core.RemotePathMappings; using NzbDrone.Core.RemotePathMappings;
namespace NzbDrone.Core.Download.Clients.UsenetBlackhole namespace NzbDrone.Core.Download.Clients.UsenetBlackhole
{ {
public class UsenetBlackhole : DownloadClientBase<UsenetBlackholeSettings> public class UsenetBlackhole : UsenetClientBase<UsenetBlackholeSettings>
{ {
private readonly IDiskScanService _diskScanService; private readonly IDiskScanService _diskScanService;
private readonly IHttpClient _httpClient;
public UsenetBlackhole(IDiskScanService diskScanService, public UsenetBlackhole(IDiskScanService diskScanService,
IHttpClient httpClient, IHttpClient httpClient,
@ -27,32 +25,25 @@ namespace NzbDrone.Core.Download.Clients.UsenetBlackhole
IDiskProvider diskProvider, IDiskProvider diskProvider,
IRemotePathMappingService remotePathMappingService, IRemotePathMappingService remotePathMappingService,
Logger logger) Logger logger)
: base(configService, diskProvider, remotePathMappingService, logger) : base(httpClient, configService, diskProvider, remotePathMappingService, logger)
{ {
_diskScanService = diskScanService; _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; var title = remoteEpisode.Release.Title;
title = FileNameBuilder.CleanFileName(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); _logger.Debug("NZB Download succeeded, saved to: {0}", filepath);
_httpClient.DownloadFile(url, filename);
_logger.Debug("NZB Download succeeded, saved to: {0}", filename);
return null; return null;
} }

@ -135,7 +135,7 @@ namespace NzbDrone.Core.Download
if (hash != actualHash) if (hash != actualHash)
{ {
_logger.Warn( _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); Definition.Implementation, remoteEpisode.Release.DownloadUrl);
} }
@ -167,7 +167,7 @@ namespace NzbDrone.Core.Download
if (hash != actualHash) if (hash != actualHash)
{ {
_logger.Warn( _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); Definition.Implementation, remoteEpisode.Release.DownloadUrl);
} }

Loading…
Cancel
Save