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

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

@ -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<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()
@ -86,7 +95,9 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
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]
@ -100,7 +111,9 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
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]

@ -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<IDiskProvider>()
.Setup(c => c.OpenWriteStream(It.IsAny<string>()))
.Returns(() => new FileStream(GetTempFilePath(), FileMode.Create));
}
protected void GivenFailedDownload()
{
Mocker.GetMock<IHttpClient>()
.Setup(c => c.DownloadFile(It.IsAny<string>(), It.IsAny<string>()))
.Setup(c => c.Get(It.IsAny<HttpRequest>()))
.Throws(new WebException());
}
@ -89,7 +93,9 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
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]
@ -103,7 +109,9 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.Blackhole
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]

@ -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<TorrentBlackholeSettings>
public class TorrentBlackhole : TorrentClientBase<TorrentBlackholeSettings>
{
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<DownloadClientItem> GetItems()

@ -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<UsenetBlackholeSettings>
public class UsenetBlackhole : UsenetClientBase<UsenetBlackholeSettings>
{
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;
}

@ -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);
}

Loading…
Cancel
Save