mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-08-08 04:48:44 +00:00
Add AllowSynchronousIOAttribute, allow creating LbpFile from request body
This commit is contained in:
parent
2085e5a8ca
commit
86c6ac035b
3 changed files with 32 additions and 2 deletions
|
@ -7,6 +7,7 @@ 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;
|
||||
|
||||
|
@ -50,14 +51,16 @@ namespace LBPUnion.ProjectLighthouse.Controllers {
|
|||
|
||||
// TODO: check if this is a valid hash
|
||||
[HttpPost("upload/{hash}")]
|
||||
[AllowSynchronousIo]
|
||||
public async Task<IActionResult> UploadResource(string hash) {
|
||||
|
||||
string assetsDirectory = FileHelper.ResourcePath;
|
||||
string path = FileHelper.GetResourcePath(hash);
|
||||
|
||||
FileHelper.EnsureDirectoryCreated(assetsDirectory);
|
||||
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();
|
||||
|
||||
|
|
18
ProjectLighthouse/Helpers/AllowSynchronousIOAttribute.cs
Normal file
18
ProjectLighthouse/Helpers/AllowSynchronousIOAttribute.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,10 +1,19 @@
|
|||
using System.IO;
|
||||
using LBPUnion.ProjectLighthouse.Helpers;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Types.Files {
|
||||
public class LbpFile {
|
||||
public LbpFile(byte[] 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>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue