core: fix .net core 5.0 warnings. resolves #10433 (#10485)

This commit is contained in:
Diego Heras
2020-12-12 21:38:33 +01:00
committed by GitHub
parent eaa4126da5
commit 13baa27656
11 changed files with 29 additions and 52 deletions

View File

@@ -52,7 +52,7 @@ namespace Jackett.Common.Services
throw new Exception("Could not create settings directory. " + ex.Message); throw new Exception("Could not create settings directory. " + ex.Message);
} }
if (System.Environment.OSVersion.Platform != PlatformID.Unix) if (Environment.OSVersion.Platform != PlatformID.Unix)
{ {
try try
{ {
@@ -69,9 +69,7 @@ namespace Jackett.Common.Services
{ {
try try
{ {
// Use EscapedCodeBase to avoid Uri reserved characters from causing bugs processService.StartProcessAndLog(EnvironmentUtil.JackettExecutablePath(), "--MigrateSettings", true);
// https://stackoverflow.com/questions/896572
processService.StartProcessAndLog(new Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath, "--MigrateSettings", true);
} }
catch catch
{ {
@@ -166,9 +164,7 @@ namespace Jackett.Common.Services
} }
} }
// Use EscapedCodeBase to avoid Uri reserved characters from causing bugs public string ApplicationFolder() => EnvironmentUtil.JackettInstallationPath();
// https://stackoverflow.com/questions/896572
public string ApplicationFolder() => Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath);
public string GetContentFolder() public string GetContentFolder()
{ {

View File

@@ -46,15 +46,6 @@ namespace Jackett.Common.Services
variant = new Variants().GetVariant(); 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 StartUpdateChecker() => Task.Factory.StartNew(UpdateWorkerThread);
public void CheckForUpdatesNow() public void CheckForUpdatesNow()
@@ -138,7 +129,7 @@ namespace Jackett.Common.Services
{ {
var tempDir = await DownloadRelease(latestRelease.Assets, isWindows, latestRelease.Name); var tempDir = await DownloadRelease(latestRelease.Assets, isWindows, latestRelease.Name);
// Copy updater // Copy updater
var installDir = Path.GetDirectoryName(ExePath()); var installDir = EnvironmentUtil.JackettInstallationPath();
var updaterPath = GetUpdaterPath(tempDir); var updaterPath = GetUpdaterPath(tempDir);
if (updaterPath != null) if (updaterPath != null)
StartUpdate(updaterPath, installDir, isWindows, serverConfig.RuntimeSettings.NoRestart, trayIsRunning); StartUpdate(updaterPath, installDir, isWindows, serverConfig.RuntimeSettings.NoRestart, trayIsRunning);
@@ -211,7 +202,7 @@ namespace Jackett.Common.Services
public void CheckUpdaterLock() public void CheckUpdaterLock()
{ {
// check .lock file to detect errors in the update process // 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)) if (File.Exists(lockFilePath))
{ {
logger.Error("An error occurred during the last update. If this error occurs again, you need to reinstall " + 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()) if (isWindows && windowsService.ServiceExists() && windowsService.ServiceRunning())
appType = "WindowsService"; appType = "WindowsService";
var exe = Path.GetFileName(ExePath());
var args = string.Join(" ", Environment.GetCommandLineArgs().Skip(1).Select(a => a.Contains(" ") ? "\"" + a + "\"" : a)).Replace("\"", "\\\""); var args = string.Join(" ", Environment.GetCommandLineArgs().Skip(1).Select(a => a.Contains(" ") ? "\"" + a + "\"" : a)).Replace("\"", "\\\"");
var startInfo = new ProcessStartInfo var startInfo = new ProcessStartInfo
@@ -326,7 +316,7 @@ namespace Jackett.Common.Services
if (variant == Variants.JackettVariant.Mono) if (variant == Variants.JackettVariant.Mono)
{ {
// Wrap mono // Wrap mono
args = exe + " " + args; args = Path.GetFileName(EnvironmentUtil.JackettExecutablePath()) + " " + args;
startInfo.Arguments = $"{Path.Combine(updaterExePath)} --Path \"{installLocation}\" --Type \"{appType}\" --Args \" {args}\""; startInfo.Arguments = $"{Path.Combine(updaterExePath)} --Path \"{installLocation}\" --Type \"{appType}\" --Args \" {args}\"";
startInfo.FileName = "mono"; startInfo.FileName = "mono";

View File

@@ -5,6 +5,7 @@ using System.Linq;
using System.Reflection; using System.Reflection;
using System.ServiceProcess; using System.ServiceProcess;
using Jackett.Common.Services.Interfaces; using Jackett.Common.Services.Interfaces;
using Jackett.Common.Utils;
using NLog; using NLog;
namespace Jackett.Common.Services namespace Jackett.Common.Services
@@ -51,10 +52,7 @@ namespace Jackett.Common.Services
} }
else else
{ {
// Use EscapedCodeBase to avoid Uri reserved characters from causing bugs var applicationFolder = EnvironmentUtil.JackettInstallationPath();
// https://stackoverflow.com/questions/896572
var applicationFolder = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath);
var exePath = Path.Combine(applicationFolder, SERVICEEXE); var exePath = Path.Combine(applicationFolder, SERVICEEXE);
if (!File.Exists(exePath) && Debugger.IsAttached) if (!File.Exists(exePath) && Debugger.IsAttached)
{ {

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
using System.Reflection; using System.Reflection;
namespace Jackett.Common.Utils namespace Jackett.Common.Utils
@@ -14,6 +15,16 @@ namespace Jackett.Common.Utils
return $"v{fvi.ProductVersion}"; 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; public static bool IsWindows => Environment.OSVersion.Platform == PlatformID.Win32NT;
} }

View File

@@ -163,7 +163,7 @@ namespace Jackett.Server.Controllers
{ {
try try
{ {
var consoleExePath = System.Reflection.Assembly.GetExecutingAssembly().CodeBase.Replace(".dll", ".exe"); var consoleExePath = EnvironmentUtil.JackettExecutablePath().Replace(".dll", ".exe");
processService.StartProcessAndLog(consoleExePath, "--ReserveUrls", true); processService.StartProcessAndLog(consoleExePath, "--ReserveUrls", true);
} }
catch catch

View File

@@ -119,7 +119,7 @@ namespace Jackett.Server
try try
{ {
logger.Debug("Creating web host..."); logger.Debug("Creating web host...");
var applicationFolder = Path.Combine(configurationService.ApplicationFolder(), "Content"); var applicationFolder = configurationService.GetContentFolder();
logger.Debug($"Content root path is: {applicationFolder}"); logger.Debug($"Content root path is: {applicationFolder}");
CreateWebHostBuilder(args, url, applicationFolder).Build().Run(); CreateWebHostBuilder(args, url, applicationFolder).Build().Run();

View File

@@ -6,6 +6,7 @@ using System.Reflection;
using System.ServiceProcess; using System.ServiceProcess;
using Jackett.Common.Services; using Jackett.Common.Services;
using Jackett.Common.Services.Interfaces; using Jackett.Common.Services.Interfaces;
using Jackett.Common.Utils;
using NLog; using NLog;
namespace Jackett.Server.Services namespace Jackett.Server.Services
@@ -49,10 +50,7 @@ namespace Jackett.Server.Services
} }
else else
{ {
// Use EscapedCodeBase to avoid Uri reserved characters from causing bugs var applicationFolder = EnvironmentUtil.JackettInstallationPath();
// https://stackoverflow.com/questions/896572
var applicationFolder = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath);
var exePath = Path.Combine(applicationFolder, SERVICEEXE); var exePath = Path.Combine(applicationFolder, SERVICEEXE);
if (!File.Exists(exePath) && Debugger.IsAttached) if (!File.Exists(exePath) && Debugger.IsAttached)
{ {

View File

@@ -51,11 +51,7 @@ namespace Jackett.Service
private void StartConsoleApplication() private void StartConsoleApplication()
{ {
// Use EscapedCodeBase to avoid Uri reserved characters from causing bugs var exePath = Path.Combine(EnvironmentUtil.JackettInstallationPath(), "JackettConsole.exe");
// https://stackoverflow.com/questions/896572
var applicationFolder = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath);
var exePath = Path.Combine(applicationFolder, "JackettConsole.exe");
var startInfo = new ProcessStartInfo() var startInfo = new ProcessStartInfo()
{ {

View File

@@ -15,14 +15,13 @@ namespace Jackett.Test.Common.Definitions
[Test] [Test]
public void LoadAndParseAllCardigannDefinitions() public void LoadAndParseAllCardigannDefinitions()
{ {
// Use EscapedCodeBase to avoid Uri reserved characters from causing bugs var applicationFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? "";
// https://stackoverflow.com/questions/896572 var definitionsFolder = Path.Combine(applicationFolder, "Definitions");
var applicationFolder = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath);
var definitionsFolder = Path.GetFullPath(Path.Combine(applicationFolder, "Definitions"));
var deserializer = new DeserializerBuilder() var deserializer = new DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance) .WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build(); .Build();
var files = new DirectoryInfo(definitionsFolder).GetFiles("*.yml"); var files = new DirectoryInfo(definitionsFolder).GetFiles("*.yml");
Assert.True(files.Length > 0);
foreach (var file in files) foreach (var file in files)
try try
{ {

View File

@@ -261,9 +261,7 @@ namespace Jackett.Tray
private void StartConsoleApplication() private void StartConsoleApplication()
{ {
var applicationFolder = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath); var exePath = Path.Combine(EnvironmentUtil.JackettInstallationPath(), "JackettConsole.exe");
var exePath = Path.Combine(applicationFolder, "JackettConsole.exe");
var startInfo = new ProcessStartInfo() var startInfo = new ProcessStartInfo()
{ {

View File

@@ -128,7 +128,7 @@ namespace Jackett.Updater
private void ProcessUpdate(UpdaterConsoleOptions options) private void ProcessUpdate(UpdaterConsoleOptions options)
{ {
var updateLocation = GetUpdateLocation(); var updateLocation = EnvironmentUtil.JackettInstallationPath();
if (!(updateLocation.EndsWith("\\") || updateLocation.EndsWith("/"))) if (!(updateLocation.EndsWith("\\") || updateLocation.EndsWith("/")))
updateLocation += Path.DirectorySeparatorChar; updateLocation += Path.DirectorySeparatorChar;
@@ -608,15 +608,6 @@ namespace Jackett.Updater
return success; 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) private string GetJackettConsolePath(string directoryPath)
{ {
var variants = new Variants(); var variants = new Variants();