simplified how the download client selection works (#22)

This commit is contained in:
Marius Nechifor
2024-12-15 18:37:32 +02:00
parent 74c49f041d
commit 0a6ec21c95
14 changed files with 41 additions and 85 deletions
+4 -8
View File
@@ -86,16 +86,14 @@ services:
# - CONTENTBLOCKER__WHITELIST__ENABLED=true # - CONTENTBLOCKER__WHITELIST__ENABLED=true
# - CONTENTBLOCKER__BLACKLIST__PATH=https://raw.githubusercontent.com/flmorg/cleanuperr/refs/heads/main/whitelist # - CONTENTBLOCKER__BLACKLIST__PATH=https://raw.githubusercontent.com/flmorg/cleanuperr/refs/heads/main/whitelist
- QBITTORRENT__ENABLED=true - DOWNLOAD_CLIENT=qBittorrent
- QBITTORRENT__URL=http://localhost:8080 - QBITTORRENT__URL=http://localhost:8080
- QBITTORRENT__USERNAME=user - QBITTORRENT__USERNAME=user
- QBITTORRENT__PASSWORD=pass - QBITTORRENT__PASSWORD=pass
# OR # OR
# - DELUGE__ENABLED=true
# - DELUGE__URL=http://localhost:8112 # - DELUGE__URL=http://localhost:8112
# - DELUGE__PASSWORD=testing # - DELUGE__PASSWORD=testing
# OR # OR
# - TRANSMISSION__ENABLED=true
# - TRANSMISSION__URL=http://localhost:9091 # - TRANSMISSION__URL=http://localhost:9091
# - TRANSMISSION__USERNAME=test # - TRANSMISSION__USERNAME=test
# - TRANSMISSION__PASSWORD=testing # - TRANSMISSION__PASSWORD=testing
@@ -133,20 +131,18 @@ services:
||||| |||||
| CONTENTBLOCKER__ENABLED | No | Enable or disable the content blocker | false | | CONTENTBLOCKER__ENABLED | No | Enable or disable the content blocker | false |
| CONTENTBLOCKER__BLACKLIST__ENABLED | Yes if content blocker is enabled and whitelist is not enabled | Enable or disable the blacklist | false | | CONTENTBLOCKER__BLACKLIST__ENABLED | Yes if content blocker is enabled and whitelist is not enabled | Enable or disable the blacklist | false |
| CONTENTBLOCKER__BLACKLIST__PATH | Yes if blacklist is enabled | Path to the blacklist (local file or url); Needs to be json compatible | empty | | CONTENTBLOCKER__BLACKLIST__PATH | Yes if blacklist is enabled | Path to the blacklist (local file or url)<br>Needs to be json compatible | empty |
| CONTENTBLOCKER__WHITELIST__ENABLED | Yes if content blocker is enabled and blacklist is not enabled | Enable or disable the whitelist | false | | CONTENTBLOCKER__WHITELIST__ENABLED | Yes if content blocker is enabled and blacklist is not enabled | Enable or disable the whitelist | false |
| CONTENTBLOCKER__BLACKLIST__PATH | Yes if whitelist is enabled | Path to the whitelist (local file or url); Needs to be json compatible | empty | | CONTENTBLOCKER__BLACKLIST__PATH | Yes if whitelist is enabled | Path to the whitelist (local file or url)<br>Needs to be json compatible | empty |
||||| |||||
| QBITTORRENT__ENABLED | No | Enable or disable qBittorrent | true | | DOWNLOAD_CLIENT | No | Download client that is used by *arrs<br>Can be `qbittorrent`, `deluge` or `transmission` | `qbittorrent` |
| QBITTORRENT__URL | No | qBittorrent instance url | http://localhost:8112 | | QBITTORRENT__URL | No | qBittorrent instance url | http://localhost:8112 |
| QBITTORRENT__USERNAME | No | qBittorrent user | empty | | QBITTORRENT__USERNAME | No | qBittorrent user | empty |
| QBITTORRENT__PASSWORD | No | qBittorrent password | empty | | QBITTORRENT__PASSWORD | No | qBittorrent password | empty |
||||| |||||
| DELUGE__ENABLED | No | Enable or disable Deluge | false |
| DELUGE__URL | No | Deluge instance url | http://localhost:8080 | | DELUGE__URL | No | Deluge instance url | http://localhost:8080 |
| DELUGE__PASSWORD | No | Deluge password | empty | | DELUGE__PASSWORD | No | Deluge password | empty |
||||| |||||
| TRANSMISSION__ENABLED | No | Enable or disable Transmission | true |
| TRANSMISSION__URL | No | Transmission instance url | http://localhost:9091 | | TRANSMISSION__URL | No | Transmission instance url | http://localhost:9091 |
| TRANSMISSION__USERNAME | No | Transmission user | empty | | TRANSMISSION__USERNAME | No | Transmission user | empty |
| TRANSMISSION__PASSWORD | No | Transmission password | empty | | TRANSMISSION__PASSWORD | No | Transmission password | empty |
@@ -4,19 +4,12 @@ public sealed record DelugeConfig : IConfig
{ {
public const string SectionName = "Deluge"; public const string SectionName = "Deluge";
public required bool Enabled { get; init; }
public Uri? Url { get; init; } public Uri? Url { get; init; }
public string? Password { get; init; } public string? Password { get; init; }
public void Validate() public void Validate()
{ {
if (!Enabled)
{
return;
}
if (Url is null) if (Url is null)
{ {
throw new ArgumentNullException(nameof(Url)); throw new ArgumentNullException(nameof(Url));
@@ -4,8 +4,6 @@ public sealed class QBitConfig : IConfig
{ {
public const string SectionName = "qBittorrent"; public const string SectionName = "qBittorrent";
public required bool Enabled { get; init; }
public Uri? Url { get; init; } public Uri? Url { get; init; }
public string? Username { get; init; } public string? Username { get; init; }
@@ -14,11 +12,6 @@ public sealed class QBitConfig : IConfig
public void Validate() public void Validate()
{ {
if (!Enabled)
{
return;
}
if (Url is null) if (Url is null)
{ {
throw new ArgumentNullException(nameof(Url)); throw new ArgumentNullException(nameof(Url));
@@ -1,11 +1,9 @@
namespace Common.Configuration.DownloadClient; namespace Common.Configuration.DownloadClient;
public record TransmissionConfig public record TransmissionConfig : IConfig
{ {
public const string SectionName = "Transmission"; public const string SectionName = "Transmission";
public required bool Enabled { get; init; }
public Uri? Url { get; init; } public Uri? Url { get; init; }
public string? Username { get; init; } public string? Username { get; init; }
@@ -14,11 +12,6 @@ public record TransmissionConfig
public void Validate() public void Validate()
{ {
if (!Enabled)
{
return;
}
if (Url is null) if (Url is null)
{ {
throw new ArgumentNullException(nameof(Url)); throw new ArgumentNullException(nameof(Url));
@@ -0,0 +1,6 @@
namespace Common.Configuration;
public static class EnvironmentVariables
{
public const string DownloadClient = "DOWNLOAD_CLIENT";
}
+8
View File
@@ -0,0 +1,8 @@
namespace Domain.Enums;
public enum DownloadClient
{
QBittorrent,
Deluge,
Transmission
}
@@ -3,6 +3,7 @@ using Common.Configuration.Arr;
using Common.Configuration.ContentBlocker; using Common.Configuration.ContentBlocker;
using Common.Configuration.DownloadClient; using Common.Configuration.DownloadClient;
using Common.Configuration.Logging; using Common.Configuration.Logging;
using Domain.Enums;
namespace Executable.DependencyInjection; namespace Executable.DependencyInjection;
+1 -3
View File
@@ -26,19 +26,17 @@
"Enabled": true, "Enabled": true,
"RunSequentially": true "RunSequentially": true
}, },
"DOWNLOAD_CLIENT": "qbittorrent",
"qBittorrent": { "qBittorrent": {
"Enabled": true,
"Url": "http://localhost:8080", "Url": "http://localhost:8080",
"Username": "test", "Username": "test",
"Password": "testing" "Password": "testing"
}, },
"Deluge": { "Deluge": {
"Enabled": false,
"Url": "http://localhost:8112", "Url": "http://localhost:8112",
"Password": "testing" "Password": "testing"
}, },
"Transmission": { "Transmission": {
"Enabled": false,
"Url": "http://localhost:9091", "Url": "http://localhost:9091",
"Username": "test", "Username": "test",
"Password": "testing" "Password": "testing"
+1 -3
View File
@@ -26,19 +26,17 @@
"Enabled": true, "Enabled": true,
"RunSequentially": true "RunSequentially": true
}, },
"DOWNLOAD_CLIENT": "qbittorrent",
"qBittorrent": { "qBittorrent": {
"Enabled": true,
"Url": "http://localhost:8080", "Url": "http://localhost:8080",
"Username": "", "Username": "",
"Password": "" "Password": ""
}, },
"Deluge": { "Deluge": {
"Enabled": false,
"Url": "http://localhost:8112", "Url": "http://localhost:8112",
"Password": "testing" "Password": "testing"
}, },
"Transmission": { "Transmission": {
"Enabled": false,
"Url": "http://localhost:9091", "Url": "http://localhost:9091",
"Username": "test", "Username": "test",
"Password": "testing" "Password": "testing"
@@ -21,6 +21,7 @@ public sealed class DelugeService : IDownloadService
) )
{ {
_logger = logger; _logger = logger;
config.Value.Validate();
_client = new (config, httpClientFactory); _client = new (config, httpClientFactory);
_filenameEvaluator = filenameEvaluator; _filenameEvaluator = filenameEvaluator;
} }
@@ -3,6 +3,7 @@ using Common.Configuration.DownloadClient;
using Infrastructure.Verticals.DownloadClient.Deluge; using Infrastructure.Verticals.DownloadClient.Deluge;
using Infrastructure.Verticals.DownloadClient.QBittorrent; using Infrastructure.Verticals.DownloadClient.QBittorrent;
using Infrastructure.Verticals.DownloadClient.Transmission; using Infrastructure.Verticals.DownloadClient.Transmission;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
@@ -10,57 +11,25 @@ namespace Infrastructure.Verticals.DownloadClient;
public sealed class DownloadServiceFactory public sealed class DownloadServiceFactory
{ {
private readonly QBitConfig _qBitConfig;
private readonly DelugeConfig _delugeConfig;
private readonly TransmissionConfig _transmissionConfig;
private readonly IServiceProvider _serviceProvider; private readonly IServiceProvider _serviceProvider;
private readonly Domain.Enums.DownloadClient _downloadClient;
public DownloadServiceFactory( public DownloadServiceFactory(IServiceProvider serviceProvider, IConfiguration configuration)
IOptions<QBitConfig> qBitConfig,
IOptions<DelugeConfig> delugeConfig,
IOptions<TransmissionConfig> transmissionConfig,
IServiceProvider serviceProvider)
{ {
_qBitConfig = qBitConfig.Value;
_delugeConfig = delugeConfig.Value;
_transmissionConfig = transmissionConfig.Value;
_serviceProvider = serviceProvider; _serviceProvider = serviceProvider;
_downloadClient = (Domain.Enums.DownloadClient)Enum.Parse(
_qBitConfig.Validate(); typeof(Domain.Enums.DownloadClient),
_delugeConfig.Validate(); configuration[EnvironmentVariables.DownloadClient] ?? Domain.Enums.DownloadClient.QBittorrent.ToString(),
_transmissionConfig.Validate(); true
);
int enabledCount = new[] { _qBitConfig.Enabled, _delugeConfig.Enabled, _transmissionConfig.Enabled }
.Count(enabled => enabled);
if (enabledCount > 1)
{
throw new Exception("only one download client can be enabled");
}
if (enabledCount == 0)
{
throw new Exception("no download client is enabled");
}
} }
public IDownloadService CreateDownloadClient() public IDownloadService CreateDownloadClient() =>
{ _downloadClient switch
if (_qBitConfig.Enabled)
{ {
return _serviceProvider.GetRequiredService<QBitService>(); Domain.Enums.DownloadClient.QBittorrent => _serviceProvider.GetRequiredService<QBitService>(),
} Domain.Enums.DownloadClient.Deluge => _serviceProvider.GetRequiredService<DelugeService>(),
Domain.Enums.DownloadClient.Transmission => _serviceProvider.GetRequiredService<TransmissionService>(),
if (_delugeConfig.Enabled) _ => throw new ArgumentOutOfRangeException()
{ };
return _serviceProvider.GetRequiredService<DelugeService>();
}
if (_transmissionConfig.Enabled)
{
return _serviceProvider.GetRequiredService<TransmissionService>();
}
throw new NotSupportedException();
}
} }
@@ -22,6 +22,7 @@ public sealed class QBitService : IDownloadService
{ {
_logger = logger; _logger = logger;
_config = config.Value; _config = config.Value;
_config.Validate();
_client = new(_config.Url); _client = new(_config.Url);
_filenameEvaluator = filenameEvaluator; _filenameEvaluator = filenameEvaluator;
} }
@@ -25,6 +25,7 @@ public sealed class TransmissionService : IDownloadService
{ {
_logger = logger; _logger = logger;
_config = config.Value; _config = config.Value;
_config.Validate();
_client = new( _client = new(
new Uri(_config.Url, "/transmission/rpc").ToString(), new Uri(_config.Url, "/transmission/rpc").ToString(),
login: _config.Username, login: _config.Username,
+1 -3
View File
@@ -189,16 +189,14 @@ services:
# - CONTENTBLOCKER__WHITELIST__ENABLED=true # - CONTENTBLOCKER__WHITELIST__ENABLED=true
# - CONTENTBLOCKER__BLACKLIST__PATH=https://raw.githubusercontent.com/flmorg/cleanuperr/refs/heads/main/whitelist # - CONTENTBLOCKER__BLACKLIST__PATH=https://raw.githubusercontent.com/flmorg/cleanuperr/refs/heads/main/whitelist
- QBITTORRENT__ENABLED=true - DOWNLOAD_CLIENT=qbittorrent
- QBITTORRENT__URL=http://qbittorrent:8080 - QBITTORRENT__URL=http://qbittorrent:8080
- QBITTORRENT__USERNAME=test - QBITTORRENT__USERNAME=test
- QBITTORRENT__PASSWORD=testing - QBITTORRENT__PASSWORD=testing
# OR # OR
# - DELUGE__ENABLED=true
# - DELUGE__URL=http://localhost:8112 # - DELUGE__URL=http://localhost:8112
# - DELUGE__PASSWORD=testing # - DELUGE__PASSWORD=testing
# OR # OR
# - TRANSMISSION__ENABLED=true
# - TRANSMISSION__URL=http://localhost:9091 # - TRANSMISSION__URL=http://localhost:9091
# - TRANSMISSION__USERNAME=test # - TRANSMISSION__USERNAME=test
# - TRANSMISSION__PASSWORD=testing # - TRANSMISSION__PASSWORD=testing