From 0fff6b4c7848ed1066e68d2792bd1b0c7a2e0b68 Mon Sep 17 00:00:00 2001 From: michael-bailey Date: Mon, 17 Mar 2025 19:42:39 +0000 Subject: [PATCH] Built system for better and larger file uploads --- .../DisableFormValueModelBindingAttribute.cs | 23 +++++++++++++++++++ .../Controllers/FilesController.cs | 6 +++++ .../Models/FileHandleListModel.cs | 3 ++- .../Repositories/FileRepository.cs | 6 ++--- .../Views/Shared/_Layout.cshtml | 2 +- 5 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 FileStorageService.www/Atttributes/DisableFormValueModelBindingAttribute.cs diff --git a/FileStorageService.www/Atttributes/DisableFormValueModelBindingAttribute.cs b/FileStorageService.www/Atttributes/DisableFormValueModelBindingAttribute.cs new file mode 100644 index 0000000..eb3e4ea --- /dev/null +++ b/FileStorageService.www/Atttributes/DisableFormValueModelBindingAttribute.cs @@ -0,0 +1,23 @@ +using Microsoft.AspNetCore.Mvc.Filters; +using Microsoft.AspNetCore.Mvc.ModelBinding; + +namespace FileStorageService.www.Atttributes; + +/// +/// https://stackoverflow.com/a/62555240/13204730 +/// +[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] +public class DisableFormValueModelBindingAttribute : Attribute, IResourceFilter +{ + public void OnResourceExecuting(ResourceExecutingContext context) + { + var factories = context.ValueProviderFactories; + // factories.RemoveType(); + factories.RemoveType(); + factories.RemoveType(); + } + + public void OnResourceExecuted(ResourceExecutedContext context) + { + } +} \ No newline at end of file diff --git a/FileStorageService.www/Controllers/FilesController.cs b/FileStorageService.www/Controllers/FilesController.cs index 1b63ef8..c4f35e3 100644 --- a/FileStorageService.www/Controllers/FilesController.cs +++ b/FileStorageService.www/Controllers/FilesController.cs @@ -1,4 +1,5 @@ using System.Diagnostics.CodeAnalysis; +using FileStorageService.www.Atttributes; using FileStorageService.www.Data; using FileStorageService.www.Models; using FileStorageService.www.Repositories; @@ -12,6 +13,8 @@ public class FilesController( ApplicationDbContext context, FileRepository fileRepository) : Controller { + private const long MaxFileSize = 7L * 1024L * 1024L * 1024L; + // GET public async Task Index(Guid? id) { @@ -62,6 +65,9 @@ public class FilesController( } [HttpPost] + [DisableFormValueModelBinding] + [RequestSizeLimit(MaxFileSize)] + [RequestFormLimits(MultipartBodyLengthLimit = MaxFileSize)] public async Task New(NewFileModel model) { if (!ModelState.IsValid) diff --git a/FileStorageService.www/Models/FileHandleListModel.cs b/FileStorageService.www/Models/FileHandleListModel.cs index 61ba49d..33aa341 100644 --- a/FileStorageService.www/Models/FileHandleListModel.cs +++ b/FileStorageService.www/Models/FileHandleListModel.cs @@ -1,4 +1,5 @@ using FileStorageService.www.Data; +using FileStorageService.www.Repositories; namespace FileStorageService.www.Models; @@ -6,6 +7,6 @@ public class FileHandleListModel { public required List FileHandles { get; init; } public int CurrentBlockCount => FileHandles.Select(s => s.BlockCount).Sum(); - public int AllocatedBlockCount => 1024; + public int AllocatedBlockCount => FileRepository.MAX_BLOCKS; public int BlockUsagePercentage => (int)Math.Ceiling(((float)CurrentBlockCount / AllocatedBlockCount)*100); } \ No newline at end of file diff --git a/FileStorageService.www/Repositories/FileRepository.cs b/FileStorageService.www/Repositories/FileRepository.cs index 5fea9fc..8c3dcc5 100644 --- a/FileStorageService.www/Repositories/FileRepository.cs +++ b/FileStorageService.www/Repositories/FileRepository.cs @@ -5,10 +5,10 @@ namespace FileStorageService.www.Repositories; public class FileRepository(ApplicationDbContext context) { - private const int MAX_BLOCKS = 1024; + public static readonly int MAX_BLOCKS = 10_485_760; - private Queue<(string, Stream)> creationQueue = new(); - private Lock countLock = new(); + private readonly Queue<(string, Stream)> creationQueue = new(); + private readonly Lock countLock = new(); public async Task> GetAllFilesAsync() { diff --git a/FileStorageService.www/Views/Shared/_Layout.cshtml b/FileStorageService.www/Views/Shared/_Layout.cshtml index 2f06810..e314b60 100644 --- a/FileStorageService.www/Views/Shared/_Layout.cshtml +++ b/FileStorageService.www/Views/Shared/_Layout.cshtml @@ -24,7 +24,7 @@ Home