2020-05-09 03:56:06 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2021-09-27 12:42:46 +00:00
|
|
|
|
using Wabbajack.DTOs;
|
|
|
|
|
using Wabbajack.Networking.NexusApi;
|
|
|
|
|
using Wabbajack.Networking.NexusApi.DTOs;
|
2020-05-09 03:56:06 +00:00
|
|
|
|
using Wabbajack.Server.DataLayer;
|
2020-08-12 04:25:12 +00:00
|
|
|
|
using Wabbajack.Server.Services;
|
2020-05-09 03:56:06 +00:00
|
|
|
|
|
|
|
|
|
namespace Wabbajack.BuildServer.Controllers
|
|
|
|
|
{
|
|
|
|
|
//[Authorize]
|
|
|
|
|
[ApiController]
|
2020-06-16 22:21:01 +00:00
|
|
|
|
[Authorize(Roles = "User")]
|
2020-05-09 03:56:06 +00:00
|
|
|
|
[Route("/v1/games/")]
|
|
|
|
|
public class NexusCache : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private AppSettings _settings;
|
|
|
|
|
private SqlService _sql;
|
|
|
|
|
private ILogger<NexusCache> _logger;
|
2021-09-27 12:42:46 +00:00
|
|
|
|
private readonly NexusApi _api;
|
2020-05-09 03:56:06 +00:00
|
|
|
|
|
2021-09-27 12:42:46 +00:00
|
|
|
|
public NexusCache(ILogger<NexusCache> logger, SqlService sql, AppSettings settings, NexusApi api)
|
2020-05-09 03:56:06 +00:00
|
|
|
|
{
|
|
|
|
|
_settings = settings;
|
|
|
|
|
_sql = sql;
|
|
|
|
|
_logger = logger;
|
2021-09-27 12:42:46 +00:00
|
|
|
|
_api = api;
|
2020-05-09 03:56:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Looks up the mod details for a given Gamename/ModId pair. If the entry is not found in the cache it will
|
|
|
|
|
/// be requested from the server (using the caller's Nexus API key if provided).
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="db"></param>
|
|
|
|
|
/// <param name="GameName">The Nexus game name</param>
|
|
|
|
|
/// <param name="ModId">The Nexus mod id</param>
|
|
|
|
|
/// <returns>A Mod Info result</returns>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("{GameName}/mods/{ModId}.json")]
|
|
|
|
|
public async Task<ModInfo> GetModInfo(string GameName, long ModId)
|
|
|
|
|
{
|
2021-09-27 12:42:46 +00:00
|
|
|
|
var game = GameRegistry.GetByNexusName(GameName)!;
|
|
|
|
|
var result = await _sql.GetNexusModInfoString(game.Game, ModId);
|
2020-05-09 03:56:06 +00:00
|
|
|
|
|
|
|
|
|
string method = "CACHED";
|
|
|
|
|
if (result == null)
|
|
|
|
|
{
|
2021-09-27 12:42:46 +00:00
|
|
|
|
var (result2, headers) = await _api.ModInfo(game.NexusName!, ModId);
|
|
|
|
|
result = result2;
|
|
|
|
|
await _sql.AddNexusModInfo(game.Game, ModId, result.UpdatedTime, result);
|
2020-05-09 03:56:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
method = "NOT_CACHED";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Response.Headers.Add("x-cache-result", method);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("{GameName}/mods/{ModId}/files.json")]
|
2021-09-27 12:42:46 +00:00
|
|
|
|
public async Task<ModFiles> GetModFiles(string GameName, long ModId)
|
2020-05-09 03:56:06 +00:00
|
|
|
|
{
|
2020-08-12 04:25:12 +00:00
|
|
|
|
//_logger.Log(LogLevel.Information, $"{GameName} {ModId}");
|
2021-09-27 12:42:46 +00:00
|
|
|
|
var game = GameRegistry.GetByNexusName(GameName)!;
|
|
|
|
|
var result = await _sql.GetModFiles(game!.Game, ModId);
|
2020-05-09 03:56:06 +00:00
|
|
|
|
|
|
|
|
|
string method = "CACHED";
|
|
|
|
|
if (result == null)
|
|
|
|
|
{
|
2021-09-27 12:42:46 +00:00
|
|
|
|
var (result2, _) = await _api.ModFiles(game.NexusName!, ModId);
|
|
|
|
|
result = result2;
|
2020-08-12 04:25:12 +00:00
|
|
|
|
|
2021-09-27 12:42:46 +00:00
|
|
|
|
var date = result.Files.Select(f => f.UploadedTime).OrderByDescending(o => o).FirstOrDefault();
|
2020-05-12 23:19:54 +00:00
|
|
|
|
date = date == default ? DateTime.UtcNow : date;
|
2021-09-27 12:42:46 +00:00
|
|
|
|
await _sql.AddNexusModFiles(game.Game, ModId, date, result);
|
2020-05-09 03:56:06 +00:00
|
|
|
|
|
|
|
|
|
method = "NOT_CACHED";
|
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
|
2020-05-09 03:56:06 +00:00
|
|
|
|
Response.Headers.Add("x-cache-result", method);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2021-06-24 23:01:03 +00:00
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("{GameName}/mods/{ModId}/files/{FileId}.json")]
|
2021-09-27 12:42:46 +00:00
|
|
|
|
public async Task<ActionResult<ModFile>> GetModFile(string GameName, long ModId, long FileId)
|
2021-06-24 23:01:03 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-09-27 12:42:46 +00:00
|
|
|
|
var game = GameRegistry.GetByNexusName(GameName)!;
|
|
|
|
|
var result = await _sql.GetModFile(game.Game, ModId, FileId);
|
2021-06-24 23:01:03 +00:00
|
|
|
|
|
|
|
|
|
string method = "CACHED";
|
|
|
|
|
if (result == null)
|
|
|
|
|
{
|
2021-09-27 12:42:46 +00:00
|
|
|
|
var (result2, _) = await _api.FileInfo(game.NexusName, ModId, FileId);
|
|
|
|
|
result = result2;
|
2021-06-24 23:01:03 +00:00
|
|
|
|
|
2021-09-27 12:42:46 +00:00
|
|
|
|
|
|
|
|
|
var date = result.UploadedTime;
|
2021-06-24 23:01:03 +00:00
|
|
|
|
date = date == default ? DateTime.UtcNow : date;
|
2021-09-27 12:42:46 +00:00
|
|
|
|
await _sql.AddNexusModFile(game.Game, ModId, FileId, date, result);
|
2021-06-24 23:01:03 +00:00
|
|
|
|
|
|
|
|
|
method = "NOT_CACHED";
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-27 12:42:46 +00:00
|
|
|
|
|
2021-06-24 23:01:03 +00:00
|
|
|
|
Response.Headers.Add("x-cache-result", method);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Unable to find mod file {GameName} {ModId}, {FileId}", GameName, ModId, FileId);
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-09 03:56:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|