in packages/ekyc-api/src/ekyc-api/Controllers/DataController.cs [120:190]
public async Task<string> GetFieldValues(S3DataRequest request)
{
AWSXRayRecorder.Instance.BeginSubsegment("DataController:GetFieldValues");
if (request == null)
throw new HttpStatusException(HttpStatusCode.BadRequest, "Request cannot be blank.");
var manager = new DataRequestManager(_config, _amazonS3, _dynamoDb);
if (request.RequestId == null ||
!manager.DataRequestExistsAndIsValid(request.RequestId).GetAwaiter().GetResult())
throw new HttpStatusException(HttpStatusCode.BadRequest,
"Request is invalid - did you create a new request first?");
if (string.IsNullOrEmpty(request.s3Key))
throw new HttpStatusException(HttpStatusCode.BadRequest, "S3Key cannot be blank.");
DocumentTypes docType;
var s3Key = request.RequestId + "/" + request.s3Key;
if (!string.IsNullOrEmpty(request.documentType))
{
if (!Enum.TryParse(request.documentType, out docType))
throw new HttpStatusException(HttpStatusCode.BadRequest,
$"{request.documentType} is not a valid document type.");
}
else
{
var detectResponse = await new Imaging(_amazonS3, _documentChecker).DetectAndCropDocument(s3Key);
// The detection may fail because there isn't a Rekognition Custom Labels project running, or it hasn't been trained
if (detectResponse.HasValue)
{
// We may need to crop out the background by detecting the bounding box of the document
// Save the cropped image to S3
s3Key = request.RequestId + "/" + Guid.NewGuid().ToString() + ".png";
await _amazonS3.PutObjectAsync(new PutObjectRequest()
{
BucketName = Globals.StorageBucket,
AutoCloseStream = true,
Key = s3Key,
InputStream = detectResponse.Value.CroppedImage
});
docType = detectResponse.Value.documentType;
}
else
{
if (string.IsNullOrEmpty(request.documentType))
throw new HttpStatusException(HttpStatusCode.BadRequest,
"Unable to infer document type, so DocumentType in the request cannot be blank.");
if (!Enum.TryParse(request.documentType, out docType))
throw new HttpStatusException(HttpStatusCode.BadRequest,
$"{request.documentType} is not a valid document type.");
}
}
var documentDefinition = await _factory.GetDocumentDefinition(docType);
var fieldData = await documentDefinition.GetFieldData(s3Key, docType);
AWSXRayRecorder.Instance.EndSubsegment();
return JsonSerializer.Serialize(fieldData);
}