2020-06-14 13:13:29 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2020-07-16 21:17:37 +00:00
|
|
|
|
using System.IO.Compression;
|
2020-06-14 13:13:29 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
2020-07-16 21:17:37 +00:00
|
|
|
|
using System.Threading;
|
2020-06-14 13:13:29 +00:00
|
|
|
|
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;
|
|
|
|
|
using Wabbajack.Lib;
|
|
|
|
|
using Wabbajack.Server.DataLayer;
|
|
|
|
|
using Wabbajack.Server.Services;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack.BuildServer.Controllers
|
|
|
|
|
{
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("/list_definitions")]
|
|
|
|
|
public class ListDefinitions : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private ILogger<ListDefinitions> _logger;
|
|
|
|
|
private SqlService _sql;
|
|
|
|
|
private DiscordWebHook _discord;
|
|
|
|
|
|
|
|
|
|
public ListDefinitions(ILogger<ListDefinitions> logger, SqlService sql, DiscordWebHook discord)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_sql = sql;
|
|
|
|
|
_discord = discord;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Route("ingest")]
|
2020-06-16 22:21:01 +00:00
|
|
|
|
[Authorize(Roles = "User")]
|
2020-06-14 13:13:29 +00:00
|
|
|
|
[HttpPost]
|
|
|
|
|
public async Task<IActionResult> PostIngest()
|
|
|
|
|
{
|
|
|
|
|
var user = Request.Headers[Consts.MetricsKeyHeader].First();
|
2020-07-16 21:17:37 +00:00
|
|
|
|
var use_gzip = Request.Headers[Consts.CompressedBodyHeader].Any();
|
2020-06-14 13:13:29 +00:00
|
|
|
|
_logger.Log(LogLevel.Information, $"Ingesting Modlist Definition for {user}");
|
2020-07-16 21:17:37 +00:00
|
|
|
|
|
2020-06-14 13:13:29 +00:00
|
|
|
|
var modlistBytes = await Request.Body.ReadAllAsync();
|
2020-07-16 21:17:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Spawning ingestion task");
|
|
|
|
|
var tsk = Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (use_gzip)
|
|
|
|
|
{
|
|
|
|
|
await using var os = new MemoryStream();
|
|
|
|
|
await using var gZipStream =
|
|
|
|
|
new GZipStream(new MemoryStream(modlistBytes), CompressionMode.Decompress);
|
|
|
|
|
await gZipStream.CopyToAsync(os);
|
|
|
|
|
modlistBytes = os.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var modlist = new MemoryStream(modlistBytes).FromJson<ModList>();
|
|
|
|
|
|
|
|
|
|
var file = AbsolutePath.EntryPoint.Combine("mod_list_definitions")
|
|
|
|
|
.Combine($"{user}_{DateTime.UtcNow.ToFileTimeUtc()}.json");
|
|
|
|
|
file.Parent.CreateDirectory();
|
|
|
|
|
await using var stream = await file.Create();
|
|
|
|
|
modlist.ToJson(stream);
|
|
|
|
|
_logger.Log(LogLevel.Information, $"Done Ingesting Modlist Definition for {user}");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Error ingesting uploaded modlist");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2020-06-14 13:13:29 +00:00
|
|
|
|
return Accepted(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|