New: Per download client setting to Remove Completed/Failed downloads instead of global setting

This commit is contained in:
Taloth Saldono 2021-05-15 21:37:55 +02:00 committed by Taloth
parent 8e46362ff9
commit 2dba5ef4b4
18 changed files with 307 additions and 95 deletions

View File

@ -16,4 +16,9 @@
color: #3a3f51;
font-size: 21px;
line-height: inherit;
&.small {
color: #909293;
font-size: 18px;
}
}

View File

@ -1,5 +1,7 @@
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import classNames from 'classnames';
import { sizes } from 'Helpers/Props';
import styles from './FieldSet.css';
class FieldSet extends Component {
@ -9,13 +11,14 @@ class FieldSet extends Component {
render() {
const {
size,
legend,
children
} = this.props;
return (
<fieldset className={styles.fieldSet}>
<legend className={styles.legend}>
<legend className={classNames(styles.legend, (size === sizes.SMALL) && styles.small)}>
{legend}
</legend>
{children}
@ -26,8 +29,13 @@ class FieldSet extends Component {
}
FieldSet.propTypes = {
size: PropTypes.oneOf(sizes.all).isRequired,
legend: PropTypes.oneOfType([PropTypes.node, PropTypes.string]),
children: PropTypes.node
};
FieldSet.defaultProps = {
size: sizes.MEDIUM
};
export default FieldSet;

View File

@ -1,6 +1,6 @@
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { inputTypes, kinds } from 'Helpers/Props';
import { inputTypes, kinds, sizes } from 'Helpers/Props';
import Alert from 'Components/Alert';
import Button from 'Components/Link/Button';
import SpinnerErrorButton from 'Components/Link/SpinnerErrorButton';
@ -9,6 +9,7 @@ import ModalContent from 'Components/Modal/ModalContent';
import ModalHeader from 'Components/Modal/ModalHeader';
import ModalBody from 'Components/Modal/ModalBody';
import ModalFooter from 'Components/Modal/ModalFooter';
import FieldSet from 'Components/FieldSet';
import Form from 'Components/Form/Form';
import FormGroup from 'Components/Form/FormGroup';
import FormLabel from 'Components/Form/FormLabel';
@ -44,7 +45,10 @@ class EditDownloadClientModalContent extends Component {
implementationName,
name,
enable,
protocol,
priority,
removeCompletedDownloads,
removeFailedDownloads,
fields,
message
} = item;
@ -133,6 +137,37 @@ class EditDownloadClientModalContent extends Component {
/>
</FormGroup>
<FieldSet
size={sizes.SMALL}
legend="Completed Download Handling"
>
<FormGroup>
<FormLabel>Remove Completed</FormLabel>
<FormInputGroup
type={inputTypes.CHECK}
name="removeCompletedDownloads"
helpText="Remove imported downloads from download client history (when finished seeding for torrents)"
{...removeCompletedDownloads}
onChange={onInputChange}
/>
</FormGroup>
{
protocol.value !== 'torrent' &&
<FormGroup>
<FormLabel>Remove Failed</FormLabel>
<FormInputGroup
type={inputTypes.CHECK}
name="removeFailedDownloads"
helpText="Remove failed downloads from download client history"
{...removeFailedDownloads}
onChange={onInputChange}
/>
</FormGroup>
}
</FieldSet>
</Form>
}
</ModalBody>

View File

@ -1,6 +1,7 @@
import PropTypes from 'prop-types';
import React from 'react';
import { inputTypes, sizes } from 'Helpers/Props';
import { inputTypes, kinds, sizes } from 'Helpers/Props';
import Alert from 'Components/Alert';
import FieldSet from 'Components/FieldSet';
import LoadingIndicator from 'Components/Loading/LoadingIndicator';
import Form from 'Components/Form/Form';
@ -31,11 +32,16 @@ function DownloadClientOptions(props) {
}
{
hasSettings && !isFetching && !error &&
hasSettings && !isFetching && !error && advancedSettings &&
<div>
<FieldSet legend="Completed Download Handling">
<Form>
<FormGroup size={sizes.MEDIUM}>
<FormGroup
advancedSettings={advancedSettings}
isAdvanced={true}
size={sizes.MEDIUM}
>
<FormLabel>Enable</FormLabel>
<FormInputGroup
@ -52,25 +58,7 @@ function DownloadClientOptions(props) {
isAdvanced={true}
size={sizes.MEDIUM}
>
<FormLabel>Remove</FormLabel>
<FormInputGroup
type={inputTypes.CHECK}
name="removeCompletedDownloads"
helpText="Remove imported downloads from download client history"
onChange={onInputChange}
{...settings.removeCompletedDownloads}
/>
</FormGroup>
</Form>
</FieldSet>
<FieldSet
legend="Failed Download Handling"
>
<Form>
<FormGroup size={sizes.MEDIUM}>
<FormLabel>Redownload</FormLabel>
<FormLabel>Redownload Failed</FormLabel>
<FormInputGroup
type={inputTypes.CHECK}
@ -80,23 +68,11 @@ function DownloadClientOptions(props) {
{...settings.autoRedownloadFailed}
/>
</FormGroup>
<FormGroup
advancedSettings={advancedSettings}
isAdvanced={true}
size={sizes.MEDIUM}
>
<FormLabel>Remove</FormLabel>
<FormInputGroup
type={inputTypes.CHECK}
name="removeFailedDownloads"
helpText="Remove failed downloads from download client history"
onChange={onInputChange}
{...settings.removeFailedDownloads}
/>
</FormGroup>
</Form>
<Alert kind={kinds.INFO}>
The Remove settings were moved to the individual Download Client settings in the table above.
</Alert>
</FieldSet>
</div>
}

View File

@ -8,10 +8,7 @@ namespace NzbDrone.Api.Config
public string DownloadClientWorkingFolders { get; set; }
public bool EnableCompletedDownloadHandling { get; set; }
public bool RemoveCompletedDownloads { get; set; }
public bool AutoRedownloadFailed { get; set; }
public bool RemoveFailedDownloads { get; set; }
}
public static class DownloadClientConfigResourceMapper
@ -23,10 +20,7 @@ namespace NzbDrone.Api.Config
DownloadClientWorkingFolders = model.DownloadClientWorkingFolders,
EnableCompletedDownloadHandling = model.EnableCompletedDownloadHandling,
RemoveCompletedDownloads = model.RemoveCompletedDownloads,
AutoRedownloadFailed = model.AutoRedownloadFailed,
RemoveFailedDownloads = model.RemoveFailedDownloads
AutoRedownloadFailed = model.AutoRedownloadFailed
};
}
}

View File

@ -16,6 +16,8 @@ namespace NzbDrone.Api.DownloadClient
resource.Enable = definition.Enable;
resource.Protocol = definition.Protocol;
resource.Priority = definition.Priority;
resource.RemoveCompletedDownloads = definition.RemoveCompletedDownloads;
resource.RemoveFailedDownloads = definition.RemoveFailedDownloads;
}
protected override void MapToModel(DownloadClientDefinition definition, DownloadClientResource resource)
@ -25,6 +27,8 @@ namespace NzbDrone.Api.DownloadClient
definition.Enable = resource.Enable;
definition.Protocol = resource.Protocol;
definition.Priority = resource.Priority;
definition.RemoveCompletedDownloads = resource.RemoveCompletedDownloads;
definition.RemoveFailedDownloads = resource.RemoveFailedDownloads;
}
protected override void Validate(DownloadClientDefinition definition, bool includeWarnings)

