Skip to content

Commit

Permalink
Parallelize some calls in UploadPhoto
Browse files Browse the repository at this point in the history
  • Loading branch information
golf1052 committed Nov 30, 2024
1 parent c6e3ad0 commit c9e65e4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
54 changes: 45 additions & 9 deletions SeattleCarsInBikeLanes/Controllers/UploadController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,47 @@ public async Task<IActionResult> UploadPhoto([FromForm]List<IFormFile> files)

string submissionId = helperMethods.GetRandomFileName();

List<Task<InitialPhotoUploadWithSasUriMetadata>> metadataTasks = new List<Task<InitialPhotoUploadWithSasUriMetadata>>();
List<InitialPhotoUploadWithSasUriMetadata> metadata = new List<InitialPhotoUploadWithSasUriMetadata>();
Dictionary<int, string> exceptions = new Dictionary<int, string>();
for (int i = 0; i < files.Count; i++)
{
IFormFile? file = files[i];
try
{
InitialPhotoUploadWithSasUriMetadata data = await ProcessInitialUpload(file, submissionId, i);
metadata.Add(data);
metadataTasks.Add(ProcessInitialUpload(file, submissionId, i));
}
catch (Exception ex)
{
exceptions.Add(i, ex.Message);
}
}

await Task.WhenAll(metadataTasks);

for (int i = 0; i < metadataTasks.Count; i++)
{
var metadataTask = metadataTasks[i];
if (metadataTask.IsFaulted)
{
if (metadataTask.Exception != null)
{
if (exceptions.ContainsKey(i))
{
exceptions[i] += $"\n{metadataTask.Exception.Message}";
}
else
{
exceptions.Add(i, metadataTask.Exception.Message);
}
}
}
else
{
metadata.Add(metadataTask.Result);
}
}

if (exceptions.Count > 0)
{
// return a bad request with a string containing each exception and the photo number on a new line
Expand Down Expand Up @@ -146,21 +171,29 @@ private async Task<InitialPhotoUploadWithSasUriMetadata> ProcessInitialUpload(IF
VisualFeatureTypes.Adult
};

ImageAnalysis imageAnalysisResults = await computerVisionClient.AnalyzeImageInStreamAsync(fileStream2, visualFeatureTypes);
Task<ImageAnalysis> imageAnalysisResultsTask = computerVisionClient.AnalyzeImageInStreamAsync(fileStream2, visualFeatureTypes);
Task<ReverseSearchCrossStreetAddressResultItem?>? crossStreetItemTask = null;
if (photoLocation != null)
{
crossStreetItemTask = helperMethods.ReverseSearchCrossStreet(photoLocation, mapsSearchClient);
}

// No idea why this is not detected as null free since we're doing the filter
await Task.WhenAll(new List<Task?>() { imageAnalysisResultsTask, crossStreetItemTask }.Where(t => t != null)!);

fileStream2.Dispose();

ImageAnalysis imageAnalysisResults = imageAnalysisResultsTask.Result;
if (imageAnalysisResults.Adult.IsAdultContent || imageAnalysisResults.Adult.IsGoryContent || imageAnalysisResults.Adult.IsRacyContent)
{
throw new BikeLaneException("Error: Photo does not pass content check.");
}

string randomFileName = helperMethods.GetRandomFileName();

ReverseSearchCrossStreetAddressResultItem? crossStreetItem = null;
string? crossStreet = null;
if (photoLocation != null)
if (crossStreetItemTask != null)
{
crossStreetItem = await helperMethods.ReverseSearchCrossStreet(photoLocation, mapsSearchClient);
crossStreetItem = crossStreetItemTask.Result;
if (crossStreetItem == null)
{
throw new BikeLaneException("Error: Could not determine cross street.");
Expand All @@ -171,6 +204,7 @@ private async Task<InitialPhotoUploadWithSasUriMetadata> ProcessInitialUpload(IF
}
}

string randomFileName = helperMethods.GetRandomFileName();
InitialPhotoUploadMetadata metadata;
if (photoDate != null && photoLocation != null && crossStreet != null)
{
Expand All @@ -193,11 +227,13 @@ private async Task<InitialPhotoUploadWithSasUriMetadata> ProcessInitialUpload(IF

BlobClient photoBlobClient = blobContainerClient.GetBlobClient($"{InitialUploadPrefix}{randomFileName}.jpeg");
using var fileStream3 = System.IO.File.OpenRead(tempFile);
await photoBlobClient.UploadAsync(fileStream3);
Task uploadPhotoTask = photoBlobClient.UploadAsync(fileStream3);
fileStream3.Dispose();

BlobClient metadataBlobClient = blobContainerClient.GetBlobClient($"{InitialUploadPrefix}{randomFileName}.json");
await metadataBlobClient.UploadAsync(new BinaryData(metadata));
Task uploadMetadataTask = metadataBlobClient.UploadAsync(new BinaryData(metadata));

await Task.WhenAll(uploadPhotoTask, uploadMetadataTask);

Uri sasUri = await photoBlobClient.GenerateUserDelegationReadOnlySasUri(DateTimeOffset.UtcNow.AddMinutes(10));
return InitialPhotoUploadWithSasUriMetadata.FromMetadata(sasUri.ToString(), metadata);
Expand Down
4 changes: 2 additions & 2 deletions SeattleCarsInBikeLanes/wwwroot/dist/bluesky.bundle.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions SeattleCarsInBikeLanes/wwwroot/src/bskyAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as elementHelpers from './element-helpers';

const blueskySignInButton = document.getElementById('blueskySignInButton') as HTMLButtonElement;
const blueskyLogoutButton: HTMLAnchorElement = document.getElementById('blueskyLogoutButton') as HTMLAnchorElement;
const blueskyHandleInput: HTMLInputElement = document.getElementById('blueskyHandleInput') as HTMLInputElement;
const blueskyNextButton: HTMLButtonElement = document.getElementById('blueskyNextButton') as HTMLButtonElement;

let bskySub: string | null = null;
Expand Down Expand Up @@ -166,8 +167,7 @@ function login(handle: string) {
}

function loginWithBluesky() {
const blueskyHandle = document.getElementById('blueskyHandleInput') as HTMLInputElement;
const handle = blueskyHandle.value;
const handle = blueskyHandleInput.value;
elementHelpers.changeButtonToLoadingButton(blueskyNextButton, 'Login');
login(handle);
elementHelpers.changeLoadingButtonToRegularButton(blueskyNextButton, 'Login');
Expand All @@ -191,7 +191,7 @@ blueskyNextButton.addEventListener('click', () => {
loginWithBluesky();
});

blueskyNextButton.addEventListener('keydown', function(event) {
blueskyHandleInput.addEventListener('keydown', function(event) {
if (event.key === 'Enter') {
loginWithBluesky();
}
Expand Down

0 comments on commit c9e65e4

Please sign in to comment.