From afee82fed993f349c9a17d82e12cba75aa4ba810 Mon Sep 17 00:00:00 2001 From: Kieran Date: Tue, 25 Jan 2022 17:57:07 +0000 Subject: [PATCH] zz --- .gitignore | 4 +- Dockerfile | 22 ++++++++++ VoidCat/Controllers/UploadController.cs | 21 ++-------- VoidCat/Dockerfile | 22 ---------- VoidCat/Program.cs | 5 ++- VoidCat/Services/IFileIngressFactory.cs | 5 ++- VoidCat/Services/InMemoryStatsCollector.cs | 15 +++++++ .../Services/LocalDiskFileIngressFactory.cs | 42 +++++++++++++++++++ VoidCat/Services/StatsCollector.cs | 6 --- VoidCat/VoidCat.csproj | 3 ++ VoidCat/spa/src/App.js | 6 ++- 11 files changed, 102 insertions(+), 49 deletions(-) create mode 100644 Dockerfile delete mode 100644 VoidCat/Dockerfile create mode 100644 VoidCat/Services/InMemoryStatsCollector.cs create mode 100644 VoidCat/Services/LocalDiskFileIngressFactory.cs delete mode 100644 VoidCat/Services/StatsCollector.cs diff --git a/.gitignore b/.gitignore index 5733fa4..7331603 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,6 @@ Release/ node_modules/ package-lock.json out/ -sw.js \ No newline at end of file +sw.js +.DS_Store +.idea/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b1361c4 --- /dev/null +++ b/Dockerfile @@ -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"] \ No newline at end of file diff --git a/VoidCat/Controllers/UploadController.cs b/VoidCat/Controllers/UploadController.cs index 55b6ecd..224512f 100644 --- a/VoidCat/Controllers/UploadController.cs +++ b/VoidCat/Controllers/UploadController.cs @@ -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 UploadFile() { return Request.HasFormContentType ? - saveFromForm() : saveFromBody(); - } - - private async Task saveFromBody() - { - var temp = Path.GetTempFileName(); - using var fsTemp = new FileStream(temp, FileMode.Create, FileAccess.ReadWrite); - - var buffer = MemoryPool.Shared.Rent(); - var rlen = 0; - while ((rlen = await Request.Body.ReadAsync(buffer.Memory)) > 0) - { - - } - return default; + saveFromForm() : _fileIngress.Ingress(Request.Body); } private Task saveFromForm() diff --git a/VoidCat/Dockerfile b/VoidCat/Dockerfile deleted file mode 100644 index 131d624..0000000 --- a/VoidCat/Dockerfile +++ /dev/null @@ -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"] \ No newline at end of file diff --git a/VoidCat/Program.cs b/VoidCat/Program.cs index 23ea55f..ec04855 100644 --- a/VoidCat/Program.cs +++ b/VoidCat/Program.cs @@ -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(); +services.AddScoped(); var configuration = builder.Configuration; var voidSettings = configuration.GetSection("Settings").Get(); diff --git a/VoidCat/Services/IFileIngressFactory.cs b/VoidCat/Services/IFileIngressFactory.cs index 12eb786..220f27c 100644 --- a/VoidCat/Services/IFileIngressFactory.cs +++ b/VoidCat/Services/IFileIngressFactory.cs @@ -1,6 +1,9 @@ -namespace VoidCat.Services +using VoidCat.Model; + +namespace VoidCat.Services { public interface IFileIngressFactory { + Task Ingress(Stream inStream); } } diff --git a/VoidCat/Services/InMemoryStatsCollector.cs b/VoidCat/Services/InMemoryStatsCollector.cs new file mode 100644 index 0000000..f994ac2 --- /dev/null +++ b/VoidCat/Services/InMemoryStatsCollector.cs @@ -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(); + } + } +} diff --git a/VoidCat/Services/LocalDiskFileIngressFactory.cs b/VoidCat/Services/LocalDiskFileIngressFactory.cs new file mode 100644 index 0000000..a16971d --- /dev/null +++ b/VoidCat/Services/LocalDiskFileIngressFactory.cs @@ -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 Ingress(Stream inStream) + { + var id = Guid.NewGuid(); + var fPath = mapPath(id); + using var fsTemp = new FileStream(fPath, FileMode.Create, FileAccess.ReadWrite); + + var buffer = MemoryPool.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()); +} \ No newline at end of file diff --git a/VoidCat/Services/StatsCollector.cs b/VoidCat/Services/StatsCollector.cs deleted file mode 100644 index 7cc9ef8..0000000 --- a/VoidCat/Services/StatsCollector.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace VoidCat.Services -{ - public class StatsCollector - { - } -} diff --git a/VoidCat/VoidCat.csproj b/VoidCat/VoidCat.csproj index 4820fcc..194f634 100644 --- a/VoidCat/VoidCat.csproj +++ b/VoidCat/VoidCat.csproj @@ -17,6 +17,9 @@ + + Dockerfile + diff --git a/VoidCat/spa/src/App.js b/VoidCat/spa/src/App.js index 9c5f604..d96bb11 100644 --- a/VoidCat/spa/src/App.js +++ b/VoidCat/spa/src/App.js @@ -1,9 +1,13 @@ import './App.css'; function App() { + + function selectFiles(e) { + + } return (
-
+

Drop files here!