Applied original .patch

From original PR https://github.com/Sonarr/Sonarr/pull/1357
This commit is contained in:
Michael Feinbier 2023-09-15 08:20:45 +02:00
parent 60f18249b0
commit 2eb24d494b
12 changed files with 776 additions and 0 deletions

View File

@ -0,0 +1,338 @@
using System;
using System.Linq;
using System.Collections.Generic;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Common.Http;
using NzbDrone.Core.MediaFiles.TorrentInfo;
using NzbDrone.Core.Download;
using NzbDrone.Core.Download.Clients.Putio;
namespace NzbDrone.Core.Test.Download.DownloadClientTests.PutioTests
{
[TestFixture]
public class PutioFixture : DownloadClientFixtureBase<Putio>
{
protected PutioSettings _settings;
protected PutioTorrent _queued;
protected PutioTorrent _downloading;
protected PutioTorrent _failed;
protected PutioTorrent _completed;
protected PutioTorrent _magnet;
protected Dictionary<string, object> _PutioAccountSettingsItems;
[SetUp]
public void Setup()
{
_settings = new PutioSettings
{
};
Subject.Definition = new DownloadClientDefinition();
Subject.Definition.Settings = _settings;
_queued = new PutioTorrent
{
HashString = "HASH",
IsFinished = false,
Status = PutioTorrentStatus.InQueue,
Name = _title,
TotalSize = 1000,
LeftUntilDone = 1000,
DownloadDir = "somepath"
};
_downloading = new PutioTorrent
{
HashString = "HASH",
IsFinished = false,
Status = PutioTorrentStatus.Downloading,
Name = _title,
TotalSize = 1000,
LeftUntilDone = 100,
DownloadDir = "somepath"
};
_failed = new PutioTorrent
{
HashString = "HASH",
IsFinished = false,
Status = PutioTorrentStatus.Error,
Name = _title,
TotalSize = 1000,
LeftUntilDone = 100,
ErrorString = "Error",
DownloadDir = "somepath"
};
_completed = new PutioTorrent
{
HashString = "HASH",
IsFinished = true,
Status = PutioTorrentStatus.Completed,
Name = _title,
TotalSize = 1000,
LeftUntilDone = 0,
DownloadDir = "somepath"
};
_magnet = new PutioTorrent
{
HashString = "HASH",
IsFinished = false,
Status = PutioTorrentStatus.Downloading,
Name = _title,
TotalSize = 0,
LeftUntilDone = 100,
DownloadDir = "somepath"
};
Mocker.GetMock<ITorrentFileInfoReader>()
.Setup(s => s.GetHashFromTorrentFile(It.IsAny<byte[]>()))
.Returns("CBC2F069FE8BB2F544EAE707D75BCD3DE9DCF951");
Mocker.GetMock<IHttpClient>()
.Setup(s => s.Get(It.IsAny<HttpRequest>()))
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), new byte[0]));
_PutioAccountSettingsItems = new Dictionary<string, object>();
_PutioAccountSettingsItems.Add("download-dir", @"C:/Downloads/Finished/Putio");
_PutioAccountSettingsItems.Add("incomplete-dir", null);
_PutioAccountSettingsItems.Add("incomplete-dir-enabled", false);
Mocker.GetMock<IPutioProxy>()
.Setup(v => v.GetAccountSettings(It.IsAny<PutioSettings>()))
.Returns(_PutioAccountSettingsItems);
}
protected void GivenFailedDownload()
{
Mocker.GetMock<IPutioProxy>()
.Setup(s => s.AddTorrentFromUrl(It.IsAny<string>(), It.IsAny<PutioSettings>()))
.Throws<InvalidOperationException>();
}
protected void GivenSuccessfulDownload()
{
Mocker.GetMock<IHttpClient>()
.Setup(s => s.Get(It.IsAny<HttpRequest>()))
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), new byte[1000]));
Mocker.GetMock<IPutioProxy>()
.Setup(s => s.AddTorrentFromUrl(It.IsAny<string>(), It.IsAny<PutioSettings>()))
.Callback(PrepareClientToReturnQueuedItem);
Mocker.GetMock<IPutioProxy>()
.Setup(s => s.AddTorrentFromData(It.IsAny<byte[]>(), It.IsAny<PutioSettings>()))
.Callback(PrepareClientToReturnQueuedItem);
}
protected virtual void GivenTorrents(List<PutioTorrent> torrents)
{
if (torrents == null)
{
torrents = new List<PutioTorrent>();
}
Mocker.GetMock<IPutioProxy>()
.Setup(s => s.GetTorrents(It.IsAny<PutioSettings>()))
.Returns(torrents);
}
protected void PrepareClientToReturnQueuedItem()
{
GivenTorrents(new List<PutioTorrent>
{
_queued
});
}
protected void PrepareClientToReturnDownloadingItem()
{
GivenTorrents(new List<PutioTorrent>
{
_downloading
});
}
protected void PrepareClientToReturnFailedItem()
{
GivenTorrents(new List<PutioTorrent>
{
_failed
});
}
protected void PrepareClientToReturnCompletedItem()
{
GivenTorrents(new List<PutioTorrent>
{
_completed
});
}
protected void PrepareClientToReturnMagnetItem()
{
GivenTorrents(new List<PutioTorrent>
{
_magnet
});
}
[Test]
public void queued_item_should_have_required_properties()
{
PrepareClientToReturnQueuedItem();
var item = Subject.GetItems().Single();
VerifyQueued(item);
}
[Test]
public void downloading_item_should_have_required_properties()
{
PrepareClientToReturnDownloadingItem();
var item = Subject.GetItems().Single();
VerifyDownloading(item);
}
[Test]
public void failed_item_should_have_required_properties()
{
PrepareClientToReturnFailedItem();
var item = Subject.GetItems().Single();
VerifyWarning(item);
}
[Test]
public void completed_download_should_have_required_properties()
{
PrepareClientToReturnCompletedItem();
var item = Subject.GetItems().Single();
VerifyCompleted(item);
}
[Test]
public void magnet_download_should_not_return_the_item()
{
PrepareClientToReturnMagnetItem();
Subject.GetItems().Count().Should().Be(0);
}
[Test]
public void Download_should_return_unique_id()
{
GivenSuccessfulDownload();
var remoteEpisode = CreateRemoteEpisode();
var id = Subject.Download(remoteEpisode);
id.Should().NotBeNullOrEmpty();
}
[TestCase("magnet:?xt=urn:btih:ZPBPA2P6ROZPKRHK44D5OW6NHXU5Z6KR&tr=udp", "CBC2F069FE8BB2F544EAE707D75BCD3DE9DCF951")]
public void Download_should_get_hash_from_magnet_url(string magnetUrl, string expectedHash)
{
GivenSuccessfulDownload();
var remoteEpisode = CreateRemoteEpisode();
remoteEpisode.Release.DownloadUrl = magnetUrl;
var id = Subject.Download(remoteEpisode);
id.Should().Be(expectedHash);
}
[TestCase(PutioTorrentStatus.Stopped, DownloadItemStatus.Downloading)]
[TestCase(PutioTorrentStatus.CheckWait, DownloadItemStatus.Downloading)]
[TestCase(PutioTorrentStatus.Check, DownloadItemStatus.Downloading)]
[TestCase(PutioTorrentStatus.Queued, DownloadItemStatus.Queued)]
[TestCase(PutioTorrentStatus.Downloading, DownloadItemStatus.Downloading)]
[TestCase(PutioTorrentStatus.SeedingWait, DownloadItemStatus.Completed)]
[TestCase(PutioTorrentStatus.Seeding, DownloadItemStatus.Completed)]
public void GetItems_should_return_queued_item_as_downloadItemStatus(PutioTorrentStatus apiStatus, DownloadItemStatus expectedItemStatus)
{
_queued.Status = apiStatus;
PrepareClientToReturnQueuedItem();
var item = Subject.GetItems().Single();
item.Status.Should().Be(expectedItemStatus);
}
[TestCase(PutioTorrentStatus.Queued, DownloadItemStatus.Queued)]
[TestCase(PutioTorrentStatus.Downloading, DownloadItemStatus.Downloading)]
[TestCase(PutioTorrentStatus.Seeding, DownloadItemStatus.Completed)]
public void GetItems_should_return_downloading_item_as_downloadItemStatus(PutioTorrentStatus apiStatus, DownloadItemStatus expectedItemStatus)
{
_downloading.Status = apiStatus;
PrepareClientToReturnDownloadingItem();
var item = Subject.GetItems().Single();
item.Status.Should().Be(expectedItemStatus);
}
[TestCase(PutioTorrentStatus.Stopped, DownloadItemStatus.Completed, false)]
[TestCase(PutioTorrentStatus.CheckWait, DownloadItemStatus.Downloading, true)]
[TestCase(PutioTorrentStatus.Check, DownloadItemStatus.Downloading, true)]
[TestCase(PutioTorrentStatus.Queued, DownloadItemStatus.Completed, true)]
[TestCase(PutioTorrentStatus.SeedingWait, DownloadItemStatus.Completed, true)]
[TestCase(PutioTorrentStatus.Seeding, DownloadItemStatus.Completed, true)]
public void GetItems_should_return_completed_item_as_downloadItemStatus(PutioTorrentStatus apiStatus, DownloadItemStatus expectedItemStatus, bool expectedReadOnly)
{
_completed.Status = apiStatus;
PrepareClientToReturnCompletedItem();
var item = Subject.GetItems().Single();
item.Status.Should().Be(expectedItemStatus);
item.IsReadOnly.Should().Be(expectedReadOnly);
}
[Test]
public void should_return_status_with_outputdirs()
{
var result = Subject.GetStatus();
result.IsLocalhost.Should().BeTrue();
result.OutputRootFolders.Should().NotBeNull();
result.OutputRootFolders.First().Should().Be(@"C:\Downloads\Finished\Putio");
}
[Test]
public void should_fix_forward_slashes()
{
WindowsOnly();
_downloading.DownloadDir = @"C:/Downloads/Finished/Putio";
GivenTorrents(new List<PutioTorrent>
{
_downloading
});
var items = Subject.GetItems().ToList();
items.Should().HaveCount(1);
items.First().OutputPath.Should().Be(@"C:\Downloads\Finished\Putio\" + _title);
}
[TestCase(-1)] // Infinite/Unknown
[TestCase(-2)] // Magnet Downloading
public void should_ignore_negative_eta(int eta)
{
_completed.Eta = eta;
PrepareClientToReturnCompletedItem();
var item = Subject.GetItems().Single();
item.RemainingTime.Should().NotHaveValue();
}
}
}

