public static async Task Run( [QueueTrigger("queue-resizeimages", Connection = "AzureWebJobsStorage")] string imageToResize, TraceWriter log) { log.Info($"Queue trigger function processing: {imageToResize}"); // Resize the image using Cognitive Services var resizedImagePath = await SmartResizeImage.MakeThumbNailRequest(imageToResize); // Get the previous info of the wolf image from table storage. All I'm doing is changing partition key // and entering the URL of the resized image log.Info("Wolf image resized"); var wolfImageInfoResizing = await AzureStorageHelper.GetPictureInfo( WolfImageEntity.PartitionKey_AwaitResizing, Path.GetFileNameWithoutExtension(imageToResize)); var wolfImageInfo = new WolfImageEntity { PartitionKey = WolfImageEntity.PartitionKey_WolfImage, ResizedFileUrl = resizedImagePath, OriginalFileUrl = wolfImageInfoResizing.OriginalFileUrl, WolfConfidence = wolfImageInfoResizing.WolfConfidence, IsAdult = wolfImageInfoResizing.IsAdult, IsWolf = wolfImageInfoResizing.IsWolf, RowKey = wolfImageInfoResizing.RowKey, IsRacy = wolfImageInfoResizing.IsRacy, ImageDescription = wolfImageInfoResizing.ImageDescription, ImageDescriptionConfidence = wolfImageInfoResizing.ImageDescriptionConfidence, UserName = wolfImageInfoResizing.UserName, CognitiveJson = wolfImageInfoResizing.CognitiveJson, Id = wolfImageInfoResizing.Id }; // Write info table storage. This is the final version log.Info("Inserting final table storage record about the image"); await AzureStorageHelper.InsertPictureInfo(wolfImageInfo); log.Info("Wolf image successfully resized"); }
public static async Task <HttpResponseMessage> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req, TraceWriter log) { log.Info("Starting process of uploading a Wolf image"); if (!req.Content.IsMimeMultipartContent()) { log.Error("Please pass a name in the request body"); return(req.CreateResponse(HttpStatusCode.UnsupportedMediaType, "Please pass a name in the request body")); } // Confirm the JWT is valid var tokenResult = await SecurityJWT.ValidateTokenAsync(req.Headers.Authorization); if (tokenResult == null) { log.Error("Invalid JWT"); return(req.CreateResponse(HttpStatusCode.Unauthorized)); } log.Info("JWT is all good"); // Extract the username from the token var userName = tokenResult.FindFirst(SecurityJWT.Predicate).Value; // Get file info from the request var provider = new MultipartFormDataStreamProvider(Path.GetTempPath()); await req.Content.ReadAsMultipartAsync(provider); if (provider.FileData.Count != 1) { return(req.CreateErrorResponse(HttpStatusCode.BadRequest, "Please only submit one file")); } // Parse the connection string and return a reference to the storage account log.Info("Uploading image to blob storage"); var blockBlob = await AzureStorageHelper.UploadWolfImageAsync(provider.FileData[0]); // Run the cognitive services vision API to confirm whether or not it's a wolf. This can take a few seconds log.Info("Submitting request for image analysis"); var analysisResponse = await AnalyseImage.MakeAnalysisRequest(blockBlob.Uri.ToString(), log); // Write info about the image to table storage log.Info("Writing info in table storage"); var id = Path.GetFileNameWithoutExtension(blockBlob.Uri.ToString()); var entity = new WolfImageEntity(analysisResponse.Response.IsWolf, id) { UserName = userName, Id = id, CognitiveJson = analysisResponse.CognitiveJson, WolfConfidence = analysisResponse.Response.WolfConfidence, ImageDescription = analysisResponse.Response.ImageDescription, ImageDescriptionConfidence = analysisResponse.Response.ImageDescriptionConfidence, IsAdult = analysisResponse.Response.IsAdult, IsRacy = analysisResponse.Response.IsRacy, OriginalFileUrl = blockBlob.Uri.ToString() }; await AzureStorageHelper.InsertPictureInfo(entity); if (!analysisResponse.Response.IsWolf || analysisResponse.Response.IsAdult || analysisResponse.Response.IsRacy) { // Please note: // ADULT = are things kids shouldn't see, such as violence // RACY = something like a Victoria Secrets (lingerie) model scores very high. Trust me. I tested. Extensively. log.Info("Image was not a wolf"); } else { log.Info("Image was a wolf"); // It's a wolf image so submit a message in the queue so that we can resize the image with Cognitive Services. // The main reason for queuing this, instead of using blob triggers, is that I want a steady rate of // processing as I have a free account and therefore limited resource. I don't mind if it takes a bit longer. await AzureStorageHelper.QueueImageResize(blockBlob.Uri.ToString()); } // Even though I return a 200 it's possible that the image is not a wolf. Will leave it to the UI layer to // break the news that their dog picture won't make it to the list return(req.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(analysisResponse.Response))); }