From 121c2482f81f878ca0d4a4929f2c0da57bbf7a30 Mon Sep 17 00:00:00 2001 From: ChiaraBm Date: Sun, 8 Dec 2024 13:25:44 +0100 Subject: [PATCH] Implemented v2.0 image import --- .../Models/Stars/LegacyImageImportModel.cs | 48 +++++++++ .../Stars/LegacyImageParseConfigModel.cs | 8 ++ .../Services/StarImportExportService.cs | 99 ++++++++++++++++++- 3 files changed, 152 insertions(+), 3 deletions(-) create mode 100644 MoonlightServers.ApiServer/Models/Stars/LegacyImageImportModel.cs create mode 100644 MoonlightServers.ApiServer/Models/Stars/LegacyImageParseConfigModel.cs diff --git a/MoonlightServers.ApiServer/Models/Stars/LegacyImageImportModel.cs b/MoonlightServers.ApiServer/Models/Stars/LegacyImageImportModel.cs new file mode 100644 index 0000000..5119879 --- /dev/null +++ b/MoonlightServers.ApiServer/Models/Stars/LegacyImageImportModel.cs @@ -0,0 +1,48 @@ +namespace MoonlightServers.ApiServer.Models.Stars; + +public class LegacyImageImportModel +{ + public string Name { get; set; } = ""; + + public string Author { get; set; } = ""; + public string? UpdateUrl { get; set; } + public string? DonateUrl { get; set; } + + public string StartupCommand { get; set; } = ""; + public string OnlineDetection { get; set; } = ""; + public string StopCommand { get; set; } = ""; + + public string InstallShell { get; set; } = ""; + public string InstallDockerImage { get; set; } = ""; + public string InstallScript { get; set; } = ""; + + public string ParseConfiguration { get; set; } = "[]"; + public int AllocationsNeeded { get; set; } = 1; + + public List Variables { get; set; } = new(); + + public int DefaultDockerImage { get; set; } = 0; + public bool AllowDockerImageChange { get; set; } = false; + public List DockerImages { get; set; } = new(); + + public class ImageVariable + { + public string Key { get; set; } = ""; + public string DefaultValue { get; set; } = ""; + + public string DisplayName { get; set; } = ""; + public string Description { get; set; } = ""; + + public bool AllowView { get; set; } = false; + public bool AllowEdit { get; set; } = false; + + public string? Filter { get; set; } + } + + public class ImageDockerImage // Weird name xd + { + public string DisplayName { get; set; } = ""; + public string Name { get; set; } = ""; + public bool AutoPull { get; set; } + } +} \ No newline at end of file diff --git a/MoonlightServers.ApiServer/Models/Stars/LegacyImageParseConfigModel.cs b/MoonlightServers.ApiServer/Models/Stars/LegacyImageParseConfigModel.cs new file mode 100644 index 0000000..52a02ef --- /dev/null +++ b/MoonlightServers.ApiServer/Models/Stars/LegacyImageParseConfigModel.cs @@ -0,0 +1,8 @@ +namespace MoonlightServers.ApiServer.Models.Stars; + +public class LegacyImageParseConfigModel +{ + public string Type { get; set; } = ""; + public string File { get; set; } = ""; + public Dictionary Configuration { get; set; } = new(); +} \ No newline at end of file diff --git a/MoonlightServers.ApiServer/Services/StarImportExportService.cs b/MoonlightServers.ApiServer/Services/StarImportExportService.cs index ece90fa..5763be8 100644 --- a/MoonlightServers.ApiServer/Services/StarImportExportService.cs +++ b/MoonlightServers.ApiServer/Services/StarImportExportService.cs @@ -5,6 +5,8 @@ using MoonCore.Exceptions; using MoonCore.Extended.Abstractions; using MoonlightServers.ApiServer.Database.Entities; using MoonlightServers.ApiServer.Models.Stars; +using MoonlightServers.Shared.Enums; +using MoonlightServers.Shared.Models; namespace MoonlightServers.ApiServer.Services; @@ -95,7 +97,7 @@ public class StarImportExportService { PropertyNameCaseInsensitive = true }); - + ArgumentNullException.ThrowIfNull(model); var star = new Star() @@ -140,13 +142,104 @@ public class StarImportExportService catch (Exception e) { Logger.LogError("An unhandled error occured while importing star: {e}", e); - throw new HttpApiException("An unhandled error occured while importing the star", 400); + throw new HttpApiException("An unhandled error occured while importing star", 400); } } public async Task ImportImage(string json) { - throw new NotImplementedException(); + try + { + var model = JsonSerializer.Deserialize(json, new JsonSerializerOptions() + { + PropertyNameCaseInsensitive = true + }); + + ArgumentNullException.ThrowIfNull(model); + + var star = new Star() + { + Name = model.Name, + Author = model.Author, + Version = "Imported from v2.0", + DonateUrl = model.DonateUrl, + UpdateUrl = model.UpdateUrl, + InstallScript = model.InstallScript, + InstallShell = model.InstallShell, + InstallDockerImage = model.InstallDockerImage, + OnlineDetection = model.OnlineDetection, + StopCommand = model.StopCommand, + StartupCommand = model.StartupCommand, + RequiredAllocations = model.AllocationsNeeded, + AllowDockerImageChange = model.AllowDockerImageChange, + Variables = model.Variables.Select(x => new StarVariable() + { + DefaultValue = x.DefaultValue, + Description = x.Description, + Filter = x.Filter, + Key = x.Key, + AllowEditing = x.AllowEdit, + AllowViewing = x.AllowView, + Type = StarVariableType.Text, + Name = x.DisplayName + }).ToList(), + DockerImages = model.DockerImages.Select(x => new StarDockerImage() + { + DisplayName = x.DisplayName, + AutoPulling = x.AutoPull, + Identifier = x.Name + }).ToList() + }; + + #region Convert parse configurations + + var oldParseConfig = JsonSerializer.Deserialize(model.ParseConfiguration, + new JsonSerializerOptions() + { + PropertyNameCaseInsensitive = true + } + ); + + ArgumentNullException.ThrowIfNull(oldParseConfig); + + var newParseConfig = new List(); + + // Remap values + foreach (var config in oldParseConfig) + { + var parseConfiguration = new ParseConfiguration() + { + File = config.File, + Parser = Enum.TryParse(config.Type, true, out FileParsers parserType) + ? parserType + : FileParsers.File + }; + + foreach (var option in config.Configuration) + { + parseConfiguration.Entries.Add(new ParseConfiguration.ParseConfigurationEntry() + { + Key = option.Key, + Value = option.Value + }); + } + + newParseConfig.Add(parseConfiguration); + } + + star.ParseConfiguration = JsonSerializer.Serialize(newParseConfig); + + #endregion + + var finalStar = StarRepository.Add(star); + + return finalStar; + } + catch (Exception e) + { + Logger.LogError("An unhandled error occured while importing image: {e}", e); + throw new HttpApiException("An unhandled error occured while importing image", 400); + } } public async Task ImportEgg(string json)