2021-09-27 12:42:46 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.IO.Compression;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Net.Http.Json;
|
2022-07-13 13:41:39 +00:00
|
|
|
|
using System.Reactive.Subjects;
|
2021-12-17 14:20:40 +00:00
|
|
|
|
using System.Text;
|
2022-09-19 03:15:39 +00:00
|
|
|
|
using System.Text.Json;
|
2021-10-21 03:18:15 +00:00
|
|
|
|
using System.Threading;
|
2021-09-27 12:42:46 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2022-06-20 23:21:04 +00:00
|
|
|
|
using System.Web;
|
2021-09-27 12:42:46 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2022-07-13 21:51:42 +00:00
|
|
|
|
using Octokit;
|
2021-09-27 12:42:46 +00:00
|
|
|
|
using Wabbajack.Common;
|
|
|
|
|
using Wabbajack.DTOs;
|
|
|
|
|
using Wabbajack.DTOs.CDN;
|
2022-01-03 04:44:16 +00:00
|
|
|
|
using Wabbajack.DTOs.Configs;
|
2022-06-20 23:21:04 +00:00
|
|
|
|
using Wabbajack.DTOs.DownloadStates;
|
2021-09-27 12:42:46 +00:00
|
|
|
|
using Wabbajack.DTOs.JsonConverters;
|
2021-10-12 05:19:13 +00:00
|
|
|
|
using Wabbajack.DTOs.Logins;
|
2021-09-27 12:42:46 +00:00
|
|
|
|
using Wabbajack.DTOs.ModListValidation;
|
|
|
|
|
using Wabbajack.DTOs.Validation;
|
2022-06-22 01:38:42 +00:00
|
|
|
|
using Wabbajack.DTOs.Vfs;
|
2021-09-27 12:42:46 +00:00
|
|
|
|
using Wabbajack.Hashing.xxHash64;
|
2021-12-17 14:20:40 +00:00
|
|
|
|
using Wabbajack.Networking.Http;
|
2021-10-12 05:19:13 +00:00
|
|
|
|
using Wabbajack.Networking.Http.Interfaces;
|
2021-09-27 12:42:46 +00:00
|
|
|
|
using Wabbajack.Paths;
|
|
|
|
|
using Wabbajack.Paths.IO;
|
2021-10-12 05:19:13 +00:00
|
|
|
|
using Wabbajack.RateLimiter;
|
2021-09-27 12:42:46 +00:00
|
|
|
|
using YamlDotNet.Serialization;
|
|
|
|
|
using YamlDotNet.Serialization.NamingConventions;
|
2022-07-13 21:51:42 +00:00
|
|
|
|
using FileMode = System.IO.FileMode;
|
2021-09-27 12:42:46 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
namespace Wabbajack.Networking.WabbajackClientApi;
|
|
|
|
|
|
|
|
|
|
public class Client
|
2021-09-27 12:42:46 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public static readonly long UploadedFileBlockSize = (long) 1024 * 1024 * 2;
|
|
|
|
|
|
|
|
|
|
private readonly HttpClient _client;
|
|
|
|
|
private readonly Configuration _configuration;
|
|
|
|
|
private readonly DTOSerializer _dtos;
|
2022-06-22 01:38:42 +00:00
|
|
|
|
private readonly IResource<Client> _hashLimiter;
|
2021-10-23 16:51:17 +00:00
|
|
|
|
private readonly IResource<HttpClient> _limiter;
|
|
|
|
|
private readonly ILogger<Client> _logger;
|
|
|
|
|
|
|
|
|
|
private readonly ITokenProvider<WabbajackApiState> _token;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Client(ILogger<Client> logger, HttpClient client, ITokenProvider<WabbajackApiState> token,
|
|
|
|
|
DTOSerializer dtos,
|
2022-06-22 01:38:42 +00:00
|
|
|
|
IResource<HttpClient> limiter, IResource<Client> hashLimiter, Configuration configuration)
|
2021-09-27 12:42:46 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
_configuration = configuration;
|
|
|
|
|
_token = token;
|
|
|
|
|
_client = client;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_dtos = dtos;
|
|
|
|
|
_limiter = limiter;
|
|
|
|
|
_hashLimiter = hashLimiter;
|
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
|
2021-12-17 23:40:45 +00:00
|
|
|
|
private async ValueTask<HttpRequestMessage> MakeMessage(HttpMethod method, Uri uri, HttpContent? content = null)
|
2021-10-23 16:51:17 +00:00
|
|
|
|
{
|
|
|
|
|
var msg = new HttpRequestMessage(method, uri);
|
|
|
|
|
var key = (await _token.Get())!;
|
|
|
|
|
msg.Headers.Add(_configuration.MetricsKeyHeader, key.MetricsKey);
|
2021-12-17 14:20:40 +00:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(key.AuthorKey))
|
|
|
|
|
msg.Headers.Add(_configuration.AuthorKeyHeader, key.AuthorKey);
|
2021-12-17 23:40:45 +00:00
|
|
|
|
|
|
|
|
|
if (content != null)
|
|
|
|
|
msg.Content = content;
|
2021-10-23 16:51:17 +00:00
|
|
|
|
return msg;
|
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public async Task SendMetric(string action, string subject)
|
|
|
|
|
{
|
|
|
|
|
var msg = await MakeMessage(HttpMethod.Get,
|
|
|
|
|
new Uri($"{_configuration.BuildServerUrl}metrics/{action}/{subject}"));
|
|
|
|
|
await _client.SendAsync(msg);
|
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public async Task<ServerAllowList> LoadDownloadAllowList()
|
|
|
|
|
{
|
|
|
|
|
var str = await _client.GetStringAsync(_configuration.ServerAllowList);
|
|
|
|
|
var d = new DeserializerBuilder()
|
|
|
|
|
.WithNamingConvention(PascalCaseNamingConvention.Instance)
|
|
|
|
|
.Build();
|
|
|
|
|
return d.Deserialize<ServerAllowList>(str);
|
|
|
|
|
}
|
2021-10-12 05:19:13 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public async Task<ServerAllowList> LoadMirrorAllowList()
|
|
|
|
|
{
|
|
|
|
|
var str = await _client.GetStringAsync(_configuration.MirrorAllowList);
|
|
|
|
|
var d = new DeserializerBuilder()
|
|
|
|
|
.WithNamingConvention(PascalCaseNamingConvention.Instance)
|
|
|
|
|
.Build();
|
|
|
|
|
return d.Deserialize<ServerAllowList>(str);
|
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public async Task<Dictionary<Hash, ValidatedArchive>> LoadUpgradedArchives()
|
|
|
|
|
{
|
|
|
|
|
return (await _client.GetFromJsonAsync<ValidatedArchive[]>(_configuration.UpgradedArchives, _dtos.Options))!
|
|
|
|
|
.ToDictionary(d => d.Original.Hash);
|
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public async Task<Archive[]> GetGameArchives(Game game, string version)
|
|
|
|
|
{
|
|
|
|
|
var url = $"https://raw.githubusercontent.com/wabbajack-tools/indexed-game-files/master/{game}/{version}.json";
|
|
|
|
|
return await _client.GetFromJsonAsync<Archive[]>(url, _dtos.Options) ?? Array.Empty<Archive>();
|
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public async Task<Archive[]> GetArchivesForHash(Hash hash)
|
|
|
|
|
{
|
|
|
|
|
var msg = await MakeMessage(HttpMethod.Get,
|
|
|
|
|
new Uri($"{_configuration.BuildServerUrl}mod_files/by_hash/{hash.ToHex()}"));
|
|
|
|
|
return await _client.GetFromJsonAsync<Archive[]>(_limiter, msg, _dtos.Options) ?? Array.Empty<Archive>();
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-18 16:14:39 +00:00
|
|
|
|
public Uri GetMirrorUrl(Hash archiveHash)
|
2021-10-23 16:51:17 +00:00
|
|
|
|
{
|
2021-12-18 16:14:39 +00:00
|
|
|
|
return new Uri($"{_configuration.MirrorServerUrl}{archiveHash.ToHex()}");
|
2021-10-23 16:51:17 +00:00
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public async Task SendModListDefinition(ModList modList)
|
|
|
|
|
{
|
|
|
|
|
await using var fs = new MemoryStream();
|
|
|
|
|
await using var gzip = new GZipStream(fs, CompressionLevel.SmallestSize, true);
|
|
|
|
|
await _dtos.Serialize(modList, gzip);
|
|
|
|
|
await gzip.DisposeAsync();
|
|
|
|
|
fs.Position = 0;
|
|
|
|
|
|
|
|
|
|
var msg = new HttpRequestMessage(HttpMethod.Post,
|
|
|
|
|
$"{_configuration.BuildServerUrl}list_definitions/ingest");
|
|
|
|
|
msg.Headers.Add("x-compressed-body", "gzip");
|
|
|
|
|
msg.Content = new StreamContent(fs);
|
|
|
|
|
await _client.SendAsync(msg);
|
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public async Task<ModListSummary[]> GetListStatuses()
|
|
|
|
|
{
|
|
|
|
|
return await _client.GetFromJsonAsync<ModListSummary[]>(
|
|
|
|
|
"https://raw.githubusercontent.com/wabbajack-tools/mod-lists/master/reports/modListSummary.json",
|
|
|
|
|
_dtos.Options) ?? Array.Empty<ModListSummary>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<ValidatedModList> GetDetailedStatus(string machineURL)
|
|
|
|
|
{
|
|
|
|
|
return (await _client.GetFromJsonAsync<ValidatedModList>(
|
|
|
|
|
$"https://raw.githubusercontent.com/wabbajack-tools/mod-lists/master/reports/{machineURL}/status.json",
|
|
|
|
|
_dtos.Options))!;
|
|
|
|
|
}
|
2022-07-13 21:51:42 +00:00
|
|
|
|
|
2021-12-17 23:40:45 +00:00
|
|
|
|
IEnumerable<PartDefinition> Blocks(long size)
|
|
|
|
|
{
|
|
|
|
|
for (long block = 0; block * UploadedFileBlockSize < size; block++)
|
|
|
|
|
yield return new PartDefinition
|
|
|
|
|
{
|
|
|
|
|
Index = block,
|
|
|
|
|
Size = Math.Min(UploadedFileBlockSize, size - block * UploadedFileBlockSize),
|
|
|
|
|
Offset = block * UploadedFileBlockSize
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-27 12:42:46 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public async Task<FileDefinition> GenerateFileDefinition(AbsolutePath path)
|
|
|
|
|
{
|
2021-09-27 12:42:46 +00:00
|
|
|
|
|
2021-12-17 23:40:45 +00:00
|
|
|
|
var parts = Blocks(path.Size()).ToArray();
|
2021-10-23 16:51:17 +00:00
|
|
|
|
var definition = new FileDefinition
|
2021-09-27 12:42:46 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
OriginalFileName = path.FileName,
|
|
|
|
|
Size = path.Size(),
|
|
|
|
|
Hash = await path.Hash(),
|
|
|
|
|
Parts = await parts.PMapAll(async part =>
|
2021-09-27 12:42:46 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
var buffer = new byte[part.Size];
|
|
|
|
|
using var job = await _hashLimiter.Begin("Hashing part", part.Size, CancellationToken.None);
|
|
|
|
|
await using (var fs = path.Open(FileMode.Open, FileAccess.Read, FileShare.Read))
|
2021-09-27 12:42:46 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
fs.Position = part.Offset;
|
|
|
|
|
await fs.ReadAsync(buffer);
|
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
part.Hash = await buffer.Hash(job);
|
|
|
|
|
return part;
|
|
|
|
|
}).ToArray()
|
|
|
|
|
};
|
2021-09-27 12:42:46 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
return definition;
|
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
|
2022-03-30 03:39:48 +00:00
|
|
|
|
public async Task<ModlistMetadata[]> LoadLists()
|
2021-10-23 16:51:17 +00:00
|
|
|
|
{
|
2022-05-15 05:19:20 +00:00
|
|
|
|
var repos = LoadRepositories();
|
|
|
|
|
var featured = await LoadFeaturedLists();
|
2022-03-30 03:39:48 +00:00
|
|
|
|
|
2022-05-15 05:19:20 +00:00
|
|
|
|
return await (await repos).PMapAll(async url =>
|
2022-09-19 03:15:39 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return (await _client.GetFromJsonAsync<ModlistMetadata[]>(_limiter,
|
|
|
|
|
new HttpRequestMessage(HttpMethod.Get, url.Value),
|
|
|
|
|
_dtos.Options))!.Select(meta =>
|
|
|
|
|
{
|
|
|
|
|
meta.RepositoryName = url.Key;
|
|
|
|
|
meta.Official = (meta.RepositoryName == "wj-featured" ||
|
|
|
|
|
featured.Contains(meta.NamespacedName));
|
|
|
|
|
return meta;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch (JsonException ex)
|
2022-07-13 21:51:42 +00:00
|
|
|
|
{
|
2022-09-19 03:15:39 +00:00
|
|
|
|
_logger.LogError(ex, "While loading {List} from {Url}", url.Key, url.Value);
|
|
|
|
|
return Enumerable.Empty<ModlistMetadata>();
|
|
|
|
|
}
|
|
|
|
|
})
|
2021-10-23 16:51:17 +00:00
|
|
|
|
.SelectMany(x => x)
|
|
|
|
|
.ToArray();
|
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
|
2022-05-15 05:19:20 +00:00
|
|
|
|
private async Task<HashSet<string>> LoadFeaturedLists()
|
|
|
|
|
{
|
|
|
|
|
var data = await _client.GetFromJsonAsync<string[]>(_limiter,
|
|
|
|
|
new HttpRequestMessage(HttpMethod.Get,
|
2022-07-13 21:51:42 +00:00
|
|
|
|
"https://raw.githubusercontent.com/wabbajack-tools/mod-lists/master/featured_lists.json"),
|
|
|
|
|
_dtos.Options);
|
2022-05-15 05:19:20 +00:00
|
|
|
|
return data!.ToHashSet(StringComparer.CurrentCultureIgnoreCase);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-30 03:39:48 +00:00
|
|
|
|
public async Task<Dictionary<string, Uri>> LoadRepositories()
|
|
|
|
|
{
|
|
|
|
|
var repositories = await _client.GetFromJsonAsync<Dictionary<string, Uri>>(_limiter,
|
|
|
|
|
new HttpRequestMessage(HttpMethod.Get,
|
|
|
|
|
"https://raw.githubusercontent.com/wabbajack-tools/mod-lists/master/repositories.json"), _dtos.Options);
|
|
|
|
|
return repositories!;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public Uri GetPatchUrl(Hash upgradeHash, Hash archiveHash)
|
|
|
|
|
{
|
|
|
|
|
return new Uri($"{_configuration.PatchBaseAddress}{upgradeHash.ToHex()}_{archiveHash.ToHex()}");
|
2021-09-27 12:42:46 +00:00
|
|
|
|
}
|
2021-12-17 14:20:40 +00:00
|
|
|
|
|
2021-12-17 23:40:45 +00:00
|
|
|
|
public async Task<ValidatedArchive> UploadPatch(ValidatedArchive validated, Stream data)
|
2021-12-17 14:20:40 +00:00
|
|
|
|
{
|
2021-12-17 23:40:45 +00:00
|
|
|
|
_logger.LogInformation("Uploading Patch {From} {To}", validated.Original.Hash, validated.PatchedFrom!.Hash);
|
|
|
|
|
var name = $"{validated.Original.Hash.ToHex()}_{validated.PatchedFrom.Hash.ToHex()}";
|
2022-07-13 21:51:42 +00:00
|
|
|
|
|
2021-12-17 23:40:45 +00:00
|
|
|
|
var blocks = Blocks(data.Length).ToArray();
|
|
|
|
|
foreach (var block in blocks)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Uploading Block {Idx}/{Max}", block.Index, blocks.Length);
|
|
|
|
|
data.Position = block.Offset;
|
|
|
|
|
var blockData = new byte[block.Size];
|
|
|
|
|
await data.ReadAsync(blockData);
|
|
|
|
|
var hash = await blockData.Hash();
|
|
|
|
|
|
|
|
|
|
using var result = await _client.SendAsync(await MakeMessage(HttpMethod.Post,
|
|
|
|
|
new Uri($"{_configuration.BuildServerUrl}patches?name={name}&start={block.Offset}"),
|
|
|
|
|
new ByteArrayContent(blockData)));
|
|
|
|
|
if (!result.IsSuccessStatusCode)
|
|
|
|
|
throw new HttpException(result);
|
|
|
|
|
|
|
|
|
|
var resultHash = Hash.FromHex(await result.Content.ReadAsStringAsync());
|
|
|
|
|
if (resultHash != hash)
|
|
|
|
|
throw new Exception($"Result Hash does not match expected hash {hash} vs {resultHash}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
validated.PatchUrl = new Uri($"https://patches.wabbajack.org/{name}");
|
|
|
|
|
|
|
|
|
|
return validated;
|
2021-12-17 14:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task AddForceHealedPatch(ValidatedArchive validated)
|
|
|
|
|
{
|
2022-07-13 21:51:42 +00:00
|
|
|
|
var oldData =
|
|
|
|
|
await GetGithubFile<ValidatedArchive[]>("wabbajack-tools", "mod-lists", "configs/forced_healing.json");
|
2021-12-17 14:20:40 +00:00
|
|
|
|
var content = oldData.Content.Append(validated).ToArray();
|
|
|
|
|
await UpdateGitHubFile("wabbajack-tools", "mod-lists", "configs/forced_healing.json", content, oldData.Sha);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task UpdateGitHubFile<T>(string owner, string repo, string path, T content, string oldSha)
|
|
|
|
|
{
|
2021-12-18 00:14:45 +00:00
|
|
|
|
var json = _dtos.Serialize(content, writeIndented: true);
|
2021-12-17 14:20:40 +00:00
|
|
|
|
var msg = await MakeMessage(HttpMethod.Post,
|
2021-12-18 00:14:45 +00:00
|
|
|
|
new Uri($"{_configuration.BuildServerUrl}github/?owner={owner}&repo={repo}&path={path}&oldSha={oldSha}"));
|
2021-12-17 14:20:40 +00:00
|
|
|
|
|
|
|
|
|
msg.Content = new StringContent(json, Encoding.UTF8, "application/json");
|
|
|
|
|
using var result = await _client.SendAsync(msg);
|
|
|
|
|
if (!result.IsSuccessStatusCode)
|
|
|
|
|
throw new HttpException(result);
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-13 21:51:42 +00:00
|
|
|
|
private async Task<(string Sha, T Content)> GetGithubFile<T>(string owner, string repo, string path,
|
|
|
|
|
CancellationToken? token = null)
|
2021-12-17 14:20:40 +00:00
|
|
|
|
{
|
2021-12-18 16:14:39 +00:00
|
|
|
|
token ??= CancellationToken.None;
|
2022-07-13 21:51:42 +00:00
|
|
|
|
|
2021-12-17 14:20:40 +00:00
|
|
|
|
var msg = await MakeMessage(HttpMethod.Get,
|
2021-12-18 00:14:45 +00:00
|
|
|
|
new Uri($"{_configuration.BuildServerUrl}github/?owner={owner}&repo={repo}&path={path}"));
|
2021-12-18 16:14:39 +00:00
|
|
|
|
using var oldData = await _client.SendAsync(msg, token.Value);
|
2021-12-17 14:20:40 +00:00
|
|
|
|
if (!oldData.IsSuccessStatusCode)
|
|
|
|
|
throw new HttpException(oldData);
|
|
|
|
|
|
|
|
|
|
var sha = oldData.Headers.GetValues(_configuration.ResponseShaHeader).First();
|
2021-12-18 16:14:39 +00:00
|
|
|
|
return (sha, (await oldData.Content.ReadFromJsonAsync<T>(_dtos.Options, token.Value))!);
|
2021-12-17 14:20:40 +00:00
|
|
|
|
}
|
2021-12-17 23:40:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task UploadMirror(FileDefinition definition, AbsolutePath file)
|
|
|
|
|
{
|
|
|
|
|
var hashAsHex = definition.Hash.ToHex();
|
|
|
|
|
_logger.LogInformation("Starting upload of {Name} ({Hash})", file.FileName, hashAsHex);
|
2022-07-13 21:51:42 +00:00
|
|
|
|
|
2021-12-17 23:40:45 +00:00
|
|
|
|
using var result = await _client.SendAsync(await MakeMessage(HttpMethod.Put,
|
|
|
|
|
new Uri($"{_configuration.BuildServerUrl}mirrored_files/create/{hashAsHex}"),
|
|
|
|
|
new StringContent(_dtos.Serialize(definition), Encoding.UTF8, "application/json")));
|
|
|
|
|
if (!result.IsSuccessStatusCode)
|
|
|
|
|
throw new HttpException(result);
|
2022-07-13 21:51:42 +00:00
|
|
|
|
|
2021-12-17 23:40:45 +00:00
|
|
|
|
_logger.LogInformation("Uploading Parts");
|
|
|
|
|
|
|
|
|
|
await using var dataIn = file.Open(FileMode.Open);
|
|
|
|
|
|
|
|
|
|
foreach (var (part, idx) in definition.Parts.Select((part, idx) => (part, idx)))
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Uploading Part {Part}/{Max}", idx, definition.Parts.Length);
|
|
|
|
|
|
|
|
|
|
dataIn.Position = part.Offset;
|
|
|
|
|
var data = new byte[part.Size];
|
|
|
|
|
await dataIn.ReadAsync(data);
|
|
|
|
|
|
|
|
|
|
using var partResult = await _client.SendAsync(await MakeMessage(HttpMethod.Put,
|
|
|
|
|
new Uri($"{_configuration.BuildServerUrl}mirrored_files/{hashAsHex}/part/{idx}"),
|
|
|
|
|
new ByteArrayContent(data)));
|
2022-07-13 21:51:42 +00:00
|
|
|
|
|
2021-12-17 23:40:45 +00:00
|
|
|
|
if (!partResult.IsSuccessStatusCode)
|
|
|
|
|
throw new HttpException(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using var finalResult = await _client.SendAsync(await MakeMessage(HttpMethod.Put,
|
|
|
|
|
new Uri($"{_configuration.BuildServerUrl}mirrored_files/{hashAsHex}/finish")));
|
2022-07-13 21:51:42 +00:00
|
|
|
|
|
2021-12-17 23:40:45 +00:00
|
|
|
|
if (!finalResult.IsSuccessStatusCode)
|
|
|
|
|
throw new HttpException(result);
|
2021-12-18 16:14:39 +00:00
|
|
|
|
}
|
2021-12-17 23:40:45 +00:00
|
|
|
|
|
2021-12-18 16:14:39 +00:00
|
|
|
|
public async Task<FileDefinition[]> GetAllMirroredFileDefinitions(CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
return (await _client.GetFromJsonAsync<FileDefinition[]>($"{_configuration.BuildServerUrl}mirrored_files",
|
|
|
|
|
_dtos.Options, token))!;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<ValidatedArchive[]> GetAllPatches(CancellationToken token)
|
|
|
|
|
{
|
2022-07-13 21:51:42 +00:00
|
|
|
|
return (await _client.GetFromJsonAsync<ValidatedArchive[]>(
|
|
|
|
|
"https://raw.githubusercontent.com/wabbajack-tools/mod-lists/master/configs/forced_healing.json",
|
|
|
|
|
_dtos.Options, token))!;
|
2021-12-17 23:40:45 +00:00
|
|
|
|
}
|
2021-12-19 22:20:24 +00:00
|
|
|
|
|
|
|
|
|
public async Task DeleteMirror(Hash hash)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Deleting mirror of {Hash}", hash);
|
2022-07-13 21:51:42 +00:00
|
|
|
|
var msg = await MakeMessage(HttpMethod.Delete,
|
|
|
|
|
new Uri($"{_configuration.BuildServerUrl}mirrored_files/{hash.ToHex()}"));
|
2021-12-19 22:20:24 +00:00
|
|
|
|
var result = await _client.SendAsync(msg);
|
|
|
|
|
if (!result.IsSuccessStatusCode)
|
|
|
|
|
throw new HttpException(result);
|
|
|
|
|
}
|
2021-12-27 05:53:39 +00:00
|
|
|
|
|
2022-01-05 03:27:39 +00:00
|
|
|
|
|
2022-07-13 13:41:39 +00:00
|
|
|
|
public async Task<(IObservable<(Percent PercentDone, string Message)> Progress, Task<Uri> Task)> UploadAuthorFile(
|
|
|
|
|
AbsolutePath path)
|
2021-12-27 05:53:39 +00:00
|
|
|
|
{
|
2022-07-13 13:41:39 +00:00
|
|
|
|
var apiKey = (await _token.Get())!.AuthorKey;
|
|
|
|
|
var report = new Subject<(Percent PercentDone, string Message)>();
|
|
|
|
|
|
|
|
|
|
var tsk = Task.Run<Uri>(async () =>
|
|
|
|
|
{
|
|
|
|
|
report.OnNext((Percent.Zero, "Generating File Definition"));
|
|
|
|
|
var definition = await GenerateFileDefinition(path);
|
|
|
|
|
|
|
|
|
|
report.OnNext((Percent.Zero, "Creating file upload"));
|
|
|
|
|
await CircuitBreaker.WithAutoRetryAllAsync(_logger, async () =>
|
|
|
|
|
{
|
2022-07-13 21:51:42 +00:00
|
|
|
|
var msg = await MakeMessage(HttpMethod.Put,
|
|
|
|
|
new Uri($"{_configuration.BuildServerUrl}authored_files/create"));
|
2022-07-13 13:41:39 +00:00
|
|
|
|
msg.Content = new StringContent(_dtos.Serialize(definition));
|
|
|
|
|
using var result = await _client.SendAsync(msg);
|
|
|
|
|
HttpException.ThrowOnFailure(result);
|
|
|
|
|
definition.ServerAssignedUniqueId = await result.Content.ReadAsStringAsync();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
report.OnNext((Percent.Zero, "Starting part uploads"));
|
|
|
|
|
await definition.Parts.PDoAll(_limiter, async part =>
|
|
|
|
|
{
|
|
|
|
|
report.OnNext((Percent.FactoryPutInRange(part.Index, definition.Parts.Length),
|
|
|
|
|
$"Uploading Part ({part.Index}/{definition.Parts.Length})"));
|
|
|
|
|
var buffer = new byte[part.Size];
|
|
|
|
|
await using (var fs = path.Open(FileMode.Open, FileAccess.Read, FileShare.Read))
|
|
|
|
|
{
|
|
|
|
|
fs.Position = part.Offset;
|
|
|
|
|
await fs.ReadAsync(buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await CircuitBreaker.WithAutoRetryAllAsync(_logger, async () =>
|
|
|
|
|
{
|
|
|
|
|
var msg = await MakeMessage(HttpMethod.Put,
|
|
|
|
|
new Uri(
|
|
|
|
|
$"{_configuration.BuildServerUrl}authored_files/{definition.ServerAssignedUniqueId}/part/{part.Index}"));
|
|
|
|
|
msg.Content = new ByteArrayContent(buffer);
|
|
|
|
|
using var putResult = await _client.SendAsync(msg);
|
|
|
|
|
HttpException.ThrowOnFailure(putResult);
|
|
|
|
|
var hash = Hash.FromBase64(await putResult.Content.ReadAsStringAsync());
|
|
|
|
|
if (hash != part.Hash)
|
|
|
|
|
throw new InvalidDataException("Hashes don't match");
|
|
|
|
|
return hash;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
report.OnNext((Percent.Zero, "Finalizing upload"));
|
|
|
|
|
return await CircuitBreaker.WithAutoRetryAllAsync(_logger, async () =>
|
|
|
|
|
{
|
|
|
|
|
var msg = await MakeMessage(HttpMethod.Put,
|
2022-07-13 21:51:42 +00:00
|
|
|
|
new Uri(
|
|
|
|
|
$"{_configuration.BuildServerUrl}authored_files/{definition.ServerAssignedUniqueId}/finish"));
|
2022-07-13 13:41:39 +00:00
|
|
|
|
msg.Content = new StringContent(_dtos.Serialize(definition));
|
|
|
|
|
using var result = await _client.SendAsync(msg);
|
|
|
|
|
HttpException.ThrowOnFailure(result);
|
|
|
|
|
report.OnNext((Percent.One, "Finished"));
|
|
|
|
|
return new Uri($"https://authored-files.wabbajack.org/{definition.MungedName}");
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
return (report, tsk);
|
2021-12-27 05:53:39 +00:00
|
|
|
|
}
|
2022-07-13 21:51:42 +00:00
|
|
|
|
|
2022-01-03 04:44:16 +00:00
|
|
|
|
public async Task<ForcedRemoval[]> GetForcedRemovals(CancellationToken token)
|
|
|
|
|
{
|
2022-07-13 21:51:42 +00:00
|
|
|
|
return (await _client.GetFromJsonAsync<ForcedRemoval[]>(
|
|
|
|
|
"https://raw.githubusercontent.com/wabbajack-tools/mod-lists/master/configs/forced_removal.json",
|
|
|
|
|
_dtos.Options, token))!;
|
2022-01-03 04:44:16 +00:00
|
|
|
|
}
|
2022-01-09 05:40:23 +00:00
|
|
|
|
|
|
|
|
|
public async Task<SteamManifest[]> GetSteamManifests(Game game, string version)
|
|
|
|
|
{
|
2022-07-13 21:51:42 +00:00
|
|
|
|
var url =
|
|
|
|
|
$"https://raw.githubusercontent.com/wabbajack-tools/indexed-game-files/master/{game}/{version}_steam_manifests.json";
|
2022-01-09 05:40:23 +00:00
|
|
|
|
return await _client.GetFromJsonAsync<SteamManifest[]>(url, _dtos.Options) ?? Array.Empty<SteamManifest>();
|
|
|
|
|
}
|
2022-07-13 21:51:42 +00:00
|
|
|
|
|
2022-06-20 23:21:04 +00:00
|
|
|
|
public async Task<bool> ProxyHas(Uri uri)
|
|
|
|
|
{
|
|
|
|
|
var newUri = new Uri($"{_configuration.BuildServerUrl}proxy?uri={HttpUtility.UrlEncode(uri.ToString())}");
|
|
|
|
|
var msg = new HttpRequestMessage(HttpMethod.Head, newUri);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var result = await _client.SendAsync(msg);
|
|
|
|
|
return result.IsSuccessStatusCode;
|
|
|
|
|
}
|
2022-10-07 22:57:12 +00:00
|
|
|
|
catch (Exception)
|
2022-06-20 23:21:04 +00:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-08 03:48:13 +00:00
|
|
|
|
|
2022-06-20 23:21:04 +00:00
|
|
|
|
public async ValueTask<Uri?> MakeProxyUrl(Archive archive, Uri uri)
|
2022-06-08 03:48:13 +00:00
|
|
|
|
{
|
2022-06-20 23:21:04 +00:00
|
|
|
|
if (archive.State is Manual && !await ProxyHas(uri))
|
|
|
|
|
return null;
|
2022-07-13 21:51:42 +00:00
|
|
|
|
|
|
|
|
|
return new Uri(
|
|
|
|
|
$"{_configuration.BuildServerUrl}proxy?name={archive.Name}&hash={archive.Hash.ToHex()}&uri={HttpUtility.UrlEncode(uri.ToString())}");
|
2022-06-08 03:48:13 +00:00
|
|
|
|
}
|
2022-06-22 01:38:42 +00:00
|
|
|
|
|
|
|
|
|
public async Task<IndexedVirtualFile?> GetCesiVfsEntry(Hash hash, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var msg = await MakeMessage(HttpMethod.Get, new Uri($"{_configuration.BuildServerUrl}cesi/vfs/{hash.ToHex()}"));
|
|
|
|
|
using var response = await _client.SendAsync(msg, token);
|
|
|
|
|
HttpException.ThrowOnFailure(response);
|
|
|
|
|
return await _dtos.DeserializeAsync<IndexedVirtualFile>(await response.Content.ReadAsStreamAsync(token), token);
|
|
|
|
|
}
|
2022-07-13 21:22:05 +00:00
|
|
|
|
|
|
|
|
|
public async Task<IReadOnlyList<string>> GetMyModlists(CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var msg = await MakeMessage(HttpMethod.Get, new Uri($"{_configuration.BuildServerUrl}author_controls/lists"));
|
|
|
|
|
using var response = await _client.SendAsync(msg, token);
|
|
|
|
|
HttpException.ThrowOnFailure(response);
|
|
|
|
|
return (await _dtos.DeserializeAsync<string[]>(await response.Content.ReadAsStreamAsync(token), token))!;
|
|
|
|
|
}
|
2022-07-13 21:51:42 +00:00
|
|
|
|
|
|
|
|
|
public async Task PublishModlist(string namespacedName, Version version, AbsolutePath modList, DownloadMetadata metadata)
|
|
|
|
|
{
|
|
|
|
|
var pair = namespacedName.Split("/");
|
|
|
|
|
var wjRepoName = pair[0];
|
|
|
|
|
var machineUrl = pair[1];
|
|
|
|
|
|
|
|
|
|
var repoUrl = (await LoadRepositories())[wjRepoName];
|
|
|
|
|
|
|
|
|
|
var decomposed = repoUrl.LocalPath.Split("/");
|
|
|
|
|
var owner = decomposed[1];
|
|
|
|
|
var repoName = decomposed[2];
|
|
|
|
|
var path = string.Join("/", decomposed[4..]);
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Uploading modlist {MachineUrl}", namespacedName);
|
|
|
|
|
|
|
|
|
|
var (progress, uploadTask) = await UploadAuthorFile(modList);
|
|
|
|
|
progress.Subscribe(x => _logger.LogInformation(x.Message));
|
|
|
|
|
var downloadUrl = await uploadTask;
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Publishing modlist {MachineUrl}", namespacedName);
|
|
|
|
|
|
|
|
|
|
var creds = new Credentials((await _token.Get())!.AuthorKey);
|
|
|
|
|
var ghClient = new GitHubClient(new ProductHeaderValue("wabbajack")) {Credentials = creds};
|
|
|
|
|
|
|
|
|
|
var oldData =
|
|
|
|
|
(await ghClient.Repository.Content.GetAllContents(owner, repoName, path))
|
|
|
|
|
.First();
|
|
|
|
|
var oldContent = _dtos.Deserialize<ModlistMetadata[]>(oldData.Content);
|
|
|
|
|
var list = oldContent.First(c => c.Links.MachineURL == machineUrl);
|
|
|
|
|
list.Version = version;
|
|
|
|
|
list.DownloadMetadata = metadata;
|
|
|
|
|
list.Links.Download = downloadUrl.ToString();
|
|
|
|
|
list.DateUpdated = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var newContent = _dtos.Serialize(oldContent, true);
|
|
|
|
|
// the website requires all names be in lowercase;
|
|
|
|
|
newContent = GameRegistry.Games.Keys.Aggregate(newContent,
|
|
|
|
|
(current, g) => current.Replace($"\"game\": \"{g}\",", $"\"game\": \"{g.ToString().ToLower()}\","));
|
|
|
|
|
|
|
|
|
|
var updateRequest = new UpdateFileRequest($"New release of {machineUrl}", newContent, oldData.Sha);
|
|
|
|
|
await ghClient.Repository.Content.UpdateFile(owner, repoName, path, updateRequest);
|
|
|
|
|
}
|
|
|
|
|
}
|