Combine a bunch of helpers together, shuffle some things around

This commit is contained in:
jvyden 2022-05-15 00:24:22 -04:00
commit 330c01317d
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
61 changed files with 327 additions and 371 deletions

View file

@ -89,7 +89,7 @@ public class LoginController : ControllerBase
{
GameToken = token,
GameTokenId = token.TokenId,
Timestamp = TimestampHelper.Timestamp,
Timestamp = TimeHelper.Timestamp,
IPAddress = ipAddress,
Platform = npTicket.Platform,
};

View file

@ -1,7 +1,7 @@
#nullable enable
using System.Text.Json;
using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Helpers.Extensions;
using LBPUnion.ProjectLighthouse.Logging;
using LBPUnion.ProjectLighthouse.Types;
using LBPUnion.ProjectLighthouse.Types.Match;

View file

@ -53,7 +53,7 @@ public class PhotosController : ControllerBase
if (photo.Subjects.Count > 4) return this.BadRequest();
if (photo.Timestamp > TimestampHelper.Timestamp) photo.Timestamp = TimestampHelper.Timestamp;
if (photo.Timestamp > TimeHelper.Timestamp) photo.Timestamp = TimeHelper.Timestamp;
foreach (PhotoSubject subject in photo.Subjects)
{

View file

@ -1,4 +1,6 @@
#nullable enable
using System.Buffers;
using System.IO.Pipelines;
using System.Xml.Serialization;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Logging;
@ -75,7 +77,7 @@ public class ResourcesController : ControllerBase
if (FileHelper.ResourceExists(hash)) this.Conflict();
Logger.LogInfo($"Processing resource upload (hash: {hash})", LogArea.Resources);
LbpFile file = new(await BinaryHelper.ReadFromPipeReader(this.Request.BodyReader));
LbpFile file = new(await readFromPipeReader(this.Request.BodyReader));
if (!FileHelper.IsFileSafe(file))
{
@ -95,4 +97,25 @@ public class ResourcesController : ControllerBase
await IOFile.WriteAllBytesAsync(path, file.Data);
return this.Ok();
}
// Written with reference from
// https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/request-response?view=aspnetcore-5.0
// Surprisingly doesn't take seconds. (67ms for a 100kb file)
private static async Task<byte[]> readFromPipeReader(PipeReader reader)
{
List<byte> data = new();
while (true)
{
ReadResult readResult = await reader.ReadAsync();
ReadOnlySequence<byte> buffer = readResult.Buffer;
if (readResult.IsCompleted && buffer.Length > 0) data.AddRange(buffer.ToArray());
reader.AdvanceTo(buffer.Start, buffer.End);
if (readResult.IsCompleted) break;
}
return data.ToArray();
}
}

View file

@ -1,7 +1,7 @@
#nullable enable
using System.Xml.Serialization;
using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Helpers.Extensions;
using LBPUnion.ProjectLighthouse.Serialization;
using LBPUnion.ProjectLighthouse.Types;
using LBPUnion.ProjectLighthouse.Types.Levels;

View file

@ -1,6 +1,6 @@
#nullable enable
using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Helpers.Extensions;
using LBPUnion.ProjectLighthouse.Serialization;
using LBPUnion.ProjectLighthouse.Types;
using LBPUnion.ProjectLighthouse.Types.Levels;