Harden API memory and CPU usage across Phases 19–22.
CI / changes (push) Successful in 17s
dotnet8-ci / build-and-test (push) Successful in 8m23s
CI / backend (push) Successful in 8m24s
node-ci / build (push) Failing after 2m1s
CI / frontend (push) Failing after 2m2s

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:
2026-11-30 23:55:56 +08:00
parent 192cb36dc7
commit a17bc2b734
46 changed files with 1677 additions and 215 deletions
@@ -146,32 +146,41 @@ public class DigitizationBatchesController : ControllerBase
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetDocument(Guid id)
{
var batch = await _batches.GetByIdAsync(id);
var batchMeta = await _db.DigitizationBatches
.AsNoTracking()
.Where(b => b.Id == id)
.Select(b => new { b.DocumentRef })
.FirstOrDefaultAsync();
if (batch.DocumentRef == "live-capture")
if (batchMeta is null)
return NotFound(ApiResponse<object>.Fail(404, "Batch not found.", "BATCH_NOT_FOUND"));
if (batchMeta.DocumentRef == "live-capture" || string.IsNullOrEmpty(batchMeta.DocumentRef))
return NotFound(ApiResponse<object>.Fail(
404, "This batch has no scanned document.", "NO_DOCUMENT"));
var scanned = await _db.ScannedDocuments
var contentType = await _db.ScannedDocuments
.AsNoTracking()
.FirstOrDefaultAsync(d => d.BatchId == id);
var contentType = scanned?.ContentType ?? "application/octet-stream";
.Where(d => d.BatchId == id)
.Select(d => d.ContentType)
.FirstOrDefaultAsync() ?? "application/octet-stream";
await RecordDocumentAccessAsync(id);
try
{
if (!await _storage.ObjectExistsAsync(batch.DocumentRef))
return NotFound(ApiResponse<object>.Fail(
404,
"Scanned document is missing from storage. Re-upload the batch or restore the object.",
"DOCUMENT_NOT_FOUND"));
var stream = await _storage.DownloadAsync(batch.DocumentRef);
var stream = await _storage.DownloadAsync(
batchMeta.DocumentRef, HttpContext.RequestAborted);
Response.Headers.CacheControl = "private, max-age=300";
return File(stream, contentType);
}
catch (Minio.Exceptions.ObjectNotFoundException)
{
return NotFound(ApiResponse<object>.Fail(
404,
"Scanned document is missing from storage. Re-upload the batch or restore the object.",
"DOCUMENT_NOT_FOUND"));
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
return StatusCode(503, ApiResponse<object>.Fail(