Cleaned up file controller and repositories.
This commit is contained in:
parent
200206c0cd
commit
5514d79b67
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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)]
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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; }
|
||||
|
|
|
|||
|
|
@ -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}");
|
||||
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
@ -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 num = blockNumber++;
|
||||
Task.Run(() =>
|
||||
{
|
||||
var block = new FileBlock
|
||||
{
|
||||
BlockNumber = blockNumber++,
|
||||
BlockNumber = num,
|
||||
Data = buffer.ToArray(),
|
||||
FileHandle = fileHandle
|
||||
};
|
||||
fileHandle.FileBlocks.Add(block);
|
||||
|
||||
logger.LogInformation($"Saved block {num}");
|
||||
});
|
||||
}
|
||||
|
||||
return fileHandle;
|
||||
|
|
|
|||
Loading…
Reference in New Issue