Add more logging to resources and matching

Change uploading to use ReadAllAsync instead of readFromPipeReader
This commit is contained in:
Slendy 2023-04-20 11:00:55 -05:00
parent f03d1d7c17
commit ddee367586
No known key found for this signature in database
GPG key ID: 7288D68361B91428
2 changed files with 11 additions and 26 deletions

View file

@ -59,7 +59,7 @@ public class MatchController : ControllerBase
}
catch(Exception e)
{
Logger.Error("Exception while parsing matchData: ", LogArea.Match);
Logger.Error($"Exception while parsing matchData: body='{bodyString}'", LogArea.Match);
Logger.Error(e.ToDetailedException(), LogArea.Match);
return this.BadRequest();
@ -67,7 +67,7 @@ public class MatchController : ControllerBase
if (matchData == null)
{
Logger.Error($"Could not parse match data: {nameof(matchData)} is null", LogArea.Match);
Logger.Error($"Could not parse match data: {nameof(matchData)} is null, body='{bodyString}'", LogArea.Match);
return this.BadRequest();
}

View file

@ -1,6 +1,5 @@
#nullable enable
using System.Buffers;
using System.IO.Pipelines;
using System.Text;
using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Files;
using LBPUnion.ProjectLighthouse.Logging;
@ -67,11 +66,18 @@ public class ResourcesController : ControllerBase
if (!fullPath.StartsWith(FileHelper.FullResourcePath)) return this.BadRequest();
Logger.Info($"Processing resource upload (hash: {hash})", LogArea.Resources);
LbpFile file = new(await readFromPipeReader(this.Request.BodyReader));
byte[] data = await this.Request.BodyReader.ReadAllAsync();
LbpFile file = new(data);
if (!FileHelper.IsFileSafe(file))
{
Logger.Warn($"File is unsafe (hash: {hash}, type: {file.FileType})", LogArea.Resources);
if (file.FileType == LbpFileType.Unknown)
{
Logger.Warn($"({hash}): File header: '{Convert.ToHexString(data[..4])}', " +
$"ascii='{Encoding.ASCII.GetString(data[..4])}'",
LogArea.Resources);
}
return this.Conflict();
}
@ -93,25 +99,4 @@ 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();
}
}