Add AllowSynchronousIOAttribute, allow creating LbpFile from request body

This commit is contained in:
jvyden 2021-10-21 18:10:06 -04:00
commit 86c6ac035b
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
3 changed files with 32 additions and 2 deletions

View file

@ -7,6 +7,7 @@ using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Serialization; using LBPUnion.ProjectLighthouse.Serialization;
using LBPUnion.ProjectLighthouse.Types; using LBPUnion.ProjectLighthouse.Types;
using LBPUnion.ProjectLighthouse.Types.Files; using LBPUnion.ProjectLighthouse.Types.Files;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using IOFile = System.IO.File; using IOFile = System.IO.File;
@ -50,14 +51,16 @@ namespace LBPUnion.ProjectLighthouse.Controllers {
// TODO: check if this is a valid hash // TODO: check if this is a valid hash
[HttpPost("upload/{hash}")] [HttpPost("upload/{hash}")]
[AllowSynchronousIo]
public async Task<IActionResult> UploadResource(string hash) { public async Task<IActionResult> UploadResource(string hash) {
string assetsDirectory = FileHelper.ResourcePath; string assetsDirectory = FileHelper.ResourcePath;
string path = FileHelper.GetResourcePath(hash); string path = FileHelper.GetResourcePath(hash);
FileHelper.EnsureDirectoryCreated(assetsDirectory); FileHelper.EnsureDirectoryCreated(assetsDirectory);
if(FileHelper.ResourceExists(hash)) this.Ok(); // no reason to fail if it's already uploaded if(FileHelper.ResourceExists(hash)) this.Ok(); // no reason to fail if it's already uploaded
LbpFile file = new(Encoding.ASCII.GetBytes(await new StreamReader(this.Request.Body).ReadToEndAsync())); LbpFile file = new(this.Request.Body);
if(!FileHelper.IsFileSafe(file)) return this.UnprocessableEntity(); if(!FileHelper.IsFileSafe(file)) return this.UnprocessableEntity();

View file

@ -0,0 +1,18 @@
using System;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc.Filters;
namespace LBPUnion.ProjectLighthouse.Helpers {
// Yoinked from https://stackoverflow.com/a/68530667
// Thanks to T-moty!
/// <summary>
/// Allows synchronous stream operations for this request.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class AllowSynchronousIoAttribute : ActionFilterAttribute {
public override void OnResultExecuting(ResultExecutingContext context) {
IHttpBodyControlFeature syncIoFeature = context.HttpContext.Features.Get<IHttpBodyControlFeature>();
if(syncIoFeature != null) syncIoFeature.AllowSynchronousIO = true;
}
}
}

View file

@ -1,10 +1,19 @@
using System.IO;
using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Helpers;
namespace LBPUnion.ProjectLighthouse.Types.Files { namespace LBPUnion.ProjectLighthouse.Types.Files {
public class LbpFile { public class LbpFile {
public LbpFile(byte[] data) { public LbpFile(byte[] data) {
this.Data = data; this.Data = data;
this.FileType = FileHelper.DetermineFileType(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);
} }
/// <summary> /// <summary>