Skip to content

Commit

Permalink
core: fix .net core 5.0 warnings. resolves #10433 (#10485)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngosang committed Dec 12, 2020
1 parent eaa4126 commit 13baa27
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 52 deletions.
10 changes: 3 additions & 7 deletions src/Jackett.Common/Services/ConfigurationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void CreateOrMigrateSettings()
throw new Exception("Could not create settings directory. " + ex.Message);
}

if (System.Environment.OSVersion.Platform != PlatformID.Unix)
if (Environment.OSVersion.Platform != PlatformID.Unix)
{
try
{
Expand All @@ -69,9 +69,7 @@ public void CreateOrMigrateSettings()
{
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
{
Expand Down Expand Up @@ -166,9 +164,7 @@ public void SaveConfig<T>(T config)
}
}

// 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()
{
Expand Down
16 changes: 3 additions & 13 deletions src/Jackett.Common/Services/UpdateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,6 @@ public UpdateService(Logger l, WebClient c, ITrayLockService ls, IServiceConfigS
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()
Expand Down Expand Up @@ -138,7 +129,7 @@ private async Task CheckForUpdates()
{
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);
Expand Down Expand Up @@ -211,7 +202,7 @@ public void CleanupTempDir()
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 " +
Expand Down Expand Up @@ -313,7 +304,6 @@ private void StartUpdate(string updaterExePath, string installLocation, bool isW
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
Expand All @@ -326,7 +316,7 @@ private void StartUpdate(string updaterExePath, string installLocation, bool isW
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";
Expand Down
6 changes: 2 additions & 4 deletions src/Jackett.Common/Services/WindowsServiceConfigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Reflection;
using System.ServiceProcess;
using Jackett.Common.Services.Interfaces;
using Jackett.Common.Utils;
using NLog;

namespace Jackett.Common.Services
Expand Down Expand Up @@ -51,10 +52,7 @@ public void Install()
}
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)
{
Expand Down
11 changes: 11 additions & 0 deletions src/Jackett.Common/Utils/EnvironmentUtil.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;

namespace Jackett.Common.Utils
Expand All @@ -14,6 +15,16 @@ public static string JackettVersion()
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;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public IActionResult UpdateConfig([FromBody]Common.Models.DTO.ServerConfig confi
{
try
{
var consoleExePath = System.Reflection.Assembly.GetExecutingAssembly().CodeBase.Replace(".dll", ".exe");
var consoleExePath = EnvironmentUtil.JackettExecutablePath().Replace(".dll", ".exe");
processService.StartProcessAndLog(consoleExePath, "--ReserveUrls", true);
}
catch
Expand Down
2 changes: 1 addition & 1 deletion src/Jackett.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public static void Main(string[] args)
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();
Expand Down
6 changes: 2 additions & 4 deletions src/Jackett.Server/Services/ServiceConfigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.ServiceProcess;
using Jackett.Common.Services;
using Jackett.Common.Services.Interfaces;
using Jackett.Common.Utils;
using NLog;

namespace Jackett.Server.Services
Expand Down Expand Up @@ -49,10 +50,7 @@ public void Install()
}
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)
{
Expand Down
6 changes: 1 addition & 5 deletions src/Jackett.Service/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ protected override void OnStop()

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()
{
Expand Down
7 changes: 3 additions & 4 deletions src/Jackett.Test/Common/Definitions/DefinitionsParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ public class DefinitionsParserTests
[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
{
Expand Down
4 changes: 1 addition & 3 deletions src/Jackett.Tray/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,7 @@ private void CloseTrayApplication()

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()
{
Expand Down
11 changes: 1 addition & 10 deletions src/Jackett.Updater/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private void KillPids(int[] pids)

private void ProcessUpdate(UpdaterConsoleOptions options)
{
var updateLocation = GetUpdateLocation();
var updateLocation = EnvironmentUtil.JackettInstallationPath();
if (!(updateLocation.EndsWith("\\") || updateLocation.EndsWith("/")))
updateLocation += Path.DirectorySeparatorChar;

Expand Down Expand Up @@ -608,15 +608,6 @@ private bool CopyUpdateFile(string jackettDestinationDirectory, string fullSourc
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();
Expand Down

0 comments on commit 13baa27

Please sign in to comment.