View File

@ -0,0 +1,191 @@
using System;
using System.Linq;
using System.Collections.Generic;
using NzbDrone.Common.Disk;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http;
using NzbDrone.Core.Configuration;
using NLog;
using FluentValidation.Results;
using NzbDrone.Core.MediaFiles.TorrentInfo;
using NzbDrone.Core.Validation;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.RemotePathMappings;
namespace NzbDrone.Core.Download.Clients.Putio
{
public class Putio : TorrentClientBase<PutioSettings>
{
private readonly IPutioProxy _proxy;
public Putio(IPutioProxy proxy,
ITorrentFileInfoReader torrentFileInfoReader,
IHttpClient httpClient,
IConfigService configService,
IDiskProvider diskProvider,
IRemotePathMappingService remotePathMappingService,
Logger logger)
: base(torrentFileInfoReader, httpClient, configService, diskProvider, remotePathMappingService, logger)
{
_proxy = proxy;
}
public override string Name
{
get
{
return "put.io";
}
}
protected override string AddFromMagnetLink(RemoteEpisode remoteEpisode, string hash, string magnetLink)
{
_proxy.AddTorrentFromUrl(magnetLink, Settings);
return hash;
}
protected override string AddFromTorrentFile(RemoteEpisode remoteEpisode, string hash, string filename, byte[] fileContent)
{
_proxy.AddTorrentFromData(fileContent, Settings);
return hash;
}
public override IEnumerable<DownloadClientItem> GetItems()
{
List<PutioTorrent> torrents;
try
{
torrents = _proxy.GetTorrents(Settings);
}
catch (DownloadClientException ex)
{
_logger.Error(ex, ex.Message);
return Enumerable.Empty<DownloadClientItem>();
}
var items = new List<DownloadClientItem>();
foreach (var torrent in torrents)
{
// If totalsize == 0 the torrent is a magnet downloading metadata
if (torrent.Size == 0)
continue;
var item = new DownloadClientItem();
item.DownloadId = "putio-" + torrent.Id;
item.Category = Settings.SaveParentId;
item.Title = torrent.Name;
item.DownloadClient = Definition.Name;
item.TotalSize = torrent.Size;
item.RemainingSize = torrent.Size - torrent.Downloaded;
try
{
if (torrent.FileId != 0)
{
var file = _proxy.GetFile(torrent.FileId, Settings);
var torrentPath = "/completed/" + file.Name;
var outputPath = _remotePathMappingService.RemapRemoteToLocal(Settings.Url, new OsPath(torrentPath));
if (Settings.SaveParentId.IsNotNullOrWhiteSpace())
{
var directories = outputPath.FullPath.Split('\\', '/');
if (!directories.Contains(string.Format("{0}", Settings.SaveParentId))) continue;
}
item.OutputPath = outputPath; // + torrent.Name;
}
}
catch (DownloadClientException ex)
{
_logger.Error(ex, ex.Message);
}
if (torrent.EstimatedTime >= 0)
{
item.RemainingTime = TimeSpan.FromSeconds(torrent.EstimatedTime);
}
if (!torrent.ErrorMessage.IsNullOrWhiteSpace())
{
item.Status = DownloadItemStatus.Warning;
item.Message = torrent.ErrorMessage;
}
else if (torrent.Status == PutioTorrentStatus.Completed)
{
item.Status = DownloadItemStatus.Completed;
}
else if (torrent.Status == PutioTorrentStatus.InQueue)
{
item.Status = DownloadItemStatus.Queued;
}
else
{
item.Status = DownloadItemStatus.Downloading;
}
// item.IsReadOnly = torrent.Status != PutioTorrentStatus.Error;
items.Add(item);
}
return items;
}
public override void RemoveItem(string downloadId, bool deleteData)
{
_proxy.RemoveTorrent(downloadId.ToLower(), Settings);
}
public override DownloadClientStatus GetStatus()
{
var destDir = string.Format("{0}", Settings.SaveParentId);
return new DownloadClientStatus
{
IsLocalhost = false,
OutputRootFolders = new List<OsPath> { _remotePathMappingService.RemapRemoteToLocal(Settings.Url, new OsPath(destDir)) }
};
}
protected override void Test(List<ValidationFailure> failures)
{
failures.AddIfNotNull(TestConnection());
if (failures.Any()) return;
failures.AddIfNotNull(TestGetTorrents());
}
private ValidationFailure TestConnection()
{
try
{
_proxy.GetAccountSettings(Settings);
}
catch (Exception ex)
{
_logger.Error(ex, ex.Message);
return new NzbDroneValidationFailure(string.Empty, "Unknown exception: " + ex.Message);
}
return null;
}
private ValidationFailure TestGetTorrents()
{
try
{
_proxy.GetTorrents(Settings);
}
catch (Exception ex)
{
_logger.Error(ex, ex.Message);
return new NzbDroneValidationFailure(string.Empty, "Failed to get the list of torrents: " + ex.Message);
}
return null;
}
}
}

