From cc1a2f82a7b560fddcbe74db66aad2e399594862 Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Mon, 16 Jan 2023 22:36:58 +0100 Subject: feat: Div --- src/BlobBin.csproj | 1 + src/Program.cs | 21 +++++++++++++++++++-- src/WallE.cs | 22 +++++++++++++++------- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/BlobBin.csproj b/src/BlobBin.csproj index 263a517..1fc2bf8 100644 --- a/src/BlobBin.csproj +++ b/src/BlobBin.csproj @@ -14,6 +14,7 @@ all + diff --git a/src/Program.cs b/src/Program.cs index 29b0fe1..68e7785 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -2,16 +2,33 @@ global using BlobBin; using System.Text; using System.Text.Json; using IOL.Helpers; +using Serilog; +using Serilog.Events; using File = BlobBin.File; const long MAX_REQUEST_BODY_SIZE = 104_857_600; var builder = WebApplication.CreateBuilder(args); +var logger = new LoggerConfiguration() + .MinimumLevel.Information() + .MinimumLevel.Override("Microsoft", LogEventLevel.Information) + .MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information) + .MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Information) + .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning) + .WriteTo.Console(); +Log.Logger = logger.CreateLogger(); +Log.Information("Starting web host"); +builder.Host.UseSerilog(Log.Logger); builder.Services.AddDbContext(); builder.Services.AddHostedService(); builder.WebHost.UseKestrel(o => { o.Limits.MaxRequestBodySize = MAX_REQUEST_BODY_SIZE; }); var app = builder.Build(); app.UseFileServer(); app.UseStatusCodePages(); +app.UseSerilogRequestLogging(); +if (app.Environment.IsProduction()) { + app.UseForwardedHeaders(); +} + app.MapGet("/upload-link", GetFileUploadLink); app.MapPost("/file/{id}", UploadFilePart); app.MapPost("/file", UploadFile); @@ -66,7 +83,7 @@ IResult DeleteUpload(HttpContext context, Db db, string id, string key = default db.SaveChanges(); return Results.Text(""" -The file is marked for deletion and cannot be accessed any more, all traces off it will be gone from our systems within 7 days. +The upload is marked for deletion and cannot be accessed any more, all traces off it will be gone from our systems within 7 days. """); } @@ -123,7 +140,7 @@ To delete the file, open this url in a browser {context.Request.GetRequestHost() """); } -IResult UploadFilePart(HttpContext context, Db db) { +IResult UploadFilePart(HttpContext context, Db db, Guid id) { return Results.Ok(); } diff --git a/src/WallE.cs b/src/WallE.cs index b675d02..744d10f 100644 --- a/src/WallE.cs +++ b/src/WallE.cs @@ -21,8 +21,8 @@ public class WallE : BackgroundService private async Task DoWork(CancellationToken stoppingToken) { _logger.LogInformation("WallE is working."); using var scope = Services.CreateScope(); - var eva = scope.ServiceProvider.GetRequiredService(); - var pastes = eva.Pastes.Where(c => c.DeletedAt != default || c.AutoDeleteAfter != default) + var db = scope.ServiceProvider.GetRequiredService(); + var pastes = db.Pastes.Where(c => c.DeletedAt != default || c.AutoDeleteAfter != default) .Select(c => new Paste() { Id = c.Id, DeletedAt = c.DeletedAt, @@ -30,7 +30,7 @@ public class WallE : BackgroundService CreatedAt = c.CreatedAt }) .ToList(); - var files = eva.Files + var files = db.Files .Where(c => c.DeletedAt != default || c.AutoDeleteAfter != default) .Select(c => new File() { Id = c.Id, @@ -44,12 +44,16 @@ public class WallE : BackgroundService var path = Path.Combine(Tools.GetFilesDirectoryPath(), file.Id.ToString()); if (DateTime.Compare(now, file.DeletedAt?.AddDays(7) ?? DateTime.MinValue) > 0) { System.IO.File.Delete(path); - eva.Files.Remove(file); + db.Files.Remove(file); + await db.SaveChangesAsync(stoppingToken); + _logger.LogInformation("Deleted file {fileId} completely", file.Id); } else if (file.AutoDeleteAfter.HasValue()) { var autoDeleteDateTime = file.CreatedAt.Add(Tools.ParseHumanTimeSpan(file.AutoDeleteAfter)); if (DateTime.Compare(now, autoDeleteDateTime) > 0) { System.IO.File.Delete(path); - eva.Files.Remove(file); + db.Files.Remove(file); + await db.SaveChangesAsync(stoppingToken); + _logger.LogInformation("Deleted file {fileId} completely", file.Id); } } } @@ -57,11 +61,15 @@ public class WallE : BackgroundService // If the file has lived more than +7 days from when it should be automatically deleted, delete the file completely. foreach (var paste in pastes) { if (DateTime.Compare(now, paste.DeletedAt?.AddDays(7) ?? DateTime.MinValue) > 0) { - eva.Pastes.Remove(paste); + db.Pastes.Remove(paste); + _logger.LogInformation("Deleted paste {pasteId} completely", paste.Id); + await db.SaveChangesAsync(stoppingToken); } else if (paste.AutoDeleteAfter.HasValue()) { var autoDeleteDateTime = paste.CreatedAt.Add(Tools.ParseHumanTimeSpan(paste.AutoDeleteAfter)); if (DateTime.Compare(now, autoDeleteDateTime) > 0) { - eva.Pastes.Remove(paste); + db.Pastes.Remove(paste); + await db.SaveChangesAsync(stoppingToken); + _logger.LogInformation("Deleted paste {pasteId} completely", paste.Id); } } } -- cgit v1.3