mirror of
https://github.com/C9Glax/tranga.git
synced 2025-01-12 19:37:33 +01:00
added logging
This commit is contained in:
parent
01c8784bab
commit
00861c406a
@ -1,4 +1,5 @@
|
|||||||
using System.Text.Json.Nodes;
|
using System.Text.Json.Nodes;
|
||||||
|
using Logging;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Tranga.LibraryManagers;
|
using Tranga.LibraryManagers;
|
||||||
using Tranga.NotificationManagers;
|
using Tranga.NotificationManagers;
|
||||||
@ -9,21 +10,22 @@ namespace Tranga;
|
|||||||
public static class Migrator
|
public static class Migrator
|
||||||
{
|
{
|
||||||
private static readonly ushort CurrentVersion = 17;
|
private static readonly ushort CurrentVersion = 17;
|
||||||
public static void Migrate(string settingsFilePath)
|
public static void Migrate(string settingsFilePath, Logger? logger)
|
||||||
{
|
{
|
||||||
if (!File.Exists(settingsFilePath))
|
if (!File.Exists(settingsFilePath))
|
||||||
return;
|
return;
|
||||||
JsonNode settingsNode = JsonNode.Parse(File.ReadAllText(settingsFilePath))!;
|
JsonNode settingsNode = JsonNode.Parse(File.ReadAllText(settingsFilePath))!;
|
||||||
ushort version = settingsNode["version"]!.GetValue<ushort>();
|
ushort version = settingsNode["version"]!.GetValue<ushort>();
|
||||||
|
logger?.WriteLine("Migrator", $"Migrating {version} -> {CurrentVersion}");
|
||||||
switch (version)
|
switch (version)
|
||||||
{
|
{
|
||||||
case 15:
|
case 15:
|
||||||
MoveToCommonObjects(settingsFilePath);
|
MoveToCommonObjects(settingsFilePath, logger);
|
||||||
TrangaSettings.SettingsJsonObject sjo = JsonConvert.DeserializeObject<TrangaSettings.SettingsJsonObject>(File.ReadAllText(settingsFilePath))!;
|
TrangaSettings.SettingsJsonObject sjo = JsonConvert.DeserializeObject<TrangaSettings.SettingsJsonObject>(File.ReadAllText(settingsFilePath))!;
|
||||||
RemoveUpdateLibraryTask(sjo.ts!);
|
RemoveUpdateLibraryTask(sjo.ts!, logger);
|
||||||
break;
|
break;
|
||||||
case 16:
|
case 16:
|
||||||
MoveToCommonObjects(settingsFilePath);
|
MoveToCommonObjects(settingsFilePath, logger);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
TrangaSettings.SettingsJsonObject sjo2 = JsonConvert.DeserializeObject<TrangaSettings.SettingsJsonObject>(File.ReadAllText(settingsFilePath))!;
|
TrangaSettings.SettingsJsonObject sjo2 = JsonConvert.DeserializeObject<TrangaSettings.SettingsJsonObject>(File.ReadAllText(settingsFilePath))!;
|
||||||
@ -31,27 +33,30 @@ public static class Migrator
|
|||||||
sjo2.ts!.ExportSettings();
|
sjo2.ts!.ExportSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void RemoveUpdateLibraryTask(TrangaSettings settings)
|
private static void RemoveUpdateLibraryTask(TrangaSettings settings, Logger? logger)
|
||||||
{
|
{
|
||||||
if (!File.Exists(settings.tasksFilePath))
|
if (!File.Exists(settings.tasksFilePath))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
logger?.WriteLine("Migrator", "Removing old/deprecated UpdateLibraryTasks (v16)");
|
||||||
string tasksJsonString = File.ReadAllText(settings.tasksFilePath);
|
string tasksJsonString = File.ReadAllText(settings.tasksFilePath);
|
||||||
HashSet<TrangaTask> tasks = JsonConvert.DeserializeObject<HashSet<TrangaTask>>(tasksJsonString, new JsonSerializerSettings { Converters = { new TrangaTask.TrangaTaskJsonConverter() } })!;
|
HashSet<TrangaTask> tasks = JsonConvert.DeserializeObject<HashSet<TrangaTask>>(tasksJsonString, new JsonSerializerSettings { Converters = { new TrangaTask.TrangaTaskJsonConverter() } })!;
|
||||||
tasks.RemoveWhere(t => t.task == TrangaTask.Task.UpdateLibraries);
|
tasks.RemoveWhere(t => t.task == TrangaTask.Task.UpdateLibraries);
|
||||||
File.WriteAllText(settings.tasksFilePath, JsonConvert.SerializeObject(tasks));
|
File.WriteAllText(settings.tasksFilePath, JsonConvert.SerializeObject(tasks));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void MoveToCommonObjects(string settingsFilePath)
|
public static void MoveToCommonObjects(string settingsFilePath, Logger? logger)
|
||||||
{
|
{
|
||||||
if (!File.Exists(settingsFilePath))
|
if (!File.Exists(settingsFilePath))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
logger?.WriteLine("Migrator", "Moving Settings to commonObjects-structure (v17)");
|
||||||
JsonNode node = JsonNode.Parse(File.ReadAllText(settingsFilePath))!;
|
JsonNode node = JsonNode.Parse(File.ReadAllText(settingsFilePath))!;
|
||||||
TrangaSettings settings = new(
|
TrangaSettings ts = new(
|
||||||
node["downloadLocation"]!.GetValue<string>(),
|
node["downloadLocation"]!.GetValue<string>(),
|
||||||
node["workingDirectory"]!.GetValue<string>());
|
node["workingDirectory"]!.GetValue<string>());
|
||||||
JsonArray libraryManagers = node["libraryManagers"]!.AsArray();
|
JsonArray libraryManagers = node["libraryManagers"]!.AsArray();
|
||||||
|
logger?.WriteLine("Migrator", $"\tGot {libraryManagers.Count} libraryManagers.");
|
||||||
JsonNode? komgaNode = libraryManagers.FirstOrDefault(lm => lm["libraryType"].GetValue<byte>() == (byte)LibraryManager.LibraryType.Komga);
|
JsonNode? komgaNode = libraryManagers.FirstOrDefault(lm => lm["libraryType"].GetValue<byte>() == (byte)LibraryManager.LibraryType.Komga);
|
||||||
JsonNode? kavitaNode = libraryManagers.FirstOrDefault(lm => lm["libraryType"].GetValue<byte>() == (byte)LibraryManager.LibraryType.Kavita);
|
JsonNode? kavitaNode = libraryManagers.FirstOrDefault(lm => lm["libraryType"].GetValue<byte>() == (byte)LibraryManager.LibraryType.Kavita);
|
||||||
HashSet<LibraryManager> lms = new();
|
HashSet<LibraryManager> lms = new();
|
||||||
@ -61,6 +66,7 @@ public static class Migrator
|
|||||||
lms.Add(new Kavita(kavitaNode["baseUrl"]!.GetValue<string>(), kavitaNode["auth"]!.GetValue<string>(), null));
|
lms.Add(new Kavita(kavitaNode["baseUrl"]!.GetValue<string>(), kavitaNode["auth"]!.GetValue<string>(), null));
|
||||||
|
|
||||||
JsonArray notificationManagers = node["notificationManagers"]!.AsArray();
|
JsonArray notificationManagers = node["notificationManagers"]!.AsArray();
|
||||||
|
logger?.WriteLine("Migrator", $"\tGot {notificationManagers.Count} notificationManagers.");
|
||||||
JsonNode? gotifyNode = notificationManagers.FirstOrDefault(nm =>
|
JsonNode? gotifyNode = notificationManagers.FirstOrDefault(nm =>
|
||||||
nm["notificationManagerType"].GetValue<byte>() == (byte)NotificationManager.NotificationManagerType.Gotify);
|
nm["notificationManagerType"].GetValue<byte>() == (byte)NotificationManager.NotificationManagerType.Gotify);
|
||||||
JsonNode? lunaSeaNode = notificationManagers.FirstOrDefault(nm =>
|
JsonNode? lunaSeaNode = notificationManagers.FirstOrDefault(nm =>
|
||||||
@ -73,7 +79,7 @@ public static class Migrator
|
|||||||
|
|
||||||
CommonObjects co = new (lms, nms, null, settingsFilePath);
|
CommonObjects co = new (lms, nms, null, settingsFilePath);
|
||||||
|
|
||||||
TrangaSettings.SettingsJsonObject sjo = new(settings, co);
|
TrangaSettings.SettingsJsonObject sjo = new(ts, co);
|
||||||
File.WriteAllText(settingsFilePath, JsonConvert.SerializeObject(sjo));
|
File.WriteAllText(settingsFilePath, JsonConvert.SerializeObject(sjo));
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -31,7 +31,7 @@ public static class Tranga
|
|||||||
" Starting Tranga-API\n"+
|
" Starting Tranga-API\n"+
|
||||||
"-------------------------------------------");
|
"-------------------------------------------");
|
||||||
logger.WriteLine("Tranga", "Migrating...");
|
logger.WriteLine("Tranga", "Migrating...");
|
||||||
Migrator.Migrate(settingsFilePath);
|
Migrator.Migrate(settingsFilePath, logger);
|
||||||
|
|
||||||
TrangaSettings settings;
|
TrangaSettings settings;
|
||||||
if (File.Exists(settingsFilePath))
|
if (File.Exists(settingsFilePath))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user