Built system for better and larger file uploads
This commit is contained in:
parent
5d19a2188f
commit
0fff6b4c78
|
|
@ -0,0 +1,23 @@
|
|||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
|
||||
namespace FileStorageService.www.Atttributes;
|
||||
|
||||
/// <summary>
|
||||
/// https://stackoverflow.com/a/62555240/13204730
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
|
||||
public class DisableFormValueModelBindingAttribute : Attribute, IResourceFilter
|
||||
{
|
||||
public void OnResourceExecuting(ResourceExecutingContext context)
|
||||
{
|
||||
var factories = context.ValueProviderFactories;
|
||||
// factories.RemoveType<FormValueProviderFactory>();
|
||||
factories.RemoveType<FormFileValueProviderFactory>();
|
||||
factories.RemoveType<JQueryFormValueProviderFactory>();
|
||||
}
|
||||
|
||||
public void OnResourceExecuted(ResourceExecutedContext context)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -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<IActionResult> Index(Guid? id)
|
||||
{
|
||||
|
|
@ -62,6 +65,9 @@ public class FilesController(
|
|||
}
|
||||
|
||||
[HttpPost]
|
||||
[DisableFormValueModelBinding]
|
||||
[RequestSizeLimit(MaxFileSize)]
|
||||
[RequestFormLimits(MultipartBodyLengthLimit = MaxFileSize)]
|
||||
public async Task<IActionResult> New(NewFileModel model)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
|
|
|
|||
|
|
@ -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<FileHandleModel> 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);
|
||||
}
|
||||
|
|
@ -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<List<FileHandle>> GetAllFilesAsync()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@
|
|||
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link text-dark" asp-area="" asp-controller="Files" asp-action="Index">Files</a>
|
||||
<a class="nav-link text-dark" asp-area="" asp-controller="Files" asp-route-id="@null">Files</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
|
|
|||
Loading…
Reference in New Issue