diff --git a/ProjectLighthouse/Controllers/ResourcesController.cs b/ProjectLighthouse/Controllers/ResourcesController.cs index 60ff7ae0..a97106ce 100644 --- a/ProjectLighthouse/Controllers/ResourcesController.cs +++ b/ProjectLighthouse/Controllers/ResourcesController.cs @@ -1,13 +1,13 @@ +using System.Buffers; using System.IO; +using System.IO.Pipelines; using System.Linq; -using System.Text; using System.Threading.Tasks; using System.Xml.Serialization; using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Serialization; using LBPUnion.ProjectLighthouse.Types; using LBPUnion.ProjectLighthouse.Types.Files; -using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Mvc; using IOFile = System.IO.File; @@ -60,7 +60,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers { FileHelper.EnsureDirectoryCreated(assetsDirectory); if(FileHelper.ResourceExists(hash)) this.Ok(); // no reason to fail if it's already uploaded - LbpFile file = new(this.Request.Body); + LbpFile file = new(await BinaryHelper.ReadFromPipeReader(Request.BodyReader)); if(!FileHelper.IsFileSafe(file)) return this.UnprocessableEntity(); diff --git a/ProjectLighthouse/Helpers/BinaryHelper.cs b/ProjectLighthouse/Helpers/BinaryHelper.cs index a157df82..80d0b1cf 100644 --- a/ProjectLighthouse/Helpers/BinaryHelper.cs +++ b/ProjectLighthouse/Helpers/BinaryHelper.cs @@ -1,7 +1,10 @@ using System; +using System.Buffers; using System.Collections.Generic; using System.IO; +using System.IO.Pipelines; using System.Text; +using System.Threading.Tasks; namespace LBPUnion.ProjectLighthouse.Helpers { public static class BinaryHelper { @@ -34,5 +37,24 @@ namespace LBPUnion.ProjectLighthouse.Helpers { if(restoreOldPosition) reader.BaseStream.Position = oldPosition; return data; } + + // 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) + public static async Task ReadFromPipeReader(PipeReader reader) { + List data = new(); + while(true) { + ReadResult readResult = await reader.ReadAsync(); + ReadOnlySequence 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(); + } } } \ No newline at end of file diff --git a/ProjectLighthouse/Types/Files/LbpFile.cs b/ProjectLighthouse/Types/Files/LbpFile.cs index b8f7645c..da521195 100644 --- a/ProjectLighthouse/Types/Files/LbpFile.cs +++ b/ProjectLighthouse/Types/Files/LbpFile.cs @@ -7,14 +7,6 @@ namespace LBPUnion.ProjectLighthouse.Types.Files { this.Data = data; this.FileType = FileHelper.DetermineFileType(this.Data); } - - public LbpFile(Stream stream) { - using MemoryStream ms = new(); - stream.CopyToAsync(ms); - - this.Data = ms.ToArray(); - this.FileType = FileHelper.DetermineFileType(this.Data); - } /// /// The type of file.