Files
ClubPenguinServer/MareSynchronosServer/MareSynchronosStaticFilesServer/Controllers/RequestController.cs
rootdarkarchon 42b15cb6b7 Add Server-Side Download Queue (#21)
* test add queueing to file service

* further adjustments to download queueing

* add check for whether the request is still in the queue to CheckQueue

* forcefully release slot if download didn't finish in 15s

* actually cancel the delay task

* add metrics and refactor some of the request queue service

* refactor pathing

* reuse httpclient

* add queue request dto to requestfile, enqueue users immediately if a slot is available

* change startup to include all controllers

* update server pathing

* update pathing, again

* several adjustments to auth, banning, jwt server tokens, renaming, authorization

* update api I guess

* adjust automated banning of charaident and reg

* generate jwt on servers for internal authentication

* remove mvcextensions

Co-authored-by: rootdarkarchon <root.darkarchon@outlook.com>
2023-01-11 12:22:22 +01:00

55 lines
1.7 KiB
C#

using MareSynchronos.API;
using MareSynchronosShared.Utils;
using MareSynchronosStaticFilesServer.Services;
using Microsoft.AspNetCore.Mvc;
using System.Text.Json;
namespace MareSynchronosStaticFilesServer.Controllers;
[Route(MareFiles.Request)]
public class RequestController : ControllerBase
{
private readonly CachedFileProvider _cachedFileProvider;
private readonly RequestQueueService _requestQueue;
public RequestController(ILogger<RequestController> logger, CachedFileProvider cachedFileProvider, RequestQueueService requestQueue,
ServerTokenGenerator generator) : base(logger, generator)
{
_cachedFileProvider = cachedFileProvider;
_requestQueue = requestQueue;
}
[HttpPost]
[Route(MareFiles.Request_Enqueue)]
public IActionResult PreRequestFiles([FromBody] List<string> files)
{
foreach (var file in files)
{
_cachedFileProvider.DownloadFileWhenRequired(file, Authorization);
}
return Ok();
}
[HttpGet]
[Route(MareFiles.Request_RequestFile)]
public async Task<IActionResult> RequestFile(string file)
{
Guid g = Guid.NewGuid();
_cachedFileProvider.DownloadFileWhenRequired(file, Authorization);
var queueStatus = await _requestQueue.EnqueueUser(new(g, MareUser, file));
return Ok(JsonSerializer.Serialize(new QueueRequestDto(g, queueStatus)));
}
[HttpGet]
[Route(MareFiles.Request_CheckQueue)]
public IActionResult CheckQueue(Guid requestId)
{
if (_requestQueue.IsActiveProcessing(requestId, MareUser, out _)) return Ok();
if (_requestQueue.StillEnqueued(requestId, MareUser, out int position)) return Conflict(position);
return BadRequest();
}
}