View File

@ -0,0 +1,13 @@
using System;
namespace NzbDrone.Core.Download.Clients.Putio
{
public class PutioException : DownloadClientException
{
public PutioException(string message)
: base(message)
{
}
}
}

View File

@ -0,0 +1,8 @@
namespace NzbDrone.Core.Download.Clients.Putio
{
public class PutioFile
{
public int Id { get; set; }
public string Name { get; set; }
}
}

View File

@ -0,0 +1,8 @@
using System.Collections.Generic;
namespace NzbDrone.Core.Download.Clients.Putio
{
public class PutioFileResponse : PutioGenericResponse
{
public PutioFile File { get; set; }
}
}

View File

@ -0,0 +1,12 @@
using Newtonsoft.Json;
namespace NzbDrone.Core.Download.Clients.Putio
{
public class PutioGenericResponse
{
[JsonProperty(PropertyName = "error_message")]
public string ErrorMessage { get; set; }
public string Status { get; set; }
}
}

View File

@ -0,0 +1,8 @@
namespace NzbDrone.Core.Download.Clients.Putio
{
public enum PutioPriority
{
Last = 0,
First = 1
}
}

View File

@ -0,0 +1,107 @@
using System;
using System.Collections.Generic;
using NzbDrone.Core.Rest;
using NLog;
using RestSharp;
using RestSharp.Deserializers;
using Newtonsoft.Json;
namespace NzbDrone.Core.Download.Clients.Putio
{
public interface IPutioProxy
{
List<PutioTorrent> GetTorrents(PutioSettings settings);
PutioFile GetFile(long fileId, PutioSettings settings);
void AddTorrentFromUrl(string torrentUrl, PutioSettings settings);
void AddTorrentFromData(byte[] torrentData, PutioSettings settings);
void RemoveTorrent(string hash, PutioSettings settings);
void GetAccountSettings(PutioSettings settings);
}
public class PutioProxy: IPutioProxy
{
private readonly Logger _logger;
public PutioProxy(Logger logger)
{
_logger = logger;
}
public List<PutioTorrent> GetTorrents(PutioSettings settings)
{
var result = ProcessRequest<PutioTransfersResponse>(Method.GET, "transfers/list", null, settings);
return result.Transfers;
}
public PutioFile GetFile(long fileId, PutioSettings settings)
{
var result = ProcessRequest<PutioFileResponse>(Method.GET, "files/" + fileId, null, settings);
return result.File;
}
public void AddTorrentFromUrl(string torrentUrl, PutioSettings settings)
{
var arguments = new Dictionary<string, object>();
arguments.Add("url", torrentUrl);
ProcessRequest<PutioGenericResponse>(Method.POST, "transfers/add", arguments, settings);
}
public void AddTorrentFromData(byte[] torrentData, PutioSettings settings)
{
var arguments = new Dictionary<string, object>();
arguments.Add("metainfo", Convert.ToBase64String(torrentData));
ProcessRequest<PutioGenericResponse>(Method.POST, "transfers/add", arguments, settings);
}
public void RemoveTorrent(string hashString, PutioSettings settings)
{
var arguments = new Dictionary<string, object>();
arguments.Add("transfer_ids", new string[] { hashString });
ProcessRequest<PutioGenericResponse>(Method.POST, "torrents/cancel", arguments, settings);
}
public void GetAccountSettings(PutioSettings settings)
{
ProcessRequest<PutioGenericResponse>(Method.GET, "account/settings", null, settings);
}
public TResponseType ProcessRequest<TResponseType>(Method method, string resource, Dictionary<string, object> arguments, PutioSettings settings) where TResponseType : PutioGenericResponse
{
var client = BuildClient(settings);
var request = new RestRequest(resource, method);
request.RequestFormat = DataFormat.Json;
request.AddQueryParameter("oauth_token", settings.OAuthToken);
if (arguments != null)
{
foreach (KeyValuePair<string, object> e in arguments)
{
request.AddParameter(e.Key, e.Value);
}
}
_logger.Debug("Method: {0} Url: {1}", method, client.BuildUri(request));
var restResponse = client.Execute(request);
var json = new JsonDeserializer();
TResponseType output = json.Deserialize<TResponseType>(restResponse);
if (output.Status != "OK")
{
throw new PutioException(output.ErrorMessage);
}
return output;
}
private IRestClient BuildClient(PutioSettings settings)
{
var restClient = RestClientFactory.BuildClient(settings.Url);
restClient.FollowRedirects = false;
return restClient;
}
}
}