View File

@ -7,5 +7,7 @@ namespace NzbDrone.Api.DownloadClient
public bool Enable { get; set; }
public DownloadProtocol Protocol { get; set; }
public int Priority { get; set; }
public bool RemoveCompletedDownloads { get; set; }
public bool RemoveFailedDownloads { get; set; }
}
}

View File

@ -0,0 +1,119 @@
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
using NzbDrone.Common.Serializer;
using NzbDrone.Core.Datastore.Migration;
using NzbDrone.Core.Download.Clients.RTorrent;
using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test.Datastore.Migration
{
[TestFixture]
public class cdh_per_downloadclientFixture : MigrationTest<cdh_per_downloadclient>
{
[Test]
public void should_set_cdh_to_enabled()
{
var db = WithMigrationTestDb(c =>
{
c.Insert.IntoTable("DownloadClients").Row(new
{
Enable = 1,
Name = "Deluge",
Implementation = "Deluge",
Priority = 1,
Settings = new DelugeSettings85
{
Host = "127.0.0.1",
TvCategory = "abc",
UrlBase = "/my/"
}.ToJson(),
ConfigContract = "DelugeSettings"
});
});
var items = db.Query<DownloadClientDefinition158>("SELECT * FROM DownloadClients");
items.Should().HaveCount(1);
items.First().RemoveCompletedDownloads.Should().BeTrue();
items.First().RemoveFailedDownloads.Should().BeFalse();
}
[Test]
public void should_set_cdh_to_disabled_when_globally_disabled()
{
var db = WithMigrationTestDb(c =>
{
c.Insert.IntoTable("Config").Row(new
{
Key = "removecompleteddownloads",
Value = "False"
});
c.Insert.IntoTable("DownloadClients").Row(new
{
Enable = 1,
Name = "Deluge",
Implementation = "Deluge",
Priority = 1,
Settings = new DelugeSettings85
{
Host = "127.0.0.1",
TvCategory = "abc",
UrlBase = "/my/"
}.ToJson(),
ConfigContract = "DelugeSettings"
});
});
var items = db.Query<DownloadClientDefinition158>("SELECT * FROM DownloadClients");
items.Should().HaveCount(1);
items.First().RemoveCompletedDownloads.Should().BeFalse();
items.First().RemoveFailedDownloads.Should().BeFalse();
}
[Test]
public void should_disable_remove_for_existing_rtorrent()
{
var db = WithMigrationTestDb(c =>
{
c.Insert.IntoTable("DownloadClients").Row(new
{
Enable = 1,
Name = "RTorrent",
Implementation = "RTorrent",
Priority = 1,
Settings = new RTorrentSettings
{
Host = "127.0.0.1",
TvCategory = "abc",
UrlBase = "/my/"
}.ToJson(),
ConfigContract = "RTorrentSettings"
});
});
var items = db.Query<DownloadClientDefinition158>("SELECT * FROM DownloadClients");
items.Should().HaveCount(1);
items.First().RemoveCompletedDownloads.Should().BeFalse();
items.First().RemoveFailedDownloads.Should().BeFalse();
}
}
public class DownloadClientDefinition158
{
public int Id { get; set; }
public bool Enable { get; set; }
public int Priority { get; set; }
public string Name { get; set; }
public string Implementation { get; set; }
public JObject Settings { get; set; }
public string ConfigContract { get; set; }
public bool RemoveCompletedDownloads { get; set; }
public bool RemoveFailedDownloads { get; set; }
}
}

