Cleaned up file controller and repositories.

This commit is contained in:
michael-bailey 2025-06-01 19:04:35 +01:00
parent 200206c0cd
commit 5514d79b67
7 changed files with 43 additions and 22 deletions

View File

@ -13,10 +13,15 @@
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2"/>
<PackageReference Include="JetBrains.Annotations" Version="2025.1.0-eap1" />
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="9.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1"/>
<PackageReference Include="xunit" Version="2.9.2"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit.v3" Version="2.0.2" />
</ItemGroup>
<ItemGroup>

View File

@ -2,15 +2,24 @@ using FileStorageService.www.Data;
using FileStorageService.www.Repositories;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Moq;
namespace FileStorageService.www.Test.Repositories;
public class FileRepositoryTests
{
private readonly Logger<FileRepository> _logger;
private readonly ApplicationDbContext _context;
public FileRepositoryTests()
{
var mock = new Mock<Logger<FileRepository>>();
mock.SetupAllProperties();
_logger = mock.Object;
var connection = new SqliteConnection("Filename=:memory:");
connection.Open();
@ -24,8 +33,7 @@ public class FileRepositoryTests
_context.Database.EnsureCreated();
}
private FileRepository repository => new FileRepository(_context);
private FileRepository repository => new(_logger, _context);
[Theory]
[InlineData("one", 1)]

View File

@ -75,10 +75,7 @@ public class FilesController(
var fileHandleId = await fileRepository.TryNewFileAsync(name, stream);
if (fileHandleId != null)
return RedirectToAction("Index", "Files", new { id = fileHandleId });
return RedirectToAction("NotEnoughSpace");
return fileHandleId != null ? RedirectToAction("Index", "Files", new { id = fileHandleId }) : RedirectToAction("NotEnoughSpace");
}
public IActionResult NotEnoughSpace()

View File

@ -12,7 +12,7 @@ public class FileBlock
public required int BlockNumber { get; init; }
[DataType("BLOB")]
[MaxLength(1024)]
[MaxLength(4096)]
public required byte[] Data { get; init; }
public required FileHandle FileHandle { get; init; }

View File

@ -23,6 +23,7 @@ builder.Services.AddScoped<FileRepository>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
@ -31,10 +32,10 @@ if (app.Environment.IsDevelopment())
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
// app.UseHsts();
}
app.UseHttpsRedirection();
// app.UseHttpsRedirection();
app.UseStatusCodePagesWithRedirects("/Error/Status/{0}");

View File

@ -5,7 +5,7 @@
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5197",
"applicationUrl": "http://localhost:5197;http://british-information-technologies.org:5197",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}

View File

@ -5,9 +5,12 @@ using Microsoft.EntityFrameworkCore;
namespace FileStorageService.www.Repositories;
public class FileRepository(ApplicationDbContext context)
public class FileRepository(
ILogger<FileRepository> logger,
ApplicationDbContext context)
{
public static readonly int MAX_BLOCKS = 10_485_760;
public static readonly int BLOCK_SIZE = 4096;
private readonly Queue<(string, Stream, TaskCompletionSource<Guid?>)> _creationQueue = new();
private readonly Lock _countLock = new();
@ -66,7 +69,7 @@ public class FileRepository(ApplicationDbContext context)
return tcs.Task;
}
private async Task StartProcessFileUpload()
{
Stream stream;
@ -107,17 +110,24 @@ public class FileRepository(ApplicationDbContext context)
private async Task<FileHandle> CreateFileBlocks(Stream reader, FileHandle fileHandle)
{
var blockNumber = 0;
var buffer = new byte[1024];
var buffer = new byte[BLOCK_SIZE];
while (await reader.ReadAsync(buffer) > 0)
{
var block = new FileBlock
var num = blockNumber++;
Task.Run(() =>
{
BlockNumber = blockNumber++,
Data = buffer.ToArray(),
FileHandle = fileHandle
};
fileHandle.FileBlocks.Add(block);
var block = new FileBlock
{
BlockNumber = num,
Data = buffer.ToArray(),
FileHandle = fileHandle
};
fileHandle.FileBlocks.Add(block);
logger.LogInformation($"Saved block {num}");
});
}
return fileHandle;