2020-05-20 03:25:41 +00:00
using System.Threading.Tasks ;
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]
public class ModUpgrade : ControllerBase
{
private ILogger < ModUpgrade > _logger ;
private SqlService _sql ;
private DiscordWebHook _discord ;
private AppSettings _settings ;
2020-06-06 21:44:30 +00:00
private QuickSync _quickSync ;
2020-05-20 03:25:41 +00:00
2020-06-06 21:44:30 +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 ;
2020-06-06 21:44:30 +00:00
_quickSync = quickSync ;
2020-05-20 03:25:41 +00:00
}
[HttpPost]
[Route("/mod_upgrade")]
public async Task < IActionResult > PostModUpgrade ( )
{
var request = ( await Request . Body . ReadAllTextAsync ( ) ) . FromJsonString < ModUpgradeRequest > ( ) ;
if ( ! request . IsValid )
{
2020-06-05 20:53:44 +00:00
_logger . Log ( LogLevel . Information , $"Upgrade requested from {request.OldArchive.Hash} to {request.NewArchive.Hash} rejected as upgrade is invalid" ) ;
2020-05-20 03:25:41 +00:00
return BadRequest ( "Invalid mod upgrade" ) ;
}
2020-06-05 21:34:23 +00:00
if ( _settings . ValidateModUpgrades & & ! await _sql . HashIsInAModlist ( request . OldArchive . Hash ) )
2020-06-05 20:53:44 +00:00
{
_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
var oldDownload = await _sql . GetOrEnqueueArchive ( request . OldArchive ) ;
var newDownload = await _sql . GetOrEnqueueArchive ( request . NewArchive ) ;
var patch = await _sql . FindOrEnqueuePatch ( oldDownload . Id , newDownload . Id ) ;
if ( patch . Finished . HasValue )
{
if ( patch . PatchSize ! = 0 )
{
2020-06-05 20:53:44 +00:00
_logger . Log ( LogLevel . Information , $"Upgrade requested from {oldDownload.Archive.Hash} to {newDownload.Archive.Hash} patch Found" ) ;
2020-06-06 21:44:30 +00:00
await _sql . MarkPatchUsage ( oldDownload . Id , newDownload . Id ) ;
2020-05-20 03:25:41 +00:00
return
Ok (
2020-05-20 03:55:12 +00:00
$"https://{_settings.BunnyCDN_StorageZone}.b-cdn.net/{Consts.ArchiveUpdatesCDNFolder}/{request.OldArchive.Hash.ToHex()}_{request.NewArchive.Hash.ToHex()}" ) ;
2020-05-20 03:25:41 +00:00
}
2020-06-05 20:53:44 +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" ) ;
}
2020-06-06 21:44:30 +00:00
if ( ! newDownload . DownloadFinished . HasValue )
{
await _quickSync . Notify < ArchiveDownloader > ( ) ;
}
else
{
await _quickSync . Notify < PatchBuilder > ( ) ;
}
2020-06-05 20:53:44 +00:00
_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 ( ) ;
}
}
}