View File

@ -134,13 +134,6 @@ namespace NzbDrone.Core.Configuration
set { SetValue("EnableCompletedDownloadHandling", value); }
}
public bool RemoveCompletedDownloads
{
get { return GetValueBoolean("RemoveCompletedDownloads", false); }
set { SetValue("RemoveCompletedDownloads", value); }
}
public bool AutoRedownloadFailed
{
get { return GetValueBoolean("AutoRedownloadFailed", true); }
@ -148,13 +141,6 @@ namespace NzbDrone.Core.Configuration
set { SetValue("AutoRedownloadFailed", value); }
}
public bool RemoveFailedDownloads
{
get { return GetValueBoolean("RemoveFailedDownloads", true); }
set { SetValue("RemoveFailedDownloads", value); }
}
public bool CreateEmptySeriesFolders
{
get { return GetValueBoolean("CreateEmptySeriesFolders", false); }

View File

@ -19,10 +19,7 @@ namespace NzbDrone.Core.Configuration
//Completed/Failed Download Handling (Download client)
bool EnableCompletedDownloadHandling { get; set; }
bool RemoveCompletedDownloads { get; set; }
bool AutoRedownloadFailed { get; set; }
bool RemoveFailedDownloads { get; set; }
//Media Management
bool AutoUnmonitorPreviouslyDownloadedEpisodes { get; set; }

View File

@ -0,0 +1,51 @@
using System.Data;
using System.Linq;
using FluentMigrator;
using Newtonsoft.Json.Linq;
using NzbDrone.Common.Serializer;
using NzbDrone.Core.Datastore.Migration.Framework;
namespace NzbDrone.Core.Datastore.Migration
{
[Migration(158)]
public class cdh_per_downloadclient : NzbDroneMigrationBase
{
protected override void MainDbUpgrade()
{
Alter.Table("DownloadClients")
.AddColumn("RemoveCompletedDownloads").AsBoolean().NotNullable().WithDefaultValue(true)
.AddColumn("RemoveFailedDownloads").AsBoolean().NotNullable().WithDefaultValue(true);
Execute.WithConnection(MoveRemoveSettings);
}
private void MoveRemoveSettings(IDbConnection conn, IDbTransaction tran)
{
var removeCompletedDownloads = true;
var removeFailedDownloads = false;
using (var removeCompletedDownloadsCmd = conn.CreateCommand(tran, "SELECT Value FROM Config WHERE Key = 'removecompleteddownloads'"))
{
if ("False" == (removeCompletedDownloadsCmd.ExecuteScalar() as string))
removeCompletedDownloads = false;
}
using (var removeFailedDownloadsCmd = conn.CreateCommand(tran, "SELECT Value FROM Config WHERE Key = 'removefaileddownloads'"))
{
if ("True" == (removeFailedDownloadsCmd.ExecuteScalar() as string))
removeFailedDownloads = true;
}
using (var updateClientCmd = conn.CreateCommand(tran, $"UPDATE DownloadClients SET RemoveCompletedDownloads = (CASE WHEN Implementation = \"RTorrent\" THEN 0 ELSE ? END), RemoveFailedDownloads = ?"))
{
updateClientCmd.AddParameter(removeCompletedDownloads ? 1 : 0);
updateClientCmd.AddParameter(removeFailedDownloads ? 1 : 0);
updateClientCmd.ExecuteNonQuery();
}
using (var removeConfigCmd = conn.CreateCommand(tran, $"DELETE FROM Config WHERE Key IN ('removecompleteddownloads', 'removefaileddownloads')"))
{
removeConfigCmd.ExecuteNonQuery();
}
}
}
}

View File

@ -1,4 +1,5 @@
using FluentMigrator.Builders.Create;
using System.Data;
using FluentMigrator.Builders.Create;
using FluentMigrator.Builders.Create.Table;
namespace NzbDrone.Core.Datastore.Migration.Framework
@ -10,7 +11,16 @@ namespace NzbDrone.Core.Datastore.Migration.Framework
return expressionRoot.Table(name).WithColumn("Id").AsInt32().PrimaryKey().Identity();
}
public static void AddParameter(this System.Data.IDbCommand command, object value)
public static IDbCommand CreateCommand(this IDbConnection conn, IDbTransaction tran, string query)
{
var command = conn.CreateCommand();
command.Transaction = tran;
command.CommandText = query;
return command;
}
public static void AddParameter(this IDbCommand command, object value)
{
var parameter = command.CreateParameter();
parameter.Value = value;

View File

@ -168,7 +168,7 @@ namespace NzbDrone.Core.Download.Clients.RTorrent
// Check if torrent is finished and if it exceeds cached seedConfig
item.CanMoveFiles = item.CanBeRemoved =
torrent.IsFinished &&
torrent.IsFinished && seedConfig != null &&
(
(torrent.Ratio / 1000.0) >= seedConfig.Ratio ||
(DateTimeOffset.Now - DateTimeOffset.FromUnixTimeSeconds(torrent.FinishedTime)) >= seedConfig.SeedTime

View File

@ -7,5 +7,8 @@ namespace NzbDrone.Core.Download
{
public DownloadProtocol Protocol { get; set; }
public int Priority { get; set; } = 1;
public bool RemoveCompletedDownloads { get; set; } = true;
public bool RemoveFailedDownloads { get; set; } = true;
}
}

View File

@ -27,38 +27,65 @@ namespace NzbDrone.Core.Download
{
var trackedDownload = message.TrackedDownload;
if (trackedDownload == null || !trackedDownload.DownloadItem.CanBeRemoved || _configService.RemoveFailedDownloads == false)
if (trackedDownload == null ||
message.TrackedDownload.DownloadItem.Removed ||
!trackedDownload.DownloadItem.CanBeRemoved)
{
return;
}
RemoveFromDownloadClient(trackedDownload);
var downloadClient = _downloadClientProvider.Get(message.TrackedDownload.DownloadClient);
var definition = downloadClient.Definition as DownloadClientDefinition;
if (!definition.RemoveFailedDownloads)
{
return;
}
RemoveFromDownloadClient(trackedDownload, downloadClient);
}
public void Handle(DownloadCompletedEvent message)
{
if (_configService.RemoveCompletedDownloads &&
!message.TrackedDownload.DownloadItem.Removed &&
message.TrackedDownload.DownloadItem.CanBeRemoved &&
message.TrackedDownload.DownloadItem.Status != DownloadItemStatus.Downloading)
var trackedDownload = message.TrackedDownload;
var downloadClient = _downloadClientProvider.Get(trackedDownload.DownloadClient);
var definition = downloadClient.Definition as DownloadClientDefinition;
MarkItemAsImported(trackedDownload, downloadClient);
if (trackedDownload.DownloadItem.Removed ||
!trackedDownload.DownloadItem.CanBeRemoved ||
trackedDownload.DownloadItem.Status == DownloadItemStatus.Downloading)
{
RemoveFromDownloadClient(message.TrackedDownload);
return;
}
else
if (!definition.RemoveCompletedDownloads)
{
MarkItemAsImported(message.TrackedDownload);
}
return;
}
RemoveFromDownloadClient(message.TrackedDownload, downloadClient);
}
public void Handle(DownloadCanBeRemovedEvent message)
{
// Already verified that it can be removed, just needs to be removed
RemoveFromDownloadClient(message.TrackedDownload);
var trackedDownload = message.TrackedDownload;
var downloadClient = _downloadClientProvider.Get(trackedDownload.DownloadClient);
var definition = downloadClient.Definition as DownloadClientDefinition;
if (trackedDownload.DownloadItem.Removed ||
!trackedDownload.DownloadItem.CanBeRemoved ||
!definition.RemoveCompletedDownloads)
{
return;
}
RemoveFromDownloadClient(message.TrackedDownload, downloadClient);
}
private void RemoveFromDownloadClient(TrackedDownload trackedDownload)
private void RemoveFromDownloadClient(TrackedDownload trackedDownload, IDownloadClient downloadClient)
{
var downloadClient = _downloadClientProvider.Get(trackedDownload.DownloadClient);
try
{
_logger.Debug("[{0}] Removing download from {1} history", trackedDownload.DownloadItem.Title, trackedDownload.DownloadItem.DownloadClientInfo.Name);
@ -75,9 +102,8 @@ namespace NzbDrone.Core.Download
}
}
private void MarkItemAsImported(TrackedDownload trackedDownload)
private void MarkItemAsImported(TrackedDownload trackedDownload, IDownloadClient downloadClient)
{
var downloadClient = _downloadClientProvider.Get(trackedDownload.DownloadClient);
try
{
_logger.Debug("[{0}] Marking download as imported from {1}", trackedDownload.DownloadItem.Title, trackedDownload.DownloadItem.DownloadClientInfo.Name);

View File

@ -48,7 +48,6 @@ namespace NzbDrone.Core.Download
public void Execute(ProcessMonitoredDownloadsCommand message)
{
var enableCompletedDownloadHandling = _configService.EnableCompletedDownloadHandling;
var removeCompletedDownloads = _configService.RemoveCompletedDownloads;
var trackedDownloads = _trackedDownloadService.GetTrackedDownloads()
.Where(t => t.IsTrackable)
.ToList();
@ -73,10 +72,7 @@ namespace NzbDrone.Core.Download
}
// Imported downloads are no longer trackable so process them after processing trackable downloads
if (removeCompletedDownloads)
{
RemoveCompletedDownloads();
}
RemoveCompletedDownloads();
_eventAggregator.PublishEvent(new DownloadsProcessedEvent());
}

View File

@ -8,10 +8,7 @@ namespace Sonarr.Api.V3.Config
public string DownloadClientWorkingFolders { get; set; }
public bool EnableCompletedDownloadHandling { get; set; }
public bool RemoveCompletedDownloads { get; set; }
public bool AutoRedownloadFailed { get; set; }
public bool RemoveFailedDownloads { get; set; }
}
public static class DownloadClientConfigResourceMapper
@ -23,10 +20,7 @@ namespace Sonarr.Api.V3.Config
DownloadClientWorkingFolders = model.DownloadClientWorkingFolders,
EnableCompletedDownloadHandling = model.EnableCompletedDownloadHandling,
RemoveCompletedDownloads = model.RemoveCompletedDownloads,
AutoRedownloadFailed = model.AutoRedownloadFailed,
RemoveFailedDownloads = model.RemoveFailedDownloads
AutoRedownloadFailed = model.AutoRedownloadFailed
};
}
}

View File

@ -8,6 +8,8 @@ namespace Sonarr.Api.V3.DownloadClient
public bool Enable { get; set; }
public DownloadProtocol Protocol { get; set; }
public int Priority { get; set; }
public bool RemoveCompletedDownloads { get; set; }
public bool RemoveFailedDownloads { get; set; }
}
public class DownloadClientResourceMapper : ProviderResourceMapper<DownloadClientResource, DownloadClientDefinition>
@ -21,6 +23,8 @@ namespace Sonarr.Api.V3.DownloadClient
resource.Enable = definition.Enable;
resource.Protocol = definition.Protocol;
resource.Priority = definition.Priority;
resource.RemoveCompletedDownloads = definition.RemoveCompletedDownloads;
resource.RemoveFailedDownloads = definition.RemoveFailedDownloads;
return resource;
}
@ -34,6 +38,8 @@ namespace Sonarr.Api.V3.DownloadClient
definition.Enable = resource.Enable;
definition.Protocol = resource.Protocol;
definition.Priority = resource.Priority;
definition.RemoveCompletedDownloads = resource.RemoveCompletedDownloads;
definition.RemoveFailedDownloads = resource.RemoveFailedDownloads;
return definition;
}