From cf3848a54ffee63a9b340332541d4c0e515818c7 Mon Sep 17 00:00:00 2001 From: flightlevel Date: Sun, 17 Jun 2018 11:48:59 +1000 Subject: [PATCH] .NET Core preparation: Access ServerConfig without using JackettModule --- src/Jackett.Common/Plumbing/JackettModule.cs | 53 ++---------------- .../Services/ConfigurationService.cs | 54 ++++++++++++++++++- .../Interfaces/IConfigurationService.cs | 4 +- .../Services/SerializeService.cs | 2 +- src/Jackett/Services/ServerService.cs | 2 + 5 files changed, 61 insertions(+), 54 deletions(-) diff --git a/src/Jackett.Common/Plumbing/JackettModule.cs b/src/Jackett.Common/Plumbing/JackettModule.cs index f795a86f3..bebd1713c 100644 --- a/src/Jackett.Common/Plumbing/JackettModule.cs +++ b/src/Jackett.Common/Plumbing/JackettModule.cs @@ -94,61 +94,14 @@ namespace Jackett.Common.Plumbing private ServerConfig BuildServerConfig(IComponentContext ctx) { var configService = ctx.Resolve(); - // Load config - var config = configService.GetConfig(); - if (config == null) - { - config = new ServerConfig(_runtimeSettings); - } - else - { - //We don't load these out of the config files as it could get confusing to users who accidently save. - //In future we could flatten the serverconfig, and use command line parameters to override any configuration. - config.RuntimeSettings = _runtimeSettings; - } - - if (string.IsNullOrWhiteSpace(config.APIKey)) - { - // Check for legacy key config - var apiKeyFile = Path.Combine(configService.GetAppDataFolder(), "api_key.txt"); - if (File.Exists(apiKeyFile)) - { - config.APIKey = File.ReadAllText(apiKeyFile); - } - - // Check for legacy settings - - var path = Path.Combine(configService.GetAppDataFolder(), "config.json"); ; - var jsonReply = new JObject(); - if (File.Exists(path)) - { - jsonReply = JObject.Parse(File.ReadAllText(path)); - config.Port = (int)jsonReply["port"]; - config.AllowExternal = (bool)jsonReply["public"]; - } - - if (string.IsNullOrWhiteSpace(config.APIKey)) - config.APIKey = StringUtil.GenerateRandom(32); - - configService.SaveConfig(config); - } - - if (string.IsNullOrWhiteSpace(config.InstanceId)) - { - config.InstanceId = StringUtil.GenerateRandom(64); - configService.SaveConfig(config); - } - config.ConfigChanged(); - return config; + return configService.BuildServerConfig(_runtimeSettings); } - - private static bool DetectMonoCompatabilityWithHttpClient() { bool usehttpclient = false; - try - { + try + { Type monotype = Type.GetType("Mono.Runtime"); if (monotype != null) { diff --git a/src/Jackett.Common/Services/ConfigurationService.cs b/src/Jackett.Common/Services/ConfigurationService.cs index b0006a167..e2558ba74 100644 --- a/src/Jackett.Common/Services/ConfigurationService.cs +++ b/src/Jackett.Common/Services/ConfigurationService.cs @@ -7,6 +7,7 @@ using System.Security.Principal; using Jackett.Common.Models.Config; using Jackett.Common.Services.Interfaces; using Jackett.Common.Utils; +using Newtonsoft.Json.Linq; using NLog; namespace Jackett.Common.Services @@ -48,8 +49,6 @@ namespace Jackett.Common.Services dir.SetAccessControl(directorySecurity); } } - - logger.Info("App config/log directory: " + GetAppDataFolder()); } catch (Exception ex) { @@ -240,5 +239,56 @@ namespace Jackett.Common.Services { return EnvironmentUtil.JackettVersion; } + + public ServerConfig BuildServerConfig(RuntimeSettings runtimeSettings) + { + // Load config + var config = GetConfig(); + if (config == null) + { + config = new ServerConfig(runtimeSettings); + } + else + { + //We don't load these out of the config files as it could get confusing to users who accidently save. + //In future we could flatten the serverconfig, and use command line parameters to override any configuration. + config.RuntimeSettings = runtimeSettings; + } + + if (string.IsNullOrWhiteSpace(config.APIKey)) + { + // Check for legacy key config + var apiKeyFile = Path.Combine(GetAppDataFolder(), "api_key.txt"); + if (File.Exists(apiKeyFile)) + { + config.APIKey = File.ReadAllText(apiKeyFile); + } + + // Check for legacy settings + + var path = Path.Combine(GetAppDataFolder(), "config.json"); ; + var jsonReply = new JObject(); + if (File.Exists(path)) + { + jsonReply = JObject.Parse(File.ReadAllText(path)); + config.Port = (int)jsonReply["port"]; + config.AllowExternal = (bool)jsonReply["public"]; + } + + if (string.IsNullOrWhiteSpace(config.APIKey)) + config.APIKey = StringUtil.GenerateRandom(32); + + SaveConfig(config); + } + + if (string.IsNullOrWhiteSpace(config.InstanceId)) + { + config.InstanceId = StringUtil.GenerateRandom(64); + SaveConfig(config); + } + + config.ConfigChanged(); + return config; + } } } diff --git a/src/Jackett.Common/Services/Interfaces/IConfigurationService.cs b/src/Jackett.Common/Services/Interfaces/IConfigurationService.cs index 7a6db3085..916d95a88 100644 --- a/src/Jackett.Common/Services/Interfaces/IConfigurationService.cs +++ b/src/Jackett.Common/Services/Interfaces/IConfigurationService.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using Jackett.Common.Models.Config; +using System.Collections.Generic; namespace Jackett.Common.Services.Interfaces { @@ -15,5 +16,6 @@ namespace Jackett.Common.Services.Interfaces List GetCardigannDefinitionsFolders(); void CreateOrMigrateSettings(); void PerformMigration(); + ServerConfig BuildServerConfig(RuntimeSettings runtimeSettings); } } diff --git a/src/Jackett.Common/Services/SerializeService.cs b/src/Jackett.Common/Services/SerializeService.cs index 7c6574abc..5f6aea7f7 100644 --- a/src/Jackett.Common/Services/SerializeService.cs +++ b/src/Jackett.Common/Services/SerializeService.cs @@ -4,7 +4,7 @@ using Newtonsoft.Json; namespace Jackett.Common.Services { - class SerializeService : ISerializeService + public class SerializeService : ISerializeService { public string Serialise(object obj) { diff --git a/src/Jackett/Services/ServerService.cs b/src/Jackett/Services/ServerService.cs index 4c567ca79..3de1069e0 100644 --- a/src/Jackett/Services/ServerService.cs +++ b/src/Jackett/Services/ServerService.cs @@ -111,6 +111,8 @@ namespace Jackett.Services logger.Error("Error while getting MaxThreads details: " + e); } + logger.Info("App config/log directory: " + configService.GetAppDataFolder()); + try { var issuefile = "/etc/issue";