Can infer mod metas again

This commit is contained in:
Timothy Baldridge 2020-06-21 16:03:54 -06:00
parent 2ee3939afb
commit bbced4f99f
7 changed files with 106 additions and 29 deletions

View File

@ -96,11 +96,11 @@ namespace Wabbajack.Common.Http
{
if (ex is HttpException http)
{
if (http.Code != 503) throw;
if (http.Code != 503 && http.Code != 521) throw;
retries++;
var ms = Utils.NextRandom(100, 1000);
Utils.Log($"Got a 503 from {msg.RequestUri} retrying in {ms}ms");
Utils.Log($"Got a {http.Code} from {msg.RequestUri} retrying in {ms}ms");
await Task.Delay(ms);
msg = CloneMessage(msg);

View File

@ -85,25 +85,6 @@ using Wabbajack.Lib.Downloaders;
throw ex;
}
/// <summary>
/// Given an archive hash, search the Wabbajack server for a matching .ini file
/// </summary>
/// <param name="hash"></param>
/// <returns></returns>
public static async Task<string?> GetModIni(Hash hash)
{
var client = new Common.Http.Client();
try
{
return await client.GetStringAsync(
$"{Consts.WabbajackBuildServerUri}indexed_files/{hash.ToHex()}/meta.ini");
}
catch (HttpException)
{
return null;
}
}
public class NexusCacheStats
{
public long CachedCount { get; set; }
@ -139,5 +120,17 @@ using Wabbajack.Lib.Downloaders;
})
.ToArray();
}
public static async Task<AbstractDownloadState?> InferDownloadState(Hash hash)
{
var client = await GetClient();
var results = await client.GetJsonAsync<Archive[]>($"{Consts.WabbajackBuildServerUri}mod_files/by_hash/{hash.ToHex()}");
foreach (var result in results)
{
if (await result.State.Verify(result)) return result.State;
}
return null;
}
}
}

View File

@ -412,12 +412,12 @@ namespace Wabbajack.Lib
await to_find.PMap(Queue, async f =>
{
var vf = VFS.Index.ByRootPath[f];
var client = new Common.Http.Client();
using var response =
await client.GetAsync(
$"http://build.wabbajack.org/indexed_files/{vf.Hash.ToHex()}/meta.ini", errorsAsExceptions: false);
if (!response.IsSuccessStatusCode)
var meta = await ClientAPI.InferDownloadState(vf.Hash);
if (meta == null)
{
await vf.AbsoluteName.WithExtension(Consts.MetaFileExtension).WriteAllLinesAsync(
"[General]",
@ -425,9 +425,8 @@ namespace Wabbajack.Lib
return;
}
var iniData = await response.Content.ReadAsStringAsync();
Utils.Log($"Inferred .meta for {vf.FullPath.FileName}, writing to disk");
await vf.AbsoluteName.WithExtension(Consts.MetaFileExtension).WriteAllTextAsync(iniData);
await vf.AbsoluteName.WithExtension(Consts.MetaFileExtension).WriteAllTextAsync(meta.GetMetaIniString());
});
}

View File

@ -37,7 +37,6 @@ namespace Wabbajack.BuildServer.Test
var allStates = await service.GetAllArchiveDownloads();
Assert.Contains(state.PrimaryKeyString, allStates.Select(s => s.PrimaryKeyString));
}
private async Task ClearDownloaderQueue()

View File

@ -0,0 +1,44 @@
using System;
using System.Threading.Tasks;
using Wabbajack.BuildServer.Test;
using Wabbajack.Common;
using Wabbajack.Lib;
using Wabbajack.Lib.Downloaders;
using Wabbajack.Server.DataLayer;
using Xunit;
using Xunit.Abstractions;
namespace Wabbajack.Server.Test
{
public class ModFileTests : ABuildServerSystemTest
{
public ModFileTests(ITestOutputHelper output, SingletonAdaptor<BuildServerFixture> fixture) : base(output, fixture)
{
}
[Fact]
public async Task CanGetDownloadStates()
{
var sql = Fixture.GetService<SqlService>();
var archive =
new Archive(new WabbajackCDNDownloader.State(new Uri(
"https://wabbajack.b-cdn.net/WABBAJACK_TEST_FILE.zip_a1a3e961-5c0b-4ccf-84b4-7aa437d9640d")))
{
Size = 20, Hash = Hash.FromBase64("eSIyd+KOG3s=")
};
await sql.EnqueueDownload(archive);
var dld = await sql.GetNextPendingDownload();
await dld.Finish(sql);
var state = await ClientAPI.InferDownloadState(archive.Hash);
Assert.Equal(archive.State.GetMetaIniString(), state!.GetMetaIniString());
}
}
}

View File

@ -0,0 +1,31 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Wabbajack.Common;
using Wabbajack.Server.DataLayer;
namespace Wabbajack.BuildServer.Controllers
{
[Authorize(Roles = "User")]
[ApiController]
[Route("/mod_files")]
public class ModFiles : ControllerBase
{
private SqlService _sql;
private ILogger<ModFiles> _logger;
public ModFiles(ILogger<ModFiles> logger, SqlService sql)
{
_logger = logger;
_sql = sql;
}
[HttpGet("by_hash/{hashAsHex}")]
public async Task<IActionResult> GetByHash(string hashAsHex)
{
var files = await _sql.ResolveDownloadStatesByHash(Hash.FromHex(hashAsHex));
return Ok(files.ToJson());
}
}
}

View File

@ -210,5 +210,16 @@ namespace Wabbajack.Server.DataLayer
return files;
}
public async Task<Archive[]> ResolveDownloadStatesByHash(Hash hash)
{
await using var conn = await Open();
var files = (await conn.QueryAsync<(long, Hash, AbstractDownloadState)>(
@"SELECT Size, Hash, DownloadState from dbo.ArchiveDownloads WHERE Hash = @Hash AND IsFailed = 0 AND DownloadFinished IS NOT NULL ORDER BY DownloadFinished DESC",
new {Hash = hash})
);
return files.Select(e =>
new Archive(e.Item3) {Size = e.Item1, Hash = e.Item2}
).ToArray();
}
}
}