feat: paste views; configuration validation; hmacsha256 method; enhance paste clean up serivce; paste view clean up service;

This commit is contained in:
2026-01-09 19:12:05 -05:00
parent 60876eb1ba
commit 3048af3a6d
20 changed files with 592 additions and 46 deletions
@@ -0,0 +1,50 @@
using Microsoft.EntityFrameworkCore;
using SharpbinV3.Server.Data;
namespace SharpbinV3.Server.Services
{
public sealed class PasteViewCleanUpService(
IServiceProvider serviceProvider,
ILogger<PasteViewCleanUpService> logger
) : BackgroundService
{
private static readonly TimeSpan Period = TimeSpan.FromHours(1);
private static readonly long RetentionMs =
(long)TimeSpan.FromDays(7).TotalMilliseconds;
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
using var timer = new PeriodicTimer(Period);
while (!stoppingToken.IsCancellationRequested &&
await timer.WaitForNextTickAsync(stoppingToken))
{
try
{
await Cleanup(stoppingToken);
}
catch (Exception ex)
{
logger.LogWarning(ex, "PasteView cleanup failed");
}
}
}
private async Task Cleanup(CancellationToken token)
{
using var scope = serviceProvider.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
var cutoff =
DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - RetentionMs;
var deleted = await db.PasteViews
.Where(v => v.ViewedAt < cutoff)
.ExecuteDeleteAsync(token);
if (deleted > 0)
logger.LogInformation("Deleted {Count} stale paste views", deleted);
}
}
}