Implemented v2.0 image import

This commit is contained in:
2024-12-08 13:25:44 +01:00
parent ebab0daf26
commit 121c2482f8
3 changed files with 152 additions and 3 deletions

View File

@@ -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<ImageVariable> Variables { get; set; } = new();
public int DefaultDockerImage { get; set; } = 0;
public bool AllowDockerImageChange { get; set; } = false;
public List<ImageDockerImage> 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; }
}
}

View File

@@ -0,0 +1,8 @@
namespace MoonlightServers.ApiServer.Models.Stars;
public class LegacyImageParseConfigModel
{
public string Type { get; set; } = "";
public string File { get; set; } = "";
public Dictionary<string, string> Configuration { get; set; } = new();
}

View File

@@ -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<Star> ImportImage(string json)
{
throw new NotImplementedException();
try
{
var model = JsonSerializer.Deserialize<LegacyImageImportModel>(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<LegacyImageParseConfigModel[]>(model.ParseConfiguration,
new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = true
}
);
ArgumentNullException.ThrowIfNull(oldParseConfig);
var newParseConfig = new List<ParseConfiguration>();
// 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<Star> ImportEgg(string json)