From 13baa27656407c88a474db0748e86eb79a0027bc Mon Sep 17 00:00:00 2001 From: Diego Heras Date: Sat, 12 Dec 2020 21:38:33 +0100 Subject: [PATCH] core: fix .net core 5.0 warnings. resolves #10433 (#10485) --- .../Services/ConfigurationService.cs | 10 +++------- src/Jackett.Common/Services/UpdateService.cs | 16 +++------------- .../Services/WindowsServiceConfigService.cs | 6 ++---- src/Jackett.Common/Utils/EnvironmentUtil.cs | 11 +++++++++++ .../Controllers/ServerConfigurationController.cs | 2 +- src/Jackett.Server/Program.cs | 2 +- .../Services/ServiceConfigService.cs | 6 ++---- src/Jackett.Service/Service.cs | 6 +----- .../Common/Definitions/DefinitionsParserTests.cs | 7 +++---- src/Jackett.Tray/Main.cs | 4 +--- src/Jackett.Updater/Program.cs | 11 +---------- 11 files changed, 29 insertions(+), 52 deletions(-) diff --git a/src/Jackett.Common/Services/ConfigurationService.cs b/src/Jackett.Common/Services/ConfigurationService.cs index f1233a1c1..6b89ad527 100644 --- a/src/Jackett.Common/Services/ConfigurationService.cs +++ b/src/Jackett.Common/Services/ConfigurationService.cs @@ -52,7 +52,7 @@ namespace Jackett.Common.Services throw new Exception("Could not create settings directory. " + ex.Message); } - if (System.Environment.OSVersion.Platform != PlatformID.Unix) + if (Environment.OSVersion.Platform != PlatformID.Unix) { try { @@ -69,9 +69,7 @@ namespace Jackett.Common.Services { try { - // Use EscapedCodeBase to avoid Uri reserved characters from causing bugs - // https://stackoverflow.com/questions/896572 - processService.StartProcessAndLog(new Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath, "--MigrateSettings", true); + processService.StartProcessAndLog(EnvironmentUtil.JackettExecutablePath(), "--MigrateSettings", true); } catch { @@ -166,9 +164,7 @@ namespace Jackett.Common.Services } } - // Use EscapedCodeBase to avoid Uri reserved characters from causing bugs - // https://stackoverflow.com/questions/896572 - public string ApplicationFolder() => Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath); + public string ApplicationFolder() => EnvironmentUtil.JackettInstallationPath(); public string GetContentFolder() { diff --git a/src/Jackett.Common/Services/UpdateService.cs b/src/Jackett.Common/Services/UpdateService.cs index 1a01af033..7d1168dff 100644 --- a/src/Jackett.Common/Services/UpdateService.cs +++ b/src/Jackett.Common/Services/UpdateService.cs @@ -46,15 +46,6 @@ namespace Jackett.Common.Services variant = new Variants().GetVariant(); } - private string ExePath() - { - // Use EscapedCodeBase to avoid Uri reserved characters from causing bugs - // https://stackoverflow.com/questions/896572 - var location = new Uri(Assembly.GetEntryAssembly().GetName().EscapedCodeBase); - // Use LocalPath instead of AbsolutePath to avoid needing to unescape Uri format. - return new FileInfo(location.LocalPath).FullName; - } - public void StartUpdateChecker() => Task.Factory.StartNew(UpdateWorkerThread); public void CheckForUpdatesNow() @@ -138,7 +129,7 @@ namespace Jackett.Common.Services { var tempDir = await DownloadRelease(latestRelease.Assets, isWindows, latestRelease.Name); // Copy updater - var installDir = Path.GetDirectoryName(ExePath()); + var installDir = EnvironmentUtil.JackettInstallationPath(); var updaterPath = GetUpdaterPath(tempDir); if (updaterPath != null) StartUpdate(updaterPath, installDir, isWindows, serverConfig.RuntimeSettings.NoRestart, trayIsRunning); @@ -211,7 +202,7 @@ namespace Jackett.Common.Services public void CheckUpdaterLock() { // check .lock file to detect errors in the update process - var lockFilePath = Path.Combine(Path.GetDirectoryName(ExePath()), ".lock"); + var lockFilePath = Path.Combine(EnvironmentUtil.JackettInstallationPath(), ".lock"); if (File.Exists(lockFilePath)) { logger.Error("An error occurred during the last update. If this error occurs again, you need to reinstall " + @@ -313,7 +304,6 @@ namespace Jackett.Common.Services if (isWindows && windowsService.ServiceExists() && windowsService.ServiceRunning()) appType = "WindowsService"; - var exe = Path.GetFileName(ExePath()); var args = string.Join(" ", Environment.GetCommandLineArgs().Skip(1).Select(a => a.Contains(" ") ? "\"" + a + "\"" : a)).Replace("\"", "\\\""); var startInfo = new ProcessStartInfo @@ -326,7 +316,7 @@ namespace Jackett.Common.Services if (variant == Variants.JackettVariant.Mono) { // Wrap mono - args = exe + " " + args; + args = Path.GetFileName(EnvironmentUtil.JackettExecutablePath()) + " " + args; startInfo.Arguments = $"{Path.Combine(updaterExePath)} --Path \"{installLocation}\" --Type \"{appType}\" --Args \" {args}\""; startInfo.FileName = "mono"; diff --git a/src/Jackett.Common/Services/WindowsServiceConfigService.cs b/src/Jackett.Common/Services/WindowsServiceConfigService.cs index 40bf44578..ea73d6c99 100644 --- a/src/Jackett.Common/Services/WindowsServiceConfigService.cs +++ b/src/Jackett.Common/Services/WindowsServiceConfigService.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Reflection; using System.ServiceProcess; using Jackett.Common.Services.Interfaces; +using Jackett.Common.Utils; using NLog; namespace Jackett.Common.Services @@ -51,10 +52,7 @@ namespace Jackett.Common.Services } else { - // Use EscapedCodeBase to avoid Uri reserved characters from causing bugs - // https://stackoverflow.com/questions/896572 - var applicationFolder = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath); - + var applicationFolder = EnvironmentUtil.JackettInstallationPath(); var exePath = Path.Combine(applicationFolder, SERVICEEXE); if (!File.Exists(exePath) && Debugger.IsAttached) { diff --git a/src/Jackett.Common/Utils/EnvironmentUtil.cs b/src/Jackett.Common/Utils/EnvironmentUtil.cs index 7a3b47a59..3f8f6f3de 100644 --- a/src/Jackett.Common/Utils/EnvironmentUtil.cs +++ b/src/Jackett.Common/Utils/EnvironmentUtil.cs @@ -1,5 +1,6 @@ using System; using System.Diagnostics; +using System.IO; using System.Reflection; namespace Jackett.Common.Utils @@ -14,6 +15,16 @@ namespace Jackett.Common.Utils return $"v{fvi.ProductVersion}"; } + public static string JackettInstallationPath() + { + return Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location); + } + + public static string JackettExecutablePath() + { + return Assembly.GetEntryAssembly()?.Location; + } + public static bool IsWindows => Environment.OSVersion.Platform == PlatformID.Win32NT; } diff --git a/src/Jackett.Server/Controllers/ServerConfigurationController.cs b/src/Jackett.Server/Controllers/ServerConfigurationController.cs index 7aa7d13a8..136ab0ef8 100644 --- a/src/Jackett.Server/Controllers/ServerConfigurationController.cs +++ b/src/Jackett.Server/Controllers/ServerConfigurationController.cs @@ -163,7 +163,7 @@ namespace Jackett.Server.Controllers { try { - var consoleExePath = System.Reflection.Assembly.GetExecutingAssembly().CodeBase.Replace(".dll", ".exe"); + var consoleExePath = EnvironmentUtil.JackettExecutablePath().Replace(".dll", ".exe"); processService.StartProcessAndLog(consoleExePath, "--ReserveUrls", true); } catch diff --git a/src/Jackett.Server/Program.cs b/src/Jackett.Server/Program.cs index 169343c50..e9ec0d23d 100644 --- a/src/Jackett.Server/Program.cs +++ b/src/Jackett.Server/Program.cs @@ -119,7 +119,7 @@ namespace Jackett.Server try { logger.Debug("Creating web host..."); - var applicationFolder = Path.Combine(configurationService.ApplicationFolder(), "Content"); + var applicationFolder = configurationService.GetContentFolder(); logger.Debug($"Content root path is: {applicationFolder}"); CreateWebHostBuilder(args, url, applicationFolder).Build().Run(); diff --git a/src/Jackett.Server/Services/ServiceConfigService.cs b/src/Jackett.Server/Services/ServiceConfigService.cs index af72cb513..1e9dcdc69 100644 --- a/src/Jackett.Server/Services/ServiceConfigService.cs +++ b/src/Jackett.Server/Services/ServiceConfigService.cs @@ -6,6 +6,7 @@ using System.Reflection; using System.ServiceProcess; using Jackett.Common.Services; using Jackett.Common.Services.Interfaces; +using Jackett.Common.Utils; using NLog; namespace Jackett.Server.Services @@ -49,10 +50,7 @@ namespace Jackett.Server.Services } else { - // Use EscapedCodeBase to avoid Uri reserved characters from causing bugs - // https://stackoverflow.com/questions/896572 - var applicationFolder = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath); - + var applicationFolder = EnvironmentUtil.JackettInstallationPath(); var exePath = Path.Combine(applicationFolder, SERVICEEXE); if (!File.Exists(exePath) && Debugger.IsAttached) { diff --git a/src/Jackett.Service/Service.cs b/src/Jackett.Service/Service.cs index 9cbf47407..208aa634c 100644 --- a/src/Jackett.Service/Service.cs +++ b/src/Jackett.Service/Service.cs @@ -51,11 +51,7 @@ namespace Jackett.Service private void StartConsoleApplication() { - // Use EscapedCodeBase to avoid Uri reserved characters from causing bugs - // https://stackoverflow.com/questions/896572 - var applicationFolder = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath); - - var exePath = Path.Combine(applicationFolder, "JackettConsole.exe"); + var exePath = Path.Combine(EnvironmentUtil.JackettInstallationPath(), "JackettConsole.exe"); var startInfo = new ProcessStartInfo() { diff --git a/src/Jackett.Test/Common/Definitions/DefinitionsParserTests.cs b/src/Jackett.Test/Common/Definitions/DefinitionsParserTests.cs index fe48db3b7..a726ceba2 100644 --- a/src/Jackett.Test/Common/Definitions/DefinitionsParserTests.cs +++ b/src/Jackett.Test/Common/Definitions/DefinitionsParserTests.cs @@ -15,14 +15,13 @@ namespace Jackett.Test.Common.Definitions [Test] public void LoadAndParseAllCardigannDefinitions() { - // Use EscapedCodeBase to avoid Uri reserved characters from causing bugs - // https://stackoverflow.com/questions/896572 - var applicationFolder = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath); - var definitionsFolder = Path.GetFullPath(Path.Combine(applicationFolder, "Definitions")); + var applicationFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? ""; + var definitionsFolder = Path.Combine(applicationFolder, "Definitions"); var deserializer = new DeserializerBuilder() .WithNamingConvention(CamelCaseNamingConvention.Instance) .Build(); var files = new DirectoryInfo(definitionsFolder).GetFiles("*.yml"); + Assert.True(files.Length > 0); foreach (var file in files) try { diff --git a/src/Jackett.Tray/Main.cs b/src/Jackett.Tray/Main.cs index 0454cfb53..78cc8fa16 100644 --- a/src/Jackett.Tray/Main.cs +++ b/src/Jackett.Tray/Main.cs @@ -261,9 +261,7 @@ namespace Jackett.Tray private void StartConsoleApplication() { - var applicationFolder = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath); - - var exePath = Path.Combine(applicationFolder, "JackettConsole.exe"); + var exePath = Path.Combine(EnvironmentUtil.JackettInstallationPath(), "JackettConsole.exe"); var startInfo = new ProcessStartInfo() { diff --git a/src/Jackett.Updater/Program.cs b/src/Jackett.Updater/Program.cs index a1e2b1b83..6e35478b6 100644 --- a/src/Jackett.Updater/Program.cs +++ b/src/Jackett.Updater/Program.cs @@ -128,7 +128,7 @@ namespace Jackett.Updater private void ProcessUpdate(UpdaterConsoleOptions options) { - var updateLocation = GetUpdateLocation(); + var updateLocation = EnvironmentUtil.JackettInstallationPath(); if (!(updateLocation.EndsWith("\\") || updateLocation.EndsWith("/"))) updateLocation += Path.DirectorySeparatorChar; @@ -608,15 +608,6 @@ namespace Jackett.Updater return success; } - private string GetUpdateLocation() - { - // Use EscapedCodeBase to avoid Uri reserved characters from causing bugs - // https://stackoverflow.com/questions/896572 - var location = new Uri(Assembly.GetEntryAssembly().GetName().EscapedCodeBase); - // Use LocalPath instead of AbsolutePath to avoid needing to unescape Uri format. - return new FileInfo(location.LocalPath).DirectoryName; - } - private string GetJackettConsolePath(string directoryPath) { var variants = new Variants();