backend + web: Monitor attachment storage and download PDFs

This commit is contained in:
GonnaHetzMe committed 2026-12-04 22:25:00 +01:00
1 parent 4f95c8a379
commit 5470818841
6 files changed
+67 -5

No files matched your search

@@ -0,0 +1,37 @@
using Helpdesk.Infrastructure.Files;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Helpdesk.Infrastructure.Background;
internal sealed partial class AttachmentStorageMonitor(TimeProvider clock, ILogger<AttachmentStorageMonitor> logger) : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
using var timer = new PeriodicTimer(TimeSpan.FromMinutes(5), clock);
do
{
try
{
var path = Directory.Exists(FileAttachmentStore.UploadDirectory)
? FileAttachmentStore.UploadDirectory
: AppContext.BaseDirectory;
var drive = new DriveInfo(path);
var available = drive.AvailableFreeSpace;
if (available < drive.TotalSize / 10)
LogLowSpace(available / (1024 * 1024));
}
catch (Exception exception) when (exception is IOException or UnauthorizedAccessException)
{
LogCheckFailed(exception);
}
}
while (await timer.WaitForNextTickAsync(stoppingToken));
}
[LoggerMessage(Level = LogLevel.Warning, Message = "Na úložišti příloh zbývá méně než 10 % volného místa ({FreeMiB} MiB).")]
private partial void LogLowSpace(long freeMiB);
[LoggerMessage(Level = LogLevel.Warning, Message = "Volné místo na úložišti příloh se nepodařilo zjistit.")]
private partial void LogCheckFailed(Exception exception);
}
@@ -47,6 +47,7 @@ public static class DependencyInjection
services.AddHostedService<AdminSeeder>();
services.AddHostedService<AuthTokenCleanupService>();
services.AddHostedService<NotificationDispatcher>();
services.AddHostedService<AttachmentStorageMonitor>();
return services;
}
@@ -8,7 +8,7 @@ namespace Helpdesk.Infrastructure.Files;
internal sealed class FileAttachmentStore(ILogger<FileAttachmentStore> logger) : IAttachmentStore
{
private readonly string directory = Path.Combine(AppContext.BaseDirectory, "uploads");
internal static readonly string UploadDirectory = Path.Combine(AppContext.BaseDirectory, "uploads");
public async Task<StoredAttachment> SaveAsync(AttachmentUpload upload, CancellationToken ct)
{
@@ -30,7 +30,7 @@ internal sealed class FileAttachmentStore(ILogger<FileAttachmentStore> logger) :
_ => throw new BadRequestException("Tento typ přílohy není povolen."),
};
Directory.CreateDirectory(directory);
Directory.CreateDirectory(UploadDirectory);
var key = Guid.NewGuid().ToString("D");
var path = FilePath(key);
@@ -97,7 +97,7 @@ internal sealed class FileAttachmentStore(ILogger<FileAttachmentStore> logger) :
}
private string FilePath(string key) => Guid.TryParseExact(key, "D", out _)
? Path.Combine(directory, key)
? Path.Combine(UploadDirectory, key)
: throw new InvalidOperationException("Neplatný klíč souboru přílohy.");
private static async Task<bool> HasExpectedFormatAsync(Stream stream, string extension, CancellationToken ct)