View File

@ -0,0 +1,39 @@
using System.Text.RegularExpressions;
using FluentValidation;
using NzbDrone.Core.Annotations;
using NzbDrone.Core.ThingiProvider;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Download.Clients.Putio
{
public class PutioSettingsValidator : AbstractValidator<PutioSettings>
{
public PutioSettingsValidator()
{
RuleFor(c => c.SaveParentId).Matches(@"^\.?[0-9]*$", RegexOptions.IgnoreCase).WithMessage("Allowed characters 0-9");
}
}
public class PutioSettings : IProviderConfig
{
private static readonly PutioSettingsValidator Validator = new PutioSettingsValidator();
public PutioSettings()
{
Url = "https://api.put.io/v2";
}
public string Url { get; }
[FieldDefinition(0, Label = "OAuth Token", Type = FieldType.Textbox)]
public string OAuthToken { get; set; }
[FieldDefinition(1, Label = "Save Parent ID", Type = FieldType.Textbox, HelpText = "Adding a save parent ID specific to Sonarr avoids conflicts with unrelated downloads, but it's optional. Creates a .[SaveParentId] subdirectory in the output directory.")]
public string SaveParentId { get; set; }
public NzbDroneValidationResult Validate()
{
return new NzbDroneValidationResult(Validator.Validate(this));
}
}
}

