Tranga-Website/Tranga/Migrator.cs

85 lines
4.4 KiB
C#
Raw Normal View History

2023-07-31 01:26:38 +02:00
using System.Text.Json.Nodes;
2023-07-31 01:42:15 +02:00
using Logging;
2023-07-31 01:26:38 +02:00
using Newtonsoft.Json;
using Tranga.LibraryManagers;
using Tranga.NotificationManagers;
using Tranga.TrangaTasks;
namespace Tranga;
public static class Migrator
{
private static readonly ushort CurrentVersion = 17;
2023-07-31 01:42:15 +02:00
public static void Migrate(string settingsFilePath, Logger? logger)
2023-07-31 01:26:38 +02:00
{
if (!File.Exists(settingsFilePath))
return;
JsonNode settingsNode = JsonNode.Parse(File.ReadAllText(settingsFilePath))!;
ushort version = settingsNode["version"]!.GetValue<ushort>();
2023-07-31 01:42:15 +02:00
logger?.WriteLine("Migrator", $"Migrating {version} -> {CurrentVersion}");
2023-07-31 01:26:38 +02:00
switch (version)
{
case 15:
2023-07-31 01:42:15 +02:00
MoveToCommonObjects(settingsFilePath, logger);
2023-07-31 01:26:38 +02:00
TrangaSettings.SettingsJsonObject sjo = JsonConvert.DeserializeObject<TrangaSettings.SettingsJsonObject>(File.ReadAllText(settingsFilePath))!;
2023-07-31 01:42:15 +02:00
RemoveUpdateLibraryTask(sjo.ts!, logger);
2023-07-31 01:26:38 +02:00
break;
case 16:
2023-07-31 01:42:15 +02:00
MoveToCommonObjects(settingsFilePath, logger);
2023-07-31 01:26:38 +02:00
break;
}
TrangaSettings.SettingsJsonObject sjo2 = JsonConvert.DeserializeObject<TrangaSettings.SettingsJsonObject>(File.ReadAllText(settingsFilePath))!;
sjo2.ts!.version = CurrentVersion;
sjo2.ts!.ExportSettings();
}
2023-07-31 01:42:15 +02:00
private static void RemoveUpdateLibraryTask(TrangaSettings settings, Logger? logger)
2023-07-31 01:26:38 +02:00
{
if (!File.Exists(settings.tasksFilePath))
return;
2023-07-31 01:42:15 +02:00
logger?.WriteLine("Migrator", "Removing old/deprecated UpdateLibraryTasks (v16)");
2023-07-31 01:26:38 +02:00
string tasksJsonString = File.ReadAllText(settings.tasksFilePath);
HashSet<TrangaTask> tasks = JsonConvert.DeserializeObject<HashSet<TrangaTask>>(tasksJsonString, new JsonSerializerSettings { Converters = { new TrangaTask.TrangaTaskJsonConverter() } })!;
tasks.RemoveWhere(t => t.task == TrangaTask.Task.UpdateLibraries);
File.WriteAllText(settings.tasksFilePath, JsonConvert.SerializeObject(tasks));
}
2023-07-31 01:42:15 +02:00
public static void MoveToCommonObjects(string settingsFilePath, Logger? logger)
2023-07-31 01:26:38 +02:00
{
if (!File.Exists(settingsFilePath))
return;
2023-07-31 01:42:15 +02:00
logger?.WriteLine("Migrator", "Moving Settings to commonObjects-structure (v17)");
2023-07-31 01:26:38 +02:00
JsonNode node = JsonNode.Parse(File.ReadAllText(settingsFilePath))!;
2023-07-31 01:42:15 +02:00
TrangaSettings ts = new(
2023-07-31 01:26:38 +02:00
node["downloadLocation"]!.GetValue<string>(),
node["workingDirectory"]!.GetValue<string>());
JsonArray libraryManagers = node["libraryManagers"]!.AsArray();
2023-07-31 01:42:15 +02:00
logger?.WriteLine("Migrator", $"\tGot {libraryManagers.Count} libraryManagers.");
2023-07-31 01:28:32 +02:00
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);
2023-07-31 01:26:38 +02:00
HashSet<LibraryManager> lms = new();
if (komgaNode is not null)
lms.Add(new Komga(komgaNode["baseUrl"]!.GetValue<string>(), komgaNode["auth"]!.GetValue<string>(), null));
if (kavitaNode is not null)
lms.Add(new Kavita(kavitaNode["baseUrl"]!.GetValue<string>(), kavitaNode["auth"]!.GetValue<string>(), null));
2023-07-31 01:30:32 +02:00
JsonArray notificationManagers = node["notificationManagers"]!.AsArray();
2023-07-31 01:42:15 +02:00
logger?.WriteLine("Migrator", $"\tGot {notificationManagers.Count} notificationManagers.");
2023-07-31 01:26:38 +02:00
JsonNode? gotifyNode = notificationManagers.FirstOrDefault(nm =>
2023-07-31 01:28:32 +02:00
nm["notificationManagerType"].GetValue<byte>() == (byte)NotificationManager.NotificationManagerType.Gotify);
2023-07-31 01:26:38 +02:00
JsonNode? lunaSeaNode = notificationManagers.FirstOrDefault(nm =>
2023-07-31 01:28:32 +02:00
nm["notificationManagerType"].GetValue<byte>() == (byte)NotificationManager.NotificationManagerType.LunaSea);
2023-07-31 01:26:38 +02:00
HashSet<NotificationManager> nms = new();
if (gotifyNode is not null)
nms.Add(new Gotify(gotifyNode["endpoint"]!.GetValue<string>(), gotifyNode["appToken"]!.GetValue<string>()));
if (lunaSeaNode is not null)
nms.Add(new LunaSea(lunaSeaNode["id"]!.GetValue<string>()));
CommonObjects co = new (lms, nms, null, settingsFilePath);
2023-07-31 01:42:15 +02:00
TrangaSettings.SettingsJsonObject sjo = new(ts, co);
2023-07-31 01:26:38 +02:00
File.WriteAllText(settingsFilePath, JsonConvert.SerializeObject(sjo));
}
}