Compare commits
No commits in common. "72ae12441864a7feb6cdbe7ee1c0fc72fdd2eb1e" and "db53e2156bde2a12f1d28503cccd25118b6e446f" have entirely different histories.
72ae124418
...
db53e2156b
@ -46,29 +46,8 @@ internal sealed class TrangaCli : Command<TrangaCli.Settings>
|
||||
|
||||
string? logFilePath = settings.fileLoggerPath ?? "";
|
||||
Logger logger = new(enabledLoggers.ToArray(), Console.Out, Console.OutputEncoding, logFilePath);
|
||||
|
||||
TrangaSettings? trangaSettings = null;
|
||||
|
||||
if (settings.downloadLocation is not null && settings.workingDirectory is not null)
|
||||
{
|
||||
trangaSettings = new TrangaSettings(settings.downloadLocation, settings.workingDirectory);
|
||||
}else if (settings.downloadLocation is not null)
|
||||
{
|
||||
if (trangaSettings is null)
|
||||
trangaSettings = new TrangaSettings(downloadLocation: settings.downloadLocation);
|
||||
else
|
||||
trangaSettings = new TrangaSettings(downloadLocation: settings.downloadLocation, settings.workingDirectory);
|
||||
}else if (settings.workingDirectory is not null)
|
||||
{
|
||||
if (trangaSettings is null)
|
||||
trangaSettings = new TrangaSettings(downloadLocation: settings.workingDirectory);
|
||||
else
|
||||
trangaSettings = new TrangaSettings(settings.downloadLocation, settings.workingDirectory);
|
||||
}
|
||||
else
|
||||
{
|
||||
trangaSettings = new TrangaSettings();
|
||||
}
|
||||
TrangaSettings trangaSettings = new (settings.downloadLocation, settings.workingDirectory, settings.apiPort);
|
||||
|
||||
Directory.CreateDirectory(trangaSettings.downloadLocation);
|
||||
Directory.CreateDirectory(trangaSettings.workingDirectory);
|
||||
|
@ -9,8 +9,7 @@ namespace Tranga;
|
||||
|
||||
public abstract class GlobalBase
|
||||
{
|
||||
[JsonIgnore]
|
||||
public Logger? logger { get; init; }
|
||||
protected Logger? logger { get; init; }
|
||||
protected TrangaSettings settings { get; init; }
|
||||
protected HashSet<NotificationConnector> notificationConnectors { get; init; }
|
||||
protected HashSet<LibraryConnector> libraryConnectors { get; init; }
|
||||
@ -89,7 +88,7 @@ public abstract class GlobalBase
|
||||
while(IsFileInUse(settings.libraryConnectorsFilePath))
|
||||
Thread.Sleep(100);
|
||||
Log("Exporting libraryConnectors");
|
||||
File.WriteAllText(settings.libraryConnectorsFilePath, JsonConvert.SerializeObject(libraryConnectors, Formatting.Indented));
|
||||
File.WriteAllText(settings.libraryConnectorsFilePath, JsonConvert.SerializeObject(libraryConnectors));
|
||||
}
|
||||
|
||||
protected void DeleteLibraryConnector(LibraryConnector.LibraryType libraryType)
|
||||
@ -99,12 +98,10 @@ public abstract class GlobalBase
|
||||
while(IsFileInUse(settings.libraryConnectorsFilePath))
|
||||
Thread.Sleep(100);
|
||||
Log("Exporting libraryConnectors");
|
||||
File.WriteAllText(settings.libraryConnectorsFilePath, JsonConvert.SerializeObject(libraryConnectors, Formatting.Indented));
|
||||
File.WriteAllText(settings.libraryConnectorsFilePath, JsonConvert.SerializeObject(libraryConnectors));
|
||||
}
|
||||
|
||||
protected bool IsFileInUse(string filePath) => IsFileInUse(filePath, this.logger);
|
||||
|
||||
public static bool IsFileInUse(string filePath, Logger? logger)
|
||||
protected bool IsFileInUse(string filePath)
|
||||
{
|
||||
if (!File.Exists(filePath))
|
||||
return false;
|
||||
@ -116,7 +113,7 @@ public abstract class GlobalBase
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
logger?.WriteLine($"File is in use {filePath}");
|
||||
Log($"File is in use {filePath}");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,4 @@
|
||||
using System.Text.Json.Nodes;
|
||||
using Logging;
|
||||
using Newtonsoft.Json;
|
||||
using JsonSerializer = System.Text.Json.JsonSerializer;
|
||||
|
||||
@ -9,7 +8,7 @@ public class Kavita : LibraryConnector
|
||||
{
|
||||
|
||||
public Kavita(GlobalBase clone, string baseUrl, string username, string password) :
|
||||
base(clone, baseUrl, GetToken(baseUrl, username, password, clone.logger), LibraryType.Kavita)
|
||||
base(clone, baseUrl, GetToken(baseUrl, username, password), LibraryType.Kavita)
|
||||
{
|
||||
}
|
||||
|
||||
@ -23,7 +22,7 @@ public class Kavita : LibraryConnector
|
||||
return $"Kavita {baseUrl}";
|
||||
}
|
||||
|
||||
private static string GetToken(string baseUrl, string username, string password, Logger? logger = null)
|
||||
private static string GetToken(string baseUrl, string username, string password)
|
||||
{
|
||||
HttpClient client = new()
|
||||
{
|
||||
@ -41,24 +40,16 @@ public class Kavita : LibraryConnector
|
||||
try
|
||||
{
|
||||
HttpResponseMessage response = client.Send(requestMessage);
|
||||
logger?.WriteLine($"Kavita | GetToken {requestMessage.RequestUri} -> {response.StatusCode}");
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
JsonObject? result = JsonSerializer.Deserialize<JsonObject>(response.Content.ReadAsStream());
|
||||
if (result is not null)
|
||||
return result["token"]!.GetValue<string>();
|
||||
}
|
||||
else
|
||||
{
|
||||
logger?.WriteLine($"Kavita | {response.Content}");
|
||||
}
|
||||
JsonObject? result = JsonSerializer.Deserialize<JsonObject>(response.Content.ReadAsStream());
|
||||
if (result is not null)
|
||||
return result["token"]!.GetValue<string>();
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
{
|
||||
logger?.WriteLine($"Kavita | Unable to retrieve token:\n\r{e}");
|
||||
Console.WriteLine($"Unable to retrieve token:\n\r{e}");
|
||||
}
|
||||
logger?.WriteLine("Kavita | Did not receive token.");
|
||||
return "";
|
||||
Console.WriteLine("Kavita: Did not receive token.");
|
||||
throw new Exception("Kavita: Did not receive token.");
|
||||
}
|
||||
|
||||
public override void UpdateLibrary()
|
||||
|
@ -23,8 +23,6 @@ public abstract class LibraryConnector : GlobalBase
|
||||
Log($"Creating libraryConnector {Enum.GetName(libraryType)}");
|
||||
if (!baseUrlRex.IsMatch(baseUrl))
|
||||
throw new ArgumentException("Base url does not match pattern");
|
||||
if(auth == "")
|
||||
throw new ArgumentNullException(nameof(auth), "Auth can not be empty");
|
||||
this.baseUrl = baseUrlRex.Match(baseUrl).Value;
|
||||
this.auth = auth;
|
||||
this.libraryType = libraryType;
|
||||
|
@ -439,7 +439,6 @@ public class Server : GlobalBase
|
||||
SendResponse(HttpStatusCode.Accepted, response);
|
||||
}else
|
||||
SendResponse(HttpStatusCode.BadRequest, response);
|
||||
settings.ExportSettings();
|
||||
break;
|
||||
case "NotificationConnectors/Update":
|
||||
if (!requestVariables.TryGetValue("notificationConnector", out notificationConnectorStr) ||
|
||||
|
@ -36,28 +36,21 @@ public class TrangaSettings
|
||||
|
||||
public TrangaSettings(string? downloadLocation = null, string? workingDirectory = null, int? apiPortNumber = null)
|
||||
{
|
||||
string wd = workingDirectory ?? Path.Join(RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "/usr/share" : Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "tranga-api");
|
||||
string sfp = Path.Join(wd, "settings.json");
|
||||
|
||||
string lockFilePath = $"{sfp}.lock";
|
||||
if (File.Exists(sfp) && !File.Exists(lockFilePath))
|
||||
string lockFilePath = $"{settingsFilePath}.lock";
|
||||
if (File.Exists(settingsFilePath) && !File.Exists(lockFilePath))
|
||||
{//Load from settings file
|
||||
FileStream lockFile = File.Create(lockFilePath,0, FileOptions.DeleteOnClose); //lock settingsfile
|
||||
string settingsStr = File.ReadAllText(sfp);
|
||||
string settingsStr = File.ReadAllText(settingsFilePath);
|
||||
TrangaSettings settings = JsonConvert.DeserializeObject<TrangaSettings>(settingsStr)!;
|
||||
this.requestLimits = settings.requestLimits;
|
||||
this.userAgent = settings.userAgent;
|
||||
this.downloadLocation = downloadLocation ?? settings.downloadLocation;
|
||||
this.workingDirectory = workingDirectory ?? settings.workingDirectory;
|
||||
this.apiPortNumber = apiPortNumber ?? settings.apiPortNumber;
|
||||
lockFile.Close(); //unlock settingsfile
|
||||
}
|
||||
else if(!File.Exists(sfp))
|
||||
else if(!File.Exists(settingsFilePath))
|
||||
{//No settings file exists
|
||||
if (downloadLocation?.Length < 1 || workingDirectory?.Length < 1)
|
||||
throw new ArgumentException("Download-location and working-directory paths can not be empty!");
|
||||
this.requestLimits = DefaultRequestLimits;
|
||||
this.userAgent = DefaultUserAgent;
|
||||
this.apiPortNumber = apiPortNumber ?? 6531;
|
||||
this.downloadLocation = downloadLocation ?? (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "/Manga" : Path.Join(Directory.GetCurrentDirectory(), "Downloads"));
|
||||
this.workingDirectory = workingDirectory ?? Path.Join(RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "/usr/share" : Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "tranga-api");
|
||||
@ -65,8 +58,6 @@ public class TrangaSettings
|
||||
}
|
||||
else
|
||||
{//Settingsfile is locked
|
||||
this.requestLimits = DefaultRequestLimits;
|
||||
this.userAgent = DefaultUserAgent;
|
||||
this.apiPortNumber = apiPortNumber!.Value;
|
||||
this.downloadLocation = downloadLocation!;
|
||||
this.workingDirectory = workingDirectory!;
|
||||
@ -139,12 +130,24 @@ public class TrangaSettings
|
||||
{
|
||||
if (File.Exists(settingsFilePath))
|
||||
{
|
||||
while(GlobalBase.IsFileInUse(settingsFilePath, null))
|
||||
Thread.Sleep(100);
|
||||
bool inUse = true;
|
||||
while (inUse)
|
||||
{
|
||||
try
|
||||
{
|
||||
using FileStream stream = new(settingsFilePath, FileMode.Open, FileAccess.Read, FileShare.None);
|
||||
stream.Close();
|
||||
inUse = false;
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
Directory.CreateDirectory(new FileInfo(settingsFilePath).DirectoryName!);
|
||||
File.WriteAllText(settingsFilePath, JsonConvert.SerializeObject(this, Formatting.Indented));
|
||||
File.WriteAllText(settingsFilePath, JsonConvert.SerializeObject(this));
|
||||
}
|
||||
|
||||
public string GetFullCoverPath(Manga manga)
|
||||
|
Loading…
Reference in New Issue
Block a user