View File

@ -0,0 +1,32 @@
using Newtonsoft.Json;
namespace NzbDrone.Core.Download.Clients.Putio
{
public class PutioTorrent
{
public long Downloaded { get; set; }
[JsonProperty(PropertyName = "error_message")]
public string ErrorMessage { get; set; }
[JsonProperty(PropertyName = "estimated_time")]
public long EstimatedTime { get; set; }
[JsonProperty(PropertyName = "file_id")]
public long FileId { get; set; }
public int Id { get; set; }
public string Name { get; set; }
[JsonProperty(PropertyName = "percent_done")]
public int PercentDone { get; set; }
[JsonProperty(PropertyName = "seconds_seeding")]
public long SecondsSeeding { get; set; }
public long Size { get; set; }
public string Status { get; set; }
}
}

View File

@ -0,0 +1,12 @@
using System;
namespace NzbDrone.Core.Download.Clients.Putio
{
public sealed class PutioTorrentStatus
{
public static readonly String Completed = "COMPLETED";
public static readonly String Downloading = "DOWNLOADING";
public static readonly String Error = "ERROR";
public static readonly String InQueue = "IN_QUEUE";
}
}

View File

@ -0,0 +1,8 @@
using System.Collections.Generic;
namespace NzbDrone.Core.Download.Clients.Putio
{
public class PutioTransfersResponse : PutioGenericResponse
{
public List<PutioTorrent> Transfers { get; set; }
}
}