From c8ad01e38eed0f60b5ba5b9be4ce9de942816491 Mon Sep 17 00:00:00 2001 From: Jesse Chan Date: Mon, 17 May 2021 21:26:58 +0100 Subject: [PATCH] New: Removing Flood downloads when seeding criteria have been met closes #4492 --- .../Migration/158_cdh_per_downloadclient.cs | 2 +- .../Download/Clients/Flood/Flood.cs | 30 +++++++++++++++++-- .../Download/Clients/Flood/Types/Torrent.cs | 4 +++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/NzbDrone.Core/Datastore/Migration/158_cdh_per_downloadclient.cs b/src/NzbDrone.Core/Datastore/Migration/158_cdh_per_downloadclient.cs index c26e33a72..00316fccb 100644 --- a/src/NzbDrone.Core/Datastore/Migration/158_cdh_per_downloadclient.cs +++ b/src/NzbDrone.Core/Datastore/Migration/158_cdh_per_downloadclient.cs @@ -36,7 +36,7 @@ namespace NzbDrone.Core.Datastore.Migration removeFailedDownloads = true; } - using (var updateClientCmd = conn.CreateCommand(tran, $"UPDATE DownloadClients SET RemoveCompletedDownloads = (CASE WHEN Implementation = \"RTorrent\" THEN 0 ELSE ? END), RemoveFailedDownloads = ?")) + using (var updateClientCmd = conn.CreateCommand(tran, $"UPDATE DownloadClients SET RemoveCompletedDownloads = (CASE WHEN Implementation IN (\"RTorrent\", \"Flood\") THEN 0 ELSE ? END), RemoveFailedDownloads = ?")) { updateClientCmd.AddParameter(removeCompletedDownloads ? 1 : 0); updateClientCmd.AddParameter(removeFailedDownloads ? 1 : 0); diff --git a/src/NzbDrone.Core/Download/Clients/Flood/Flood.cs b/src/NzbDrone.Core/Download/Clients/Flood/Flood.cs index a9cf03b22..a613146c3 100644 --- a/src/NzbDrone.Core/Download/Clients/Flood/Flood.cs +++ b/src/NzbDrone.Core/Download/Clients/Flood/Flood.cs @@ -18,8 +18,10 @@ namespace NzbDrone.Core.Download.Clients.Flood public class Flood : TorrentClientBase { private readonly IFloodProxy _proxy; + private readonly IDownloadSeedConfigProvider _downloadSeedConfigProvider; public Flood(IFloodProxy proxy, + IDownloadSeedConfigProvider downloadSeedConfigProvider, ITorrentFileInfoReader torrentFileInfoReader, IHttpClient httpClient, IConfigService configService, @@ -29,6 +31,7 @@ namespace NzbDrone.Core.Download.Clients.Flood : base(torrentFileInfoReader, httpClient, configService, diskProvider, remotePathMappingService, logger) { _proxy = proxy; + _downloadSeedConfigProvider = downloadSeedConfigProvider; } private static IEnumerable HandleTags(RemoteEpisode remoteEpisode, FloodSettings settings) @@ -77,7 +80,7 @@ namespace NzbDrone.Core.Download.Clients.Flood } public override string Name => "Flood"; - public override ProviderMessage Message => new ProviderMessage("Sonarr is unable to remove torrents that have finished seeding when using Flood", ProviderMessageType.Warning); + public override ProviderMessage Message => new ProviderMessage("Sonarr will handle automatic removal of torrents based on the current seed criteria in Settings -> Indexers", ProviderMessageType.Info); protected override string AddFromTorrentFile(RemoteEpisode remoteEpisode, string hash, string filename, byte[] fileContent) { @@ -119,6 +122,8 @@ namespace NzbDrone.Core.Download.Clients.Flood TotalSize = properties.SizeBytes, SeedRatio = properties.Ratio, Message = properties.Message, + CanMoveFiles = false, + CanBeRemoved = false, }; if (properties.Eta > 0) @@ -143,7 +148,28 @@ namespace NzbDrone.Core.Download.Clients.Flood item.Status = DownloadItemStatus.Paused; } - item.CanMoveFiles = item.CanBeRemoved = false; + if (item.Status == DownloadItemStatus.Completed) + { + // Grab cached seedConfig + var seedConfig = _downloadSeedConfigProvider.GetSeedConfiguration(item.DownloadId); + + if (seedConfig != null) + { + if (item.SeedRatio >= seedConfig.Ratio) + { + // Check if seed ratio reached + item.CanMoveFiles = item.CanBeRemoved = true; + } + else if (properties.DateFinished != null && properties.DateFinished > 0) + { + // Check if seed time reached + if ((DateTimeOffset.Now - DateTimeOffset.FromUnixTimeSeconds(properties.DateFinished)) >= seedConfig.SeedTime) + { + item.CanMoveFiles = item.CanBeRemoved = true; + } + } + } + } items.Add(item); } diff --git a/src/NzbDrone.Core/Download/Clients/Flood/Types/Torrent.cs b/src/NzbDrone.Core/Download/Clients/Flood/Types/Torrent.cs index 3f3500307..92ac1d1ab 100644 --- a/src/NzbDrone.Core/Download/Clients/Flood/Types/Torrent.cs +++ b/src/NzbDrone.Core/Download/Clients/Flood/Types/Torrent.cs @@ -31,5 +31,9 @@ namespace NzbDrone.Core.Download.Clients.Flood.Types [JsonProperty(PropertyName = "tags")] public List Tags { get; set; } + + // added in Flood 4.5 + [JsonProperty(PropertyName = "dateFinished")] + public long? DateFinished { get; set; } } }