mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-07-06 05:11:27 +00:00
Combine a bunch of helpers together, shuffle some things around
This commit is contained in:
parent
71a97894ad
commit
330c01317d
61 changed files with 327 additions and 371 deletions
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue