コード例 #1
0
        public async Task <Result <AnalysisResult> > AnalyzeImageAsync(Image image, string username, string ipAddress, int failureCount)
        {
            var result = new AnalysisResult(new List <string>(), Constants.NoValueString, Constants.NoValueString);

            // Escape condition for recursive call if exception is thrown.
            if (failureCount >= Constants.OperationRetry)
            {
                return(SystemUtilityService.CreateResult(Constants.AnalyzationFailedMessage, result, true));
            }

            try
            {
                result = await _googleImageAnalysisService.AnalyzeAsync(image, Constants.ExogredientCategories).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                // Log exception.
                await _loggingManager.LogAsync(DateTime.UtcNow.ToString(Constants.LoggingFormatString),
                                               Constants.AnalyzeImageOperation, username, ipAddress, ex.ToString()).ConfigureAwait(false);

                // Recursively retry the operation until the maximum amount of retries is reached.
                await AnalyzeImageAsync(image, username, ipAddress, ++failureCount).ConfigureAwait(false);
            }


            // Log the fact that the operation was successful.
            await _loggingManager.LogAsync(DateTime.UtcNow.ToString(Constants.LoggingFormatString),
                                           Constants.AnalyzeImageOperation, username, ipAddress).ConfigureAwait(false);

            return(SystemUtilityService.CreateResult(Constants.AnalyzationSuccessMessage, result, false));
        }
コード例 #2
0
        public async Task <IActionResult> AnalyzeImageAsync(IFormCollection data, IFormFile formFile)
        {
            try
            {
                formFile.OpenReadStream();

                using var memoryStream = new MemoryStream();
                await formFile.CopyToAsync(memoryStream);

                var image = new Bitmap(memoryStream);

                Byte[] bytes;

                using (var ms = new MemoryStream())
                {
                    image.Save(ms, ImageFormat.Jpeg);

                    bytes = memoryStream.ToArray();
                }

                var img = Image.FromBytes(bytes);

                var result = await _uploadManager.AnalyzeImageAsync(img, data[Constants.UsernameKey], data[Constants.IPAddressKey], Constants.NoValueInt).ConfigureAwait(false);

                var suggestionsArray = result.Data.Suggestions.ToArray();


                return(Ok(new AnalyzeResponse()
                {
                    Message = result.Message,
                    ExceptionOccurred = result.ExceptionOccurred,
                    Suggestions = suggestionsArray,
                    Category = result.Data.Category,
                    Name = result.Data.Name
                }));
            }
            catch
            {
                // Return generic server error.
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }