diff --git a/code/Common/Configuration/Arr/ArrConfig.cs b/code/Common/Configuration/Arr/ArrConfig.cs index 40ded3a5..92e6f351 100644 --- a/code/Common/Configuration/Arr/ArrConfig.cs +++ b/code/Common/Configuration/Arr/ArrConfig.cs @@ -2,7 +2,7 @@ using Microsoft.Extensions.Configuration; namespace Common.Configuration.Arr; -public abstract class ArrConfig +public abstract class ArrConfig : IConfig { public bool Enabled { get; init; } @@ -10,6 +10,6 @@ public abstract class ArrConfig public short ImportFailedMaxStrikes { get; init; } = -1; public List Instances { get; init; } = []; -} -// Block struct moved to ContentBlockerConfig.cs as BlocklistSettings \ No newline at end of file + public abstract void Validate(); +} \ No newline at end of file diff --git a/code/Common/Configuration/Arr/ArrInstance.cs b/code/Common/Configuration/Arr/ArrInstance.cs index 22579707..9330f316 100644 --- a/code/Common/Configuration/Arr/ArrInstance.cs +++ b/code/Common/Configuration/Arr/ArrInstance.cs @@ -2,6 +2,8 @@ public sealed class ArrInstance { + public Guid Id { get; set; } = Guid.NewGuid(); + public required string Name { get; set; } public required Uri Url { get; set; } diff --git a/code/Common/Configuration/Arr/LidarrConfig.cs b/code/Common/Configuration/Arr/LidarrConfig.cs index 6b4f0a3f..a28aa646 100644 --- a/code/Common/Configuration/Arr/LidarrConfig.cs +++ b/code/Common/Configuration/Arr/LidarrConfig.cs @@ -1,11 +1,8 @@ namespace Common.Configuration.Arr; -public sealed class LidarrConfig : ArrConfig, IConfig +public sealed class LidarrConfig : ArrConfig { - public const string SectionName = "Lidarr"; - - public void Validate() + public override void Validate() { - throw new NotImplementedException(); } } \ No newline at end of file diff --git a/code/Common/Configuration/Arr/RadarrConfig.cs b/code/Common/Configuration/Arr/RadarrConfig.cs index b32c3dd4..33973e7d 100644 --- a/code/Common/Configuration/Arr/RadarrConfig.cs +++ b/code/Common/Configuration/Arr/RadarrConfig.cs @@ -1,11 +1,8 @@ namespace Common.Configuration.Arr; -public sealed class RadarrConfig : ArrConfig, IConfig +public sealed class RadarrConfig : ArrConfig { - public const string SectionName = "Radarr"; - - public void Validate() + public override void Validate() { - throw new NotImplementedException(); } } \ No newline at end of file diff --git a/code/Common/Configuration/Arr/SonarrConfig.cs b/code/Common/Configuration/Arr/SonarrConfig.cs index d5566dec..5133cb49 100644 --- a/code/Common/Configuration/Arr/SonarrConfig.cs +++ b/code/Common/Configuration/Arr/SonarrConfig.cs @@ -1,13 +1,10 @@ namespace Common.Configuration.Arr; -public sealed class SonarrConfig : ArrConfig, IConfig +public sealed class SonarrConfig : ArrConfig { - public const string SectionName = "Sonarr"; + public SonarrSearchType SearchType { get; init; } = SonarrSearchType.Episode; - public SonarrSearchType SearchType { get; init; } - - public void Validate() + public override void Validate() { - throw new NotImplementedException(); } } \ No newline at end of file diff --git a/code/Executable/DependencyInjection/ConfigurationDI.cs b/code/Executable/DependencyInjection/ConfigurationDI.cs index d53c95e3..ecbf700a 100644 --- a/code/Executable/DependencyInjection/ConfigurationDI.cs +++ b/code/Executable/DependencyInjection/ConfigurationDI.cs @@ -11,9 +11,8 @@ public static class ConfigurationDI // Instead, we rely solely on JSON configuration files // Define the configuration directory - string configDirectory = Path.Combine( - Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory) ?? AppDomain.CurrentDomain.BaseDirectory, - "config"); + // TODO change for docker containers + string configDirectory = "config"; // Ensure the configuration directory exists Directory.CreateDirectory(configDirectory); diff --git a/code/Infrastructure/Configuration/ConfigInitializer.cs b/code/Infrastructure/Configuration/ConfigInitializer.cs index 44c6039f..d43420e5 100644 --- a/code/Infrastructure/Configuration/ConfigInitializer.cs +++ b/code/Infrastructure/Configuration/ConfigInitializer.cs @@ -1,3 +1,4 @@ +using Common.Configuration.ContentBlocker; using Microsoft.Extensions.Logging; namespace Infrastructure.Configuration; @@ -37,41 +38,81 @@ public class ConfigInitializer private async Task EnsureContentBlockerConfigAsync() { - _ = await _configManager.GetContentBlockerConfigAsync(); + var config = await _configManager.GetContentBlockerConfigAsync(); + + if (config is null) + { + await _configManager.SaveContentBlockerConfigAsync(new()); + } } private async Task EnsureQueueCleanerConfigAsync() { - _ = await _configManager.GetQueueCleanerConfigAsync(); + var config = await _configManager.GetQueueCleanerConfigAsync(); + + if (config is null) + { + await _configManager.SaveQueueCleanerConfigAsync(new()); + } } private async Task EnsureDownloadCleanerConfigAsync() { - _ = await _configManager.GetDownloadCleanerConfigAsync(); + var config = await _configManager.GetDownloadCleanerConfigAsync(); + + if (config is null) + { + await _configManager.SaveDownloadCleanerConfigAsync(new()); + } } private async Task EnsureDownloadClientConfigAsync() { - _ = await _configManager.GetDownloadClientConfigAsync(); + var config = await _configManager.GetDownloadClientConfigAsync(); + + if (config is null) + { + await _configManager.SaveDownloadClientConfigAsync(new()); + } } private async Task EnsureSonarrConfigAsync() { - _ = await _configManager.GetSonarrConfigAsync(); + var config = await _configManager.GetSonarrConfigAsync(); + + if (config is null) + { + await _configManager.SaveSonarrConfigAsync(new()); + } } private async Task EnsureRadarrConfigAsync() { - _ = await _configManager.GetRadarrConfigAsync(); + var config = await _configManager.GetRadarrConfigAsync(); + + if (config is null) + { + await _configManager.SaveRadarrConfigAsync(new()); + } } private async Task EnsureLidarrConfigAsync() { - _ = await _configManager.GetLidarrConfigAsync(); + var config = await _configManager.GetLidarrConfigAsync(); + + if (config is null) + { + await _configManager.SaveLidarrConfigAsync(new()); + } } private async Task EnsureIgnoredDownloadsConfigAsync() { - _ = await _configManager.GetIgnoredDownloadsConfigAsync(); + var config = await _configManager.GetIgnoredDownloadsConfigAsync(); + + if (config is null) + { + await _configManager.SaveIgnoredDownloadsConfigAsync(new()); + } } } diff --git a/code/Infrastructure/Configuration/ConfigManager.cs b/code/Infrastructure/Configuration/ConfigManager.cs index 6afd0657..d0195b00 100644 --- a/code/Infrastructure/Configuration/ConfigManager.cs +++ b/code/Infrastructure/Configuration/ConfigManager.cs @@ -77,11 +77,12 @@ public class ConfigManager : IConfigManager private const string SonarrConfigFile = "sonarr.json"; private const string RadarrConfigFile = "radarr.json"; private const string LidarrConfigFile = "lidarr.json"; - private const string ContentBlockerConfigFile = "contentblocker.json"; - private const string QueueCleanerConfigFile = "queuecleaner.json"; - private const string DownloadCleanerConfigFile = "downloadcleaner.json"; - private const string DownloadClientConfigFile = "downloadclient.json"; - private const string IgnoredDownloadsConfigFile = "ignoreddownloads.json"; + private const string ContentBlockerConfigFile = "content_blocker.json"; + private const string QueueCleanerConfigFile = "queue_cleaner.json"; + private const string DownloadCleanerConfigFile = "download_cleaner.json"; + private const string DownloadClientConfigFile = "download_client.json"; + private const string IgnoredDownloadsConfigFile = "ignored_downloads.json"; + private const string GeneralSettings = "general.json"; public ConfigManager( ILogger logger, diff --git a/code/Infrastructure/Configuration/JsonConfigurationProvider.cs b/code/Infrastructure/Configuration/JsonConfigurationProvider.cs index 09994598..61069850 100644 --- a/code/Infrastructure/Configuration/JsonConfigurationProvider.cs +++ b/code/Infrastructure/Configuration/JsonConfigurationProvider.cs @@ -37,7 +37,7 @@ public class JsonConfigurationProvider _serializerOptions = new JsonSerializerOptions { WriteIndented = true, - PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower, PropertyNameCaseInsensitive = true }; } @@ -79,10 +79,8 @@ public class JsonConfigurationProvider if (!File.Exists(fullPath)) { - _logger.LogDebug("Configuration file does not exist: {file} | creating it now", fullPath); - T defaultConfig = new(); - await WriteConfigurationAsync(fileName, defaultConfig); - return defaultConfig; + _logger.LogWarning("Configuration file does not exist: {file}", fullPath); + return null; } var json = await File.ReadAllTextAsync(fullPath); @@ -90,17 +88,17 @@ public class JsonConfigurationProvider if (string.IsNullOrWhiteSpace(json)) { _logger.LogWarning("Configuration file is empty: {file}", fullPath); - return new T(); + return null; } var config = JsonSerializer.Deserialize(json, _serializerOptions); _logger.LogDebug("Read configuration from {file}", fullPath); - return config ?? new T(); + return config; } catch (Exception ex) { _logger.LogError(ex, "Error reading configuration from {file}", fullPath); - return new T(); + return null; } finally { @@ -123,7 +121,7 @@ public class JsonConfigurationProvider if (!File.Exists(fullPath)) { _logger.LogWarning("Configuration file does not exist: {file}", fullPath); - return new T(); + return null; } var json = File.ReadAllText(fullPath); @@ -131,17 +129,17 @@ public class JsonConfigurationProvider if (string.IsNullOrWhiteSpace(json)) { _logger.LogWarning("Configuration file is empty: {file}", fullPath); - return new T(); + return null; } var config = JsonSerializer.Deserialize(json, _serializerOptions); _logger.LogDebug("Read configuration from {file}", fullPath); - return config ?? new T(); + return config; } catch (Exception ex) { _logger.LogError(ex, "Error reading configuration from {file}", fullPath); - return new T(); + return null; } finally { diff --git a/code/Infrastructure/Verticals/DownloadClient/DownloadService.cs b/code/Infrastructure/Verticals/DownloadClient/DownloadService.cs index 9eeff067..8e159f12 100644 --- a/code/Infrastructure/Verticals/DownloadClient/DownloadService.cs +++ b/code/Infrastructure/Verticals/DownloadClient/DownloadService.cs @@ -3,7 +3,6 @@ using System.Text.RegularExpressions; using Common.Configuration.ContentBlocker; using Common.Configuration.DownloadCleaner; using Common.Configuration.DownloadClient; -using Common.Configuration.QueueCleaner; using Common.CustomDataTypes; using Common.Helpers; using Domain.Enums; @@ -209,7 +208,7 @@ public abstract class DownloadService : IDownloadService { _logger.LogTrace("slow estimated time | {time} | {name}", currentTime.ToString(), downloadName); - var queueCleanerConfig = _configManager.GetQueueCleanerConfig(); + var queueCleanerConfig = await _configManager.GetQueueCleanerConfigAsync(); ushort maxStrikes = queueCleanerConfig?.SlowMaxStrikes ?? 0; bool shouldRemove = await _striker .StrikeAndCheckLimit(downloadHash, downloadName, maxStrikes, StrikeType.SlowTime);