Skip to content

Commit

Permalink
Update UploadFile.cs
Browse files Browse the repository at this point in the history
stream rather than write the inbound image to storage
  • Loading branch information
gloveboxes committed Apr 19, 2024
1 parent 004bf01 commit a93a893
Showing 1 changed file with 9 additions and 22 deletions.
31 changes: 9 additions & 22 deletions src/api/NewPatient/UploadFile.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Azure;
using Azure.AI.FormRecognizer.DocumentAnalysis;
using Azure.Storage.Blobs;
using Azure.Storage.Sas;
using Contoso.Healthcare.Api.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -19,7 +17,6 @@ public static class UploadFile
[FunctionName(nameof(UploadFile))]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "new-patient/upload-file")] HttpRequest req,
[Blob("unprocessed-patient-forms", Connection = "NEW_PATIENT_STORAGE")] BlobContainerClient containerClient,
[CosmosDB(
databaseName: "patientDb",
containerName: "patientContainer",
Expand All @@ -29,15 +26,20 @@ public static async Task<IActionResult> Run(
var formData = await req.ReadFormAsync();
var file = formData.Files["file"];

var (patientId, filename) = await StoreFile(file, containerClient);
var outputs = await ExtractFormInfo(containerClient, filename);
if (file == null)
{
return new BadRequestObjectResult("File not found");
}

string patientId = Guid.NewGuid().ToString();
var outputs = await ExtractFormInfo(file);

await formResponse.AddAsync(new FormRecognizerResponse(FormRecognizerResponse.GetId(patientId), outputs));

return new OkObjectResult(new { PatientId = patientId });
}

private static async Task<Dictionary<string, (string, float?)>> ExtractFormInfo(BlobContainerClient containerClient, string filename)
private static async Task<Dictionary<string, (string, float?)>> ExtractFormInfo(IFormFile file)
{
string? endpoint = Environment.GetEnvironmentVariable("FORM_RECOGNIZER_ENDPOINT");
string? apiKey = Environment.GetEnvironmentVariable("FORM_RECOGNIZER_API_KEY");
Expand All @@ -51,10 +53,7 @@ public static async Task<IActionResult> Run(
var credential = new AzureKeyCredential(apiKey);
var client = new DocumentAnalysisClient(new Uri(endpoint), credential);

var blobClient = containerClient.GetBlobClient(filename);
var uri = blobClient.GenerateSasUri(BlobSasPermissions.Read, DateTimeOffset.UtcNow.AddMinutes(5));

AnalyzeDocumentOperation operation = await client.AnalyzeDocumentFromUriAsync(WaitUntil.Completed, modelId, uri);
AnalyzeDocumentOperation operation = await client.AnalyzeDocumentAsync(WaitUntil.Completed, modelId, file.OpenReadStream());
AnalyzeResult result = operation.Value;

var outputs = new Dictionary<string, (string, float?)>();
Expand All @@ -69,16 +68,4 @@ public static async Task<IActionResult> Run(

return outputs;
}

private static async Task<(string, string)> StoreFile(IFormFile file, BlobContainerClient containerClient)
{
_ = await containerClient.CreateIfNotExistsAsync();

var patientId = Guid.NewGuid().ToString();

var name = $"{patientId}_{file.FileName}";
_ = await containerClient.UploadBlobAsync(name, file.OpenReadStream());

return (patientId, name);
}
}

0 comments on commit a93a893

Please sign in to comment.