Implemented the ability to configure the default port for non-docker users. Docker users will always be 5000. (#280)

This commit is contained in:
Joseph Milazzo 2021-06-07 14:36:35 -05:00 committed by GitHub
parent 4606b54603
commit 5259a1484a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 63 additions and 7 deletions

View file

@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Text.Json;
using Kavita.Common.EnvironmentInfo;
namespace Kavita.Common
{
@ -28,6 +29,7 @@ namespace Kavita.Common
return false;
}
public static bool UpdateJwtToken(string filePath, string token)
{
@ -42,5 +44,50 @@ namespace Kavita.Common
return false;
}
}
public static bool UpdatePort(string filePath, int port)
{
if (new OsInfo(Array.Empty<IOsVersionAdapter>()).IsDocker)
{
return true;
}
try
{
var currentPort = GetPort(filePath);
var json = File.ReadAllText(filePath).Replace("\"Port\": " + currentPort, "\"Port\": " + port);
File.WriteAllText(filePath, json);
return true;
}
catch (Exception)
{
return false;
}
}
public static int GetPort(string filePath)
{
const int defaultPort = 5000;
if (new OsInfo(Array.Empty<IOsVersionAdapter>()).IsDocker)
{
return defaultPort;
}
try {
var json = File.ReadAllText(filePath);
var jsonObj = JsonSerializer.Deserialize<dynamic>(json);
const string key = "Port";
if (jsonObj.TryGetProperty(key, out JsonElement tokenElement))
{
return tokenElement.GetInt32();
}
}
catch (Exception ex) {
Console.WriteLine("Error writing app settings: " + ex.Message);
}
return defaultPort;
}
}
}