88 lines
2.9 KiB
C#
88 lines
2.9 KiB
C#
using System.Text;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using MoonlightServers.Shared.Admin.Templates;
|
|
using VYaml.Serialization;
|
|
|
|
namespace MoonlightServers.Api.Admin.Templates;
|
|
|
|
[ApiController]
|
|
[Route("api/admin/servers/templates")]
|
|
public class TransferController : Controller
|
|
{
|
|
private readonly TemplateTransferService TransferService;
|
|
|
|
public TransferController(TemplateTransferService transferService)
|
|
{
|
|
TransferService = transferService;
|
|
}
|
|
|
|
[HttpGet("{id:int}/export")]
|
|
public async Task<ActionResult> ExportAsync([FromRoute] int id)
|
|
{
|
|
var transferModel = await TransferService.ExportAsync(id);
|
|
|
|
if (transferModel == null)
|
|
return Problem("No template with that id found", statusCode: 404);
|
|
|
|
var yml = YamlSerializer.Serialize(transferModel, new YamlSerializerOptions
|
|
{
|
|
Resolver = CompositeResolver.Create([
|
|
GeneratedResolver.Instance,
|
|
StandardResolver.Instance
|
|
])
|
|
});
|
|
|
|
return File(yml.ToArray(), "text/yaml", $"{transferModel.Name}.yml");
|
|
}
|
|
|
|
[HttpPost("import")]
|
|
public async Task<ActionResult<TemplateDto>> ImportAsync()
|
|
{
|
|
string content;
|
|
|
|
await using (Stream receiveStream = Request.Body)
|
|
|
|
using (StreamReader readStream = new StreamReader(receiveStream))
|
|
content = await readStream.ReadToEndAsync();
|
|
|
|
if(content.Contains("version: PLCN_v3"))
|
|
{
|
|
var importService = HttpContext.RequestServices.GetRequiredService<PelicanEggImportService>();
|
|
|
|
var template = await importService.ImportAsync(content);
|
|
|
|
return TemplateMapper.ToDto(template);
|
|
}
|
|
if (
|
|
content.Contains("PTDL_v2", StringComparison.OrdinalIgnoreCase) ||
|
|
content.Contains("PLCN_v1", StringComparison.OrdinalIgnoreCase) ||
|
|
content.Contains("PLCN_v2", StringComparison.OrdinalIgnoreCase) ||
|
|
content.Contains("PLCN_v3", StringComparison.OrdinalIgnoreCase)
|
|
)
|
|
{
|
|
var importService = HttpContext.RequestServices.GetRequiredService<PterodactylEggImportService>();
|
|
|
|
var template = await importService.ImportAsync(content);
|
|
|
|
return TemplateMapper.ToDto(template);
|
|
}
|
|
else
|
|
{
|
|
var transferModel = YamlSerializer.Deserialize<TemplateTransferModel>(
|
|
Encoding.UTF8.GetBytes(content),
|
|
new YamlSerializerOptions
|
|
{
|
|
Resolver = CompositeResolver.Create([
|
|
GeneratedResolver.Instance,
|
|
StandardResolver.Instance
|
|
])
|
|
}
|
|
);
|
|
|
|
var template = await TransferService.ImportAsync(transferModel);
|
|
|
|
return TemplateMapper.ToDto(template);
|
|
}
|
|
}
|
|
} |