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:
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user