wabbajack/Wabbajack.Server/Controllers/ModUpgrade.cs

164 lines
6.5 KiB
C#
Raw Normal View History

2020-07-13 22:10:05 +00:00
using System;
using System.Linq;
using System.Security.Claims;
2020-08-06 22:40:07 +00:00
using System.Text;
2020-07-13 22:10:05 +00:00
using System.Threading.Tasks;
2020-06-16 22:21:01 +00:00
using Microsoft.AspNetCore.Authorization;
2020-05-20 03:25:41 +00:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Wabbajack.Common;
using Wabbajack.Lib;
using Wabbajack.Server.DataLayer;
using Wabbajack.Server.DTOs;
2020-05-20 03:25:41 +00:00
using Wabbajack.Server.Services;
namespace Wabbajack.BuildServer.Controllers
{
[ApiController]
public class ModUpgrade : ControllerBase
{
private ILogger<ModUpgrade> _logger;
private SqlService _sql;
private DiscordWebHook _discord;
private AppSettings _settings;
private QuickSync _quickSync;
private Task<BunnyCdnFtpInfo> _creds;
2020-08-26 04:03:43 +00:00
private Task<BunnyCdnFtpInfo> _mirrorCreds;
2020-05-20 03:25:41 +00:00
public ModUpgrade(ILogger<ModUpgrade> logger, SqlService sql, DiscordWebHook discord, QuickSync quickSync, AppSettings settings)
2020-05-20 03:25:41 +00:00
{
_logger = logger;
_sql = sql;
_discord = discord;
_settings = settings;
_quickSync = quickSync;
_creds = BunnyCdnFtpInfo.GetCreds(StorageSpace.Patches);
2020-08-26 04:03:43 +00:00
_mirrorCreds = BunnyCdnFtpInfo.GetCreds(StorageSpace.Mirrors);
2020-05-20 03:25:41 +00:00
}
[HttpPost]
2020-06-16 22:21:01 +00:00
[Authorize(Roles = "User")]
2020-05-20 03:25:41 +00:00
[Route("/mod_upgrade")]
public async Task<IActionResult> PostModUpgrade()
{
2020-07-13 22:10:05 +00:00
var isAuthor = User.Claims.Any(c => c.Type == ClaimTypes.Role && c.Value == "Author");
2020-05-20 03:25:41 +00:00
var request = (await Request.Body.ReadAllTextAsync()).FromJsonString<ModUpgradeRequest>();
2020-07-13 22:10:05 +00:00
if (!isAuthor)
2020-05-20 03:25:41 +00:00
{
2020-07-13 22:10:05 +00:00
var srcDownload = await _sql.GetArchiveDownload(request.OldArchive.State.PrimaryKeyString,
request.OldArchive.Hash, request.OldArchive.Size);
var destDownload = await _sql.GetArchiveDownload(request.NewArchive.State.PrimaryKeyString,
request.NewArchive.Hash, request.NewArchive.Size);
if (srcDownload == default || destDownload == default ||
await _sql.FindPatch(srcDownload.Id, destDownload.Id) == default)
{
if (!await request.IsValid())
{
_logger.Log(LogLevel.Information,
$"Upgrade requested from {request.OldArchive.Hash} to {request.NewArchive.Hash} rejected as upgrade is invalid");
return BadRequest("Invalid mod upgrade");
}
if (_settings.ValidateModUpgrades && !await _sql.HashIsInAModlist(request.OldArchive.Hash))
{
_logger.Log(LogLevel.Information,
$"Upgrade requested from {request.OldArchive.Hash} to {request.NewArchive.Hash} rejected as src hash is not in a curated modlist");
return BadRequest("Hash is not in a recent modlist");
}
}
2020-05-20 03:25:41 +00:00
}
2020-07-13 22:10:05 +00:00
try
{
if (await request.OldArchive.State.Verify(request.OldArchive))
{
//_logger.LogInformation(
// $"Refusing to upgrade ({request.OldArchive.State.PrimaryKeyString}), old archive is valid");
2020-07-13 22:10:05 +00:00
return NotFound("File is Valid");
}
}
2020-10-01 03:50:09 +00:00
catch (Exception)
{
//_logger.LogInformation(
// $"Refusing to upgrade ({request.OldArchive.State.PrimaryKeyString}), due to upgrade failure");
return NotFound("File is Valid");
}
2020-07-13 22:10:05 +00:00
2020-05-20 03:25:41 +00:00
var oldDownload = await _sql.GetOrEnqueueArchive(request.OldArchive);
2020-08-06 22:40:07 +00:00
if (await _sql.IsNoPatch(oldDownload.Archive.Hash))
{
return BadRequest("File has NoPatch attached");
}
2020-05-20 03:25:41 +00:00
var newDownload = await _sql.GetOrEnqueueArchive(request.NewArchive);
var patch = await _sql.FindOrEnqueuePatch(oldDownload.Id, newDownload.Id);
if (patch.Finished.HasValue)
{
if (patch.PatchSize != 0)
{
//_logger.Log(LogLevel.Information, $"Upgrade requested from {oldDownload.Archive.Hash} to {newDownload.Archive.Hash} patch Found");
await _sql.MarkPatchUsage(oldDownload.Id, newDownload.Id);
2020-05-20 03:25:41 +00:00
return
Ok(
$"https://{(await _creds).Username}.b-cdn.net/{request.OldArchive.Hash.ToHex()}_{request.NewArchive.Hash.ToHex()}");
2020-05-20 03:25:41 +00:00
}
//_logger.Log(LogLevel.Information, $"Upgrade requested from {oldDownload.Archive.Hash} to {newDownload.Archive.Hash} patch found but was failed");
2020-05-20 03:25:41 +00:00
return NotFound("Patch creation failed");
}
if (!newDownload.DownloadFinished.HasValue)
{
await _quickSync.Notify<ArchiveDownloader>();
}
else
{
await _quickSync.Notify<PatchBuilder>();
}
//_logger.Log(LogLevel.Information, $"Upgrade requested from {oldDownload.Archive.Hash} to {newDownload.Archive.Hash} patch found is processing");
2020-05-20 03:25:41 +00:00
// Still processing
return Accepted();
}
2020-07-13 22:10:05 +00:00
[HttpGet]
[Authorize(Roles = "User")]
[Route("/mod_upgrade/find/{hashAsHex}")]
public async Task<IActionResult> FindUpgrade(string hashAsHex)
{
var hash = Hash.FromHex(hashAsHex);
var patches = await _sql.PatchesForSource(hash);
return Ok(patches.Select(p => p.Dest.Archive).ToList().ToJson());
2020-07-13 22:10:05 +00:00
}
2020-08-06 22:40:07 +00:00
[HttpGet]
[Authorize(Roles = "Author")]
[Route("/mod_upgrade/no_patch/{hashAsHex}/{rationaleAsHex}")]
public async Task<IActionResult> PurgePatch(string hashAsHex, string rationaleAsHex)
{
var hash = Hash.FromHex(hashAsHex);
var rationale = Encoding.UTF8.GetString(rationaleAsHex.FromHex());
await _sql.PurgePatch(hash, rationale);
return Ok("Purged");
}
2020-08-08 03:40:03 +00:00
[HttpGet]
[Authorize(Roles = "User")]
[Route("/mirror/{hashAsHex}")]
public async Task<IActionResult> HaveHash(string hashAsHex)
{
var result = await _sql.HaveMirror(Hash.FromHex(hashAsHex));
2020-08-26 04:03:43 +00:00
if (result) return Ok($"https://{(await _mirrorCreds).Username}.b-cdn.net/{hashAsHex}");
2020-08-08 03:40:03 +00:00
return NotFound("Not Mirrored");
}
2020-07-13 22:10:05 +00:00
2020-05-20 03:25:41 +00:00
}
}