using System.Security.Cryptography; using Microsoft.Extensions.Options; using Minio; using Minio.DataModel.Args; public class DocumentStorageService : IDocumentStorageService { private readonly IMinioClient _minio; private readonly MinioOptions _options; // Process-lifetime: DocumentStorageService is scoped; bucket only needs ensuring once. private static int _bucketReady; // 0 = unknown, 1 = ensured public DocumentStorageService(IMinioClient minio, IOptions options) { _minio = minio; _options = options.Value; } public async Task<(string objectKey, string sha256, long fileSize)> UploadAsync( Stream fileStream, string contentType, Guid batchId, CancellationToken ct = default) { var now = DateTimeOffset.UtcNow; using var sha256 = IncrementalHash.CreateHash(HashAlgorithmName.SHA256); using var memStream = new MemoryStream(); var buffer = new byte[81920]; int read; while ((read = await fileStream.ReadAsync(buffer.AsMemory(0, buffer.Length), ct)) > 0) { sha256.AppendData(buffer.AsSpan(0, read)); await memStream.WriteAsync(buffer.AsMemory(0, read), ct); } var hashHex = Convert.ToHexString(sha256.GetHashAndReset()).ToLowerInvariant(); memStream.Position = 0; var extension = contentType switch { "application/pdf" => "pdf", "image/jpeg" => "jpg", "image/png" => "png", _ => "bin" }; var objectKey = $"scans/{now.Year}/{now.Month:D2}/{batchId}/{hashHex}.{extension}"; await EnsureBucketAsync(ct); await _minio.PutObjectAsync(new PutObjectArgs() .WithBucket(_options.BucketName) .WithObject(objectKey) .WithStreamData(memStream) .WithObjectSize(memStream.Length) .WithContentType(contentType), ct); return (objectKey, hashHex, memStream.Length); } public async Task GetPresignedUrlAsync(string objectKey, CancellationToken ct = default) { ct.ThrowIfCancellationRequested(); return await _minio.PresignedGetObjectAsync(new PresignedGetObjectArgs() .WithBucket(_options.BucketName) .WithObject(objectKey) .WithExpiry(_options.PresignedUrlExpiryMinutes * 60)); } public async Task DownloadAsync(string objectKey, CancellationToken ct = default) { var memStream = new MemoryStream(); await _minio.GetObjectAsync(new GetObjectArgs() .WithBucket(_options.BucketName) .WithObject(objectKey) .WithCallbackStream(async (stream, cancellationToken) => { await stream.CopyToAsync(memStream, 81920, cancellationToken); }), ct); memStream.Position = 0; return memStream; } public async Task ObjectExistsAsync(string objectKey, CancellationToken ct = default) { try { await _minio.StatObjectAsync(new StatObjectArgs() .WithBucket(_options.BucketName) .WithObject(objectKey), ct); return true; } catch (Minio.Exceptions.ObjectNotFoundException) { return false; } } public async Task EnsureObjectAsync( string objectKey, byte[] content, string contentType, CancellationToken ct = default) { await EnsureBucketAsync(ct); if (await ObjectExistsAsync(objectKey, ct)) return; using var stream = new MemoryStream(content); await _minio.PutObjectAsync(new PutObjectArgs() .WithBucket(_options.BucketName) .WithObject(objectKey) .WithStreamData(stream) .WithObjectSize(stream.Length) .WithContentType(contentType), ct); } public async Task EnsureBucketAsync(CancellationToken ct = default) { if (Interlocked.CompareExchange(ref _bucketReady, 0, 0) == 1) return; var exists = await _minio.BucketExistsAsync( new BucketExistsArgs().WithBucket(_options.BucketName), ct); if (!exists) await _minio.MakeBucketAsync( new MakeBucketArgs().WithBucket(_options.BucketName), ct); Interlocked.Exchange(ref _bucketReady, 1); } }