Harden API memory and CPU usage across Phases 19–22.
Bound list/PDF payloads, stream MinIO I/O with cancellation, paginate history and FHIR $everything, tighten OCR lifetimes/buffers, and add phase tracking docs.
This commit is contained in:
@@ -7,6 +7,8 @@ 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<MinioOptions> options)
|
||||
{
|
||||
@@ -15,17 +17,21 @@ public class DocumentStorageService : IDocumentStorageService
|
||||
}
|
||||
|
||||
public async Task<(string objectKey, string sha256, long fileSize)> UploadAsync(
|
||||
Stream fileStream, string contentType, Guid batchId)
|
||||
Stream fileStream, string contentType, Guid batchId, CancellationToken ct = default)
|
||||
{
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
using var sha256 = SHA256.Create();
|
||||
using var sha256 = IncrementalHash.CreateHash(HashAlgorithmName.SHA256);
|
||||
using var memStream = new MemoryStream();
|
||||
|
||||
await fileStream.CopyToAsync(memStream);
|
||||
memStream.Position = 0;
|
||||
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 hashBytes = sha256.ComputeHash(memStream);
|
||||
var hashHex = Convert.ToHexString(hashBytes).ToLowerInvariant();
|
||||
var hashHex = Convert.ToHexString(sha256.GetHashAndReset()).ToLowerInvariant();
|
||||
memStream.Position = 0;
|
||||
|
||||
var extension = contentType switch
|
||||
@@ -38,45 +44,49 @@ public class DocumentStorageService : IDocumentStorageService
|
||||
|
||||
var objectKey = $"scans/{now.Year}/{now.Month:D2}/{batchId}/{hashHex}.{extension}";
|
||||
|
||||
await EnsureBucketAsync();
|
||||
await EnsureBucketAsync(ct);
|
||||
|
||||
await _minio.PutObjectAsync(new PutObjectArgs()
|
||||
.WithBucket(_options.BucketName)
|
||||
.WithObject(objectKey)
|
||||
.WithStreamData(memStream)
|
||||
.WithObjectSize(memStream.Length)
|
||||
.WithContentType(contentType));
|
||||
.WithContentType(contentType), ct);
|
||||
|
||||
return (objectKey, hashHex, memStream.Length);
|
||||
}
|
||||
|
||||
public async Task<string> GetPresignedUrlAsync(string objectKey)
|
||||
public async Task<string> 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<Stream> DownloadAsync(string objectKey)
|
||||
public async Task<Stream> DownloadAsync(string objectKey, CancellationToken ct = default)
|
||||
{
|
||||
var memStream = new MemoryStream();
|
||||
await _minio.GetObjectAsync(new GetObjectArgs()
|
||||
.WithBucket(_options.BucketName)
|
||||
.WithObject(objectKey)
|
||||
.WithCallbackStream(stream => stream.CopyTo(memStream)));
|
||||
.WithCallbackStream(async (stream, cancellationToken) =>
|
||||
{
|
||||
await stream.CopyToAsync(memStream, 81920, cancellationToken);
|
||||
}), ct);
|
||||
|
||||
memStream.Position = 0;
|
||||
return memStream;
|
||||
}
|
||||
|
||||
public async Task<bool> ObjectExistsAsync(string objectKey)
|
||||
public async Task<bool> ObjectExistsAsync(string objectKey, CancellationToken ct = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _minio.StatObjectAsync(new StatObjectArgs()
|
||||
.WithBucket(_options.BucketName)
|
||||
.WithObject(objectKey));
|
||||
.WithObject(objectKey), ct);
|
||||
return true;
|
||||
}
|
||||
catch (Minio.Exceptions.ObjectNotFoundException)
|
||||
@@ -85,10 +95,11 @@ public class DocumentStorageService : IDocumentStorageService
|
||||
}
|
||||
}
|
||||
|
||||
public async Task EnsureObjectAsync(string objectKey, byte[] content, string contentType)
|
||||
public async Task EnsureObjectAsync(
|
||||
string objectKey, byte[] content, string contentType, CancellationToken ct = default)
|
||||
{
|
||||
await EnsureBucketAsync();
|
||||
if (await ObjectExistsAsync(objectKey))
|
||||
await EnsureBucketAsync(ct);
|
||||
if (await ObjectExistsAsync(objectKey, ct))
|
||||
return;
|
||||
|
||||
using var stream = new MemoryStream(content);
|
||||
@@ -97,13 +108,20 @@ public class DocumentStorageService : IDocumentStorageService
|
||||
.WithObject(objectKey)
|
||||
.WithStreamData(stream)
|
||||
.WithObjectSize(stream.Length)
|
||||
.WithContentType(contentType));
|
||||
.WithContentType(contentType), ct);
|
||||
}
|
||||
|
||||
private async Task EnsureBucketAsync()
|
||||
public async Task EnsureBucketAsync(CancellationToken ct = default)
|
||||
{
|
||||
var exists = await _minio.BucketExistsAsync(new BucketExistsArgs().WithBucket(_options.BucketName));
|
||||
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));
|
||||
await _minio.MakeBucketAsync(
|
||||
new MakeBucketArgs().WithBucket(_options.BucketName), ct);
|
||||
|
||||
Interlocked.Exchange(ref _bucketReady, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user