using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Options; using Socialize.Api.Infrastructure.BlobStorage.Configuration; using Socialize.Api.Infrastructure.BlobStorage.Services; namespace Socialize.Api.Infrastructure.Observability; internal sealed class LocalBlobStorageHealthCheck( LocalBlobStorage blobStorage, IOptions options) : IHealthCheck { public async Task CheckHealthAsync( HealthCheckContext context, CancellationToken cancellationToken = default) { string rootPath = blobStorage.GetRootPath(); if (string.IsNullOrWhiteSpace(options.Value.RequestPath)) { return HealthCheckResult.Unhealthy("Local blob storage request path is not configured."); } try { Directory.CreateDirectory(rootPath); string probePath = Path.Combine(rootPath, ".healthcheck"); await File.WriteAllTextAsync( probePath, DateTimeOffset.UtcNow.ToString("O", System.Globalization.CultureInfo.InvariantCulture), cancellationToken); File.Delete(probePath); return HealthCheckResult.Healthy("Local blob storage is writable."); } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) { return HealthCheckResult.Unhealthy("Local blob storage is not writable.", ex); } } }