wabbajack/Wabbajack.Server/Controllers/AuthorControls.cs

131 lines
4.5 KiB
C#
Raw Normal View History

using System;
2021-07-19 22:15:27 +00:00
using System.Collections.Generic;
using System.Linq;
using System.Net;
2021-09-27 12:42:46 +00:00
using System.Net.Http;
using System.Security.Claims;
using System.Threading.Tasks;
2021-05-28 23:40:58 +00:00
using FluentFTP.Helpers;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Nettle;
using Wabbajack.Common;
2021-09-27 12:42:46 +00:00
using Wabbajack.DTOs.GitHub;
using Wabbajack.DTOs.JsonConverters;
using Wabbajack.Networking.GitHub;
using Wabbajack.Paths.IO;
using Wabbajack.Server.DataLayer;
2021-07-20 05:08:12 +00:00
using Wabbajack.Server.Services;
namespace Wabbajack.BuildServer.Controllers
{
[Authorize(Roles="Author")]
[Route("/author_controls")]
public class AuthorControls : ControllerBase
{
private ILogger<AuthorControls> _logger;
private SqlService _sql;
2021-07-20 05:08:12 +00:00
private readonly QuickSync _quickSync;
2021-09-27 12:42:46 +00:00
private readonly HttpClient _client;
private readonly AppSettings _settings;
private readonly DTOSerializer _dtos;
private readonly Client _gitHubClient;
2021-09-27 12:42:46 +00:00
public AuthorControls(ILogger<AuthorControls> logger, SqlService sql, QuickSync quickSync, HttpClient client, AppSettings settings, DTOSerializer dtos,
Client gitHubClient)
{
_logger = logger;
_sql = sql;
2021-07-20 05:08:12 +00:00
_quickSync = quickSync;
2021-09-27 12:42:46 +00:00
_client = client;
_settings = settings;
_dtos = dtos;
_gitHubClient = gitHubClient;
}
[Route("login/{authorKey}")]
[AllowAnonymous]
public async Task<IActionResult> Login(string authorKey)
{
Response.Cookies.Append(ApiKeyAuthenticationHandler.ApiKeyHeaderName, authorKey);
2021-09-27 12:42:46 +00:00
return Redirect($"{_settings.WabbajackBuildServerUri}author_controls/home");
}
2021-07-19 22:15:27 +00:00
[Route("lists")]
[HttpGet]
public async Task<IActionResult> AuthorLists()
{
var user = User.FindFirstValue(ClaimTypes.Name);
List<string> lists = new();
2021-09-27 12:42:46 +00:00
foreach (var file in Enum.GetValues<List>())
2021-07-19 22:15:27 +00:00
{
2021-09-27 12:42:46 +00:00
lists.AddRange((await _gitHubClient.GetData(file)).Lists.Where(l => l.Maintainers.Contains(user))
2021-07-19 22:15:27 +00:00
.Select(lst => lst.Links.MachineURL));
}
return Ok(lists);
}
2021-07-20 05:08:12 +00:00
[Route("lists/download_metadata")]
2021-07-19 22:15:27 +00:00
[HttpPost]
2021-07-20 05:08:12 +00:00
public async Task<IActionResult> PostDownloadMetadata()
2021-07-19 22:15:27 +00:00
{
var user = User.FindFirstValue(ClaimTypes.Name);
2021-09-27 12:42:46 +00:00
var data = await _dtos.DeserializeAsync<UpdateRequest>(Request.Body);
2021-07-19 22:15:27 +00:00
try
{
2021-09-27 12:42:46 +00:00
await _gitHubClient.UpdateList(user, data);
2021-07-20 05:08:12 +00:00
await _quickSync.Notify<ModListDownloader>();
2021-07-19 22:15:27 +00:00
}
catch (Exception ex)
{
2021-07-20 05:08:12 +00:00
_logger.LogError(ex, "During posting of download_metadata");
2021-07-19 22:15:27 +00:00
return BadRequest(ex);
}
return Ok(data);
}
2021-04-13 01:17:03 +00:00
private static async Task<string> HomePageTemplate(object o)
{
2021-09-27 12:42:46 +00:00
var data = await KnownFolders.EntryPoint.Combine(@"Controllers\Templates\AuthorControls.html")
2021-04-13 01:17:03 +00:00
.ReadAllTextAsync();
var func = NettleEngine.GetCompiler().Compile(data);
return func(o);
}
[Route("home")]
[Authorize("")]
public async Task<IActionResult> HomePage()
{
var user = User.FindFirstValue(ClaimTypes.Name);
var files = (await _sql.AllAuthoredFiles())
.Where(af => af.Author == user)
.Select(af => new
{
Size = af.Size.FileSizeToString(),
OriginalSize = af.Size,
Name = af.OriginalFileName,
MangledName = af.MungedName,
UploadedDate = af.LastTouched
})
.OrderBy(f => f.Name)
.ThenBy(f => f.UploadedDate)
.ToList();
var result = HomePageTemplate(new
{
User = user,
TotalUsage = files.Select(f => f.OriginalSize).Sum().ToFileSizeString(),
2021-09-27 12:42:46 +00:00
WabbajackFiles = files.Where(f => f.Name.EndsWith(Ext.Wabbajack.ToString())),
OtherFiles = files.Where(f => !f.Name.EndsWith(Ext.Wabbajack.ToString()))
});
return new ContentResult {
ContentType = "text/html",
StatusCode = (int)HttpStatusCode.OK,
2021-04-13 01:17:03 +00:00
Content = await result};
}
}
}