wabbajack/Wabbajack.Server/Controllers/ListDefinitions.cs

83 lines
2.7 KiB
C#
Raw Normal View History

2020-06-14 13:13:29 +00:00
using System;
using System.IO;
using System.IO.Compression;
2020-06-14 13:13:29 +00:00
using System.Linq;
using System.Threading.Tasks;
2020-06-16 22:21:01 +00:00
using Microsoft.AspNetCore.Authorization;
2020-06-14 13:13:29 +00:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Wabbajack.Common;
2021-09-27 12:42:46 +00:00
using Wabbajack.DTOs;
using Wabbajack.DTOs.JsonConverters;
using Wabbajack.Paths.IO;
2020-06-14 13:13:29 +00:00
using Wabbajack.Server.DataLayer;
using Wabbajack.Server.Services;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.BuildServer.Controllers;
[ApiController]
[Route("/list_definitions")]
public class ListDefinitions : ControllerBase
2020-06-14 13:13:29 +00:00
{
2021-10-23 16:51:17 +00:00
private readonly DTOSerializer _dtos;
private readonly AppSettings _settings;
private DiscordWebHook _discord;
private readonly ILogger<ListDefinitions> _logger;
private SqlService _sql;
2020-06-14 13:13:29 +00:00
2021-10-23 16:51:17 +00:00
public ListDefinitions(ILogger<ListDefinitions> logger, SqlService sql, DiscordWebHook discord,
AppSettings settings,
DTOSerializer dtos)
{
_logger = logger;
_sql = sql;
_discord = discord;
_settings = settings;
_dtos = dtos;
}
2020-06-14 13:13:29 +00:00
2021-10-23 16:51:17 +00:00
[Route("ingest")]
[Authorize(Roles = "User")]
[HttpPost]
public async Task<IActionResult> PostIngest()
{
var user = Request.Headers[_settings.MetricsKeyHeader].First();
var use_gzip = Request.Headers[_settings.CompressedBodyHeader].Any();
_logger.Log(LogLevel.Information, $"Ingesting Modlist Definition for {user}");
2021-10-23 16:51:17 +00:00
var modlistBytes = await Request.Body.ReadAllAsync();
2021-10-23 16:51:17 +00:00
_logger.LogInformation("Spawning ingestion task");
var tsk = Task.Run(async () =>
{
try
{
2021-10-23 16:51:17 +00:00
if (use_gzip)
{
2021-10-23 16:51:17 +00:00
await using var os = new MemoryStream();
await using var gZipStream =
new GZipStream(new MemoryStream(modlistBytes), CompressionMode.Decompress);
await gZipStream.CopyToAsync(os);
modlistBytes = os.ToArray();
}
2021-10-23 16:51:17 +00:00
var modlist = _dtos.DeserializeAsync<ModList>(new MemoryStream(modlistBytes));
2021-10-23 16:51:17 +00:00
var file = KnownFolders.EntryPoint.Combine("mod_list_definitions")
.Combine($"{user}_{DateTime.UtcNow.ToFileTimeUtc()}.json");
file.Parent.CreateDirectory();
await using var stream = file.Open(FileMode.Create, FileAccess.Write);
await _dtos.Serialize(modlist, stream);
_logger.Log(LogLevel.Information, $"Done Ingesting Modlist Definition for {user}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Error ingesting uploaded modlist");
}
});
return Accepted(0);
2020-06-14 13:13:29 +00:00
}
2021-10-23 16:51:17 +00:00
}