From 0a6ec21c95dd451f0afd45f1bff20c178c75a4c4 Mon Sep 17 00:00:00 2001 From: Marius Nechifor Date: Sun, 15 Dec 2024 18:37:32 +0200 Subject: [PATCH] simplified how the download client selection works (#22) --- README.md | 12 ++-- .../DownloadClient/DelugeConfig.cs | 7 --- .../DownloadClient/QBitConfig.cs | 7 --- .../DownloadClient/TransmissionConfig.cs | 9 +-- .../Configuration/EnvironmentVariables.cs | 6 ++ code/Domain/Enums/DownloadClient.cs | 8 +++ .../DependencyInjection/ConfigurationDI.cs | 1 + code/Executable/appsettings.Development.json | 4 +- code/Executable/appsettings.json | 4 +- .../DownloadClient/Deluge/DelugeService.cs | 1 + .../DownloadClient/DownloadServiceFactory.cs | 61 +++++-------------- .../DownloadClient/QBittorrent/QBitService.cs | 1 + .../Transmission/TransmissionService.cs | 1 + code/test/docker-compose.yml | 4 +- 14 files changed, 41 insertions(+), 85 deletions(-) create mode 100644 code/Common/Configuration/EnvironmentVariables.cs create mode 100644 code/Domain/Enums/DownloadClient.cs diff --git a/README.md b/README.md index 2a16bab7..4e972405 100644 --- a/README.md +++ b/README.md @@ -86,16 +86,14 @@ services: # - CONTENTBLOCKER__WHITELIST__ENABLED=true # - 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__USERNAME=user - QBITTORRENT__PASSWORD=pass # OR - # - DELUGE__ENABLED=true # - DELUGE__URL=http://localhost:8112 # - DELUGE__PASSWORD=testing # OR - # - TRANSMISSION__ENABLED=true # - TRANSMISSION__URL=http://localhost:9091 # - TRANSMISSION__USERNAME=test # - TRANSMISSION__PASSWORD=testing @@ -133,20 +131,18 @@ services: ||||| | 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__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)
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__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)
Needs to be json compatible | empty | ||||| -| QBITTORRENT__ENABLED | No | Enable or disable qBittorrent | true | +| DOWNLOAD_CLIENT | No | Download client that is used by *arrs
Can be `qbittorrent`, `deluge` or `transmission` | `qbittorrent` | | QBITTORRENT__URL | No | qBittorrent instance url | http://localhost:8112 | | QBITTORRENT__USERNAME | No | qBittorrent user | 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__PASSWORD | No | Deluge password | empty | ||||| -| TRANSMISSION__ENABLED | No | Enable or disable Transmission | true | | TRANSMISSION__URL | No | Transmission instance url | http://localhost:9091 | | TRANSMISSION__USERNAME | No | Transmission user | empty | | TRANSMISSION__PASSWORD | No | Transmission password | empty | diff --git a/code/Common/Configuration/DownloadClient/DelugeConfig.cs b/code/Common/Configuration/DownloadClient/DelugeConfig.cs index 1340e5aa..18044208 100644 --- a/code/Common/Configuration/DownloadClient/DelugeConfig.cs +++ b/code/Common/Configuration/DownloadClient/DelugeConfig.cs @@ -4,19 +4,12 @@ public sealed record DelugeConfig : IConfig { public const string SectionName = "Deluge"; - public required bool Enabled { get; init; } - public Uri? Url { get; init; } public string? Password { get; init; } public void Validate() { - if (!Enabled) - { - return; - } - if (Url is null) { throw new ArgumentNullException(nameof(Url)); diff --git a/code/Common/Configuration/DownloadClient/QBitConfig.cs b/code/Common/Configuration/DownloadClient/QBitConfig.cs index 2b1fe674..b2b3cbea 100644 --- a/code/Common/Configuration/DownloadClient/QBitConfig.cs +++ b/code/Common/Configuration/DownloadClient/QBitConfig.cs @@ -4,8 +4,6 @@ public sealed class QBitConfig : IConfig { public const string SectionName = "qBittorrent"; - public required bool Enabled { get; init; } - public Uri? Url { get; init; } public string? Username { get; init; } @@ -14,11 +12,6 @@ public sealed class QBitConfig : IConfig public void Validate() { - if (!Enabled) - { - return; - } - if (Url is null) { throw new ArgumentNullException(nameof(Url)); diff --git a/code/Common/Configuration/DownloadClient/TransmissionConfig.cs b/code/Common/Configuration/DownloadClient/TransmissionConfig.cs index a9e08dfe..30e94b47 100644 --- a/code/Common/Configuration/DownloadClient/TransmissionConfig.cs +++ b/code/Common/Configuration/DownloadClient/TransmissionConfig.cs @@ -1,11 +1,9 @@ namespace Common.Configuration.DownloadClient; -public record TransmissionConfig +public record TransmissionConfig : IConfig { public const string SectionName = "Transmission"; - public required bool Enabled { get; init; } - public Uri? Url { get; init; } public string? Username { get; init; } @@ -14,11 +12,6 @@ public record TransmissionConfig public void Validate() { - if (!Enabled) - { - return; - } - if (Url is null) { throw new ArgumentNullException(nameof(Url)); diff --git a/code/Common/Configuration/EnvironmentVariables.cs b/code/Common/Configuration/EnvironmentVariables.cs new file mode 100644 index 00000000..9ee9d4b5 --- /dev/null +++ b/code/Common/Configuration/EnvironmentVariables.cs @@ -0,0 +1,6 @@ +namespace Common.Configuration; + +public static class EnvironmentVariables +{ + public const string DownloadClient = "DOWNLOAD_CLIENT"; +} \ No newline at end of file diff --git a/code/Domain/Enums/DownloadClient.cs b/code/Domain/Enums/DownloadClient.cs new file mode 100644 index 00000000..8f67021c --- /dev/null +++ b/code/Domain/Enums/DownloadClient.cs @@ -0,0 +1,8 @@ +namespace Domain.Enums; + +public enum DownloadClient +{ + QBittorrent, + Deluge, + Transmission +} \ No newline at end of file diff --git a/code/Executable/DependencyInjection/ConfigurationDI.cs b/code/Executable/DependencyInjection/ConfigurationDI.cs index fd34bc7e..d955f3b0 100644 --- a/code/Executable/DependencyInjection/ConfigurationDI.cs +++ b/code/Executable/DependencyInjection/ConfigurationDI.cs @@ -3,6 +3,7 @@ using Common.Configuration.Arr; using Common.Configuration.ContentBlocker; using Common.Configuration.DownloadClient; using Common.Configuration.Logging; +using Domain.Enums; namespace Executable.DependencyInjection; diff --git a/code/Executable/appsettings.Development.json b/code/Executable/appsettings.Development.json index bcc3d338..aea90457 100644 --- a/code/Executable/appsettings.Development.json +++ b/code/Executable/appsettings.Development.json @@ -26,19 +26,17 @@ "Enabled": true, "RunSequentially": true }, + "DOWNLOAD_CLIENT": "qbittorrent", "qBittorrent": { - "Enabled": true, "Url": "http://localhost:8080", "Username": "test", "Password": "testing" }, "Deluge": { - "Enabled": false, "Url": "http://localhost:8112", "Password": "testing" }, "Transmission": { - "Enabled": false, "Url": "http://localhost:9091", "Username": "test", "Password": "testing" diff --git a/code/Executable/appsettings.json b/code/Executable/appsettings.json index c9c6d995..4c1011a3 100644 --- a/code/Executable/appsettings.json +++ b/code/Executable/appsettings.json @@ -26,19 +26,17 @@ "Enabled": true, "RunSequentially": true }, + "DOWNLOAD_CLIENT": "qbittorrent", "qBittorrent": { - "Enabled": true, "Url": "http://localhost:8080", "Username": "", "Password": "" }, "Deluge": { - "Enabled": false, "Url": "http://localhost:8112", "Password": "testing" }, "Transmission": { - "Enabled": false, "Url": "http://localhost:9091", "Username": "test", "Password": "testing" diff --git a/code/Infrastructure/Verticals/DownloadClient/Deluge/DelugeService.cs b/code/Infrastructure/Verticals/DownloadClient/Deluge/DelugeService.cs index a3293586..484fd8f5 100644 --- a/code/Infrastructure/Verticals/DownloadClient/Deluge/DelugeService.cs +++ b/code/Infrastructure/Verticals/DownloadClient/Deluge/DelugeService.cs @@ -21,6 +21,7 @@ public sealed class DelugeService : IDownloadService ) { _logger = logger; + config.Value.Validate(); _client = new (config, httpClientFactory); _filenameEvaluator = filenameEvaluator; } diff --git a/code/Infrastructure/Verticals/DownloadClient/DownloadServiceFactory.cs b/code/Infrastructure/Verticals/DownloadClient/DownloadServiceFactory.cs index 6f90fad7..81390788 100644 --- a/code/Infrastructure/Verticals/DownloadClient/DownloadServiceFactory.cs +++ b/code/Infrastructure/Verticals/DownloadClient/DownloadServiceFactory.cs @@ -3,6 +3,7 @@ using Common.Configuration.DownloadClient; using Infrastructure.Verticals.DownloadClient.Deluge; using Infrastructure.Verticals.DownloadClient.QBittorrent; using Infrastructure.Verticals.DownloadClient.Transmission; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; @@ -10,57 +11,25 @@ namespace Infrastructure.Verticals.DownloadClient; public sealed class DownloadServiceFactory { - private readonly QBitConfig _qBitConfig; - private readonly DelugeConfig _delugeConfig; - private readonly TransmissionConfig _transmissionConfig; private readonly IServiceProvider _serviceProvider; + private readonly Domain.Enums.DownloadClient _downloadClient; - public DownloadServiceFactory( - IOptions qBitConfig, - IOptions delugeConfig, - IOptions transmissionConfig, - IServiceProvider serviceProvider) + public DownloadServiceFactory(IServiceProvider serviceProvider, IConfiguration configuration) { - _qBitConfig = qBitConfig.Value; - _delugeConfig = delugeConfig.Value; - _transmissionConfig = transmissionConfig.Value; _serviceProvider = serviceProvider; - - _qBitConfig.Validate(); - _delugeConfig.Validate(); - _transmissionConfig.Validate(); - - 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"); - } + _downloadClient = (Domain.Enums.DownloadClient)Enum.Parse( + typeof(Domain.Enums.DownloadClient), + configuration[EnvironmentVariables.DownloadClient] ?? Domain.Enums.DownloadClient.QBittorrent.ToString(), + true + ); } - public IDownloadService CreateDownloadClient() - { - if (_qBitConfig.Enabled) + public IDownloadService CreateDownloadClient() => + _downloadClient switch { - return _serviceProvider.GetRequiredService(); - } - - if (_delugeConfig.Enabled) - { - return _serviceProvider.GetRequiredService(); - } - - if (_transmissionConfig.Enabled) - { - return _serviceProvider.GetRequiredService(); - } - - throw new NotSupportedException(); - } + Domain.Enums.DownloadClient.QBittorrent => _serviceProvider.GetRequiredService(), + Domain.Enums.DownloadClient.Deluge => _serviceProvider.GetRequiredService(), + Domain.Enums.DownloadClient.Transmission => _serviceProvider.GetRequiredService(), + _ => throw new ArgumentOutOfRangeException() + }; } \ No newline at end of file diff --git a/code/Infrastructure/Verticals/DownloadClient/QBittorrent/QBitService.cs b/code/Infrastructure/Verticals/DownloadClient/QBittorrent/QBitService.cs index 1f2e4b03..7a09f605 100644 --- a/code/Infrastructure/Verticals/DownloadClient/QBittorrent/QBitService.cs +++ b/code/Infrastructure/Verticals/DownloadClient/QBittorrent/QBitService.cs @@ -22,6 +22,7 @@ public sealed class QBitService : IDownloadService { _logger = logger; _config = config.Value; + _config.Validate(); _client = new(_config.Url); _filenameEvaluator = filenameEvaluator; } diff --git a/code/Infrastructure/Verticals/DownloadClient/Transmission/TransmissionService.cs b/code/Infrastructure/Verticals/DownloadClient/Transmission/TransmissionService.cs index ecdd5b50..764d4e19 100644 --- a/code/Infrastructure/Verticals/DownloadClient/Transmission/TransmissionService.cs +++ b/code/Infrastructure/Verticals/DownloadClient/Transmission/TransmissionService.cs @@ -25,6 +25,7 @@ public sealed class TransmissionService : IDownloadService { _logger = logger; _config = config.Value; + _config.Validate(); _client = new( new Uri(_config.Url, "/transmission/rpc").ToString(), login: _config.Username, diff --git a/code/test/docker-compose.yml b/code/test/docker-compose.yml index 834b3df6..910eab91 100644 --- a/code/test/docker-compose.yml +++ b/code/test/docker-compose.yml @@ -189,16 +189,14 @@ services: # - CONTENTBLOCKER__WHITELIST__ENABLED=true # - 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__USERNAME=test - QBITTORRENT__PASSWORD=testing # OR - # - DELUGE__ENABLED=true # - DELUGE__URL=http://localhost:8112 # - DELUGE__PASSWORD=testing # OR - # - TRANSMISSION__ENABLED=true # - TRANSMISSION__URL=http://localhost:9091 # - TRANSMISSION__USERNAME=test # - TRANSMISSION__PASSWORD=testing