This commit is contained in:
Kieran 2022-01-25 17:57:07 +00:00
parent b68f19c1b9
commit afee82fed9
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
11 changed files with 102 additions and 49 deletions

4
.gitignore vendored
View File

@ -11,4 +11,6 @@ Release/
node_modules/
package-lock.json
out/
sw.js
sw.js
.DS_Store
.idea/

22
Dockerfile Normal file
View File

@ -0,0 +1,22 @@
# syntax=docker/dockerfile:1
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app
#install npm
RUN curl -fsSL https://deb.nodesource.com/setup_16.x | bash -
RUN apt-get install -y nodejs
#run yarn install
COPY VoidCat/spa/package.json VoidCat/spa/yarn.lock spa/
RUN cd spa && npx yarn install
# Copy everything else and build
COPY VoidCat .
RUN rm -rf appsettings.*.json
RUN dotnet publish -c Release -o out VoidCat.csproj
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "VoidCat.dll"]

View File

@ -1,5 +1,4 @@
using Microsoft.AspNetCore.Mvc;
using System.Buffers;
using VoidCat.Model;
using VoidCat.Services;
@ -8,32 +7,20 @@ namespace VoidCat.Controllers
[Route("upload")]
public class UploadController : Controller
{
private readonly IFileIngressFactory _fileIngress;
private readonly IStatsCollector _stats;
public UploadController(IStatsCollector stats)
public UploadController(IStatsCollector stats, IFileIngressFactory fileIngress)
{
_stats = stats;
_fileIngress = fileIngress;
}
[HttpPost]
public Task<VoidFile> UploadFile()
{
return Request.HasFormContentType ?
saveFromForm() : saveFromBody();
}
private async Task<VoidFile> saveFromBody()
{
var temp = Path.GetTempFileName();
using var fsTemp = new FileStream(temp, FileMode.Create, FileAccess.ReadWrite);
var buffer = MemoryPool<byte>.Shared.Rent();
var rlen = 0;
while ((rlen = await Request.Body.ReadAsync(buffer.Memory)) > 0)
{
}
return default;
saveFromForm() : _fileIngress.Ingress(Request.Body);
}
private Task<VoidFile> saveFromForm()

View File

@ -1,22 +0,0 @@
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["VoidCat/VoidCat.csproj", "VoidCat/"]
RUN dotnet restore "VoidCat/VoidCat.csproj"
COPY . .
WORKDIR "/src/VoidCat"
RUN dotnet build "VoidCat.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "VoidCat.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "VoidCat.dll"]

View File

@ -1,10 +1,13 @@
using VoidCat;
using VoidCat.Model;
using VoidCat.Services;
var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;
services.AddRouting();
services.AddControllers();
services.AddScoped<IFileIngressFactory, LocalDiskFileIngressFactory>();
services.AddScoped<IStatsCollector, InMemoryStatsCollector>();
var configuration = builder.Configuration;
var voidSettings = configuration.GetSection("Settings").Get<VoidSettings>();

View File

@ -1,6 +1,9 @@
namespace VoidCat.Services
using VoidCat.Model;
namespace VoidCat.Services
{
public interface IFileIngressFactory
{
Task<VoidFile> Ingress(Stream inStream);
}
}

View File

@ -0,0 +1,15 @@
namespace VoidCat.Services
{
public class InMemoryStatsCollector : IStatsCollector
{
public ValueTask TrackIngress(Guid id, ulong amount)
{
throw new NotImplementedException();
}
public ValueTask TrackEgress(Guid id, ulong amount)
{
throw new NotImplementedException();
}
}
}

View File

@ -0,0 +1,42 @@
using System.Buffers;
using VoidCat.Model;
namespace VoidCat.Services;
public class LocalDiskFileIngressFactory : IFileIngressFactory
{
private readonly VoidSettings _settings;
private readonly IStatsCollector _stats;
public LocalDiskFileIngressFactory(VoidSettings settings, IStatsCollector stats)
{
_settings = settings;
_stats = stats;
}
public async Task<VoidFile> Ingress(Stream inStream)
{
var id = Guid.NewGuid();
var fPath = mapPath(id);
using var fsTemp = new FileStream(fPath, FileMode.Create, FileAccess.ReadWrite);
var buffer = MemoryPool<byte>.Shared.Rent();
var total = 0UL;
var readLength = 0;
while ((readLength = await inStream.ReadAsync(buffer.Memory)) > 0)
{
await fsTemp.WriteAsync(buffer.Memory[..readLength]);
await _stats.TrackIngress(id, (ulong)readLength);
total += (ulong)readLength;
}
return new()
{
Id = id,
Size = total
};
}
private string mapPath(Guid id) =>
Path.Join(_settings.FilePath, id.ToString());
}

View File

@ -1,6 +0,0 @@
namespace VoidCat.Services
{
public class StatsCollector
{
}
}

View File

@ -17,6 +17,9 @@
<!-- Don't publish the SPA source files, but do show them in the project files list -->
<Content Remove="$(SpaRoot)**" />
<Content Include="$(SolutionDir).github\workflows\build.yml" />
<Content Include="..\Dockerfile">
<Link>Dockerfile</Link>
</Content>
<None Remove="$(SpaRoot)**" />
<None Include="$(SpaRoot)**" Exclude="$(SpaRoot)node_modules\**" />
</ItemGroup>

View File

@ -1,9 +1,13 @@
import './App.css';
function App() {
function selectFiles(e) {
}
return (
<div className="app">
<div className="drop">
<div className="drop" onClick={selectFiles}>
<h3>Drop files here!</h3>
</div>
</div>