public async Task HandleAsync(HttpContext context)
        {
            _logger.LogInformation("Function received request");

            try
            {
                var(bucket, file) = await _requestReader.ReadCloudStorageData(context);

                var storageUrl = $"gs://{bucket}/{file}";
                _logger.LogInformation($"Storage url: {storageUrl}");

                var safe = await IsPictureSafe(storageUrl);

                _logger.LogInformation($"Is the picture safe? {safe}");

                var replyData = new { safe = safe };
                var json      = JsonConvert.SerializeObject(replyData);
                _logger.LogInformation($"Replying back with json: {json}");

                context.Response.ContentType = "application/json";
                await context.Response.WriteAsync(json);
            }
            catch (Exception e)
            {
                _logger.LogError($"Error processing: " + e.Message);
                throw e;
            }
        }
예제 #2
0
        public async Task HandleAsync(HttpContext context)
        {
            _logger.LogInformation("Function received request");

            try
            {
                var(bucket, file) = await _requestReader.ReadCloudStorageData(context);

                using (var inputStream = new MemoryStream())
                {
                    var client = await StorageClient.CreateAsync();

                    await client.DownloadObjectAsync(bucket, file, inputStream);

                    _logger.LogInformation($"Downloaded '{file}' from bucket '{bucket}'");

                    using (var outputStream = new MemoryStream())
                    {
                        inputStream.Position = 0; // Reset to read
                        using (Image image = Image.Load(inputStream))
                        {
                            image.Mutate(x => x
                                         .Resize(ThumbWidth, ThumbHeight)
                                         );
                            _logger.LogInformation($"Resized image '{file}' to {ThumbWidth}x{ThumbHeight}");

                            image.SaveAsPng(outputStream);
                        }

                        var outputFile = $"{Path.GetFileNameWithoutExtension(file)}-{ThumbWidth}x{ThumbHeight}.png";
                        await client.UploadObjectAsync(_outputBucket, outputFile, "image/png", outputStream);

                        _logger.LogInformation($"Uploaded '{outputFile}' to bucket '{_outputBucket}'");

                        var replyData = new { bucket = _outputBucket, file = outputFile };
                        var json      = JsonConvert.SerializeObject(replyData);
                        _logger.LogInformation($"Replying back with json: {json}");

                        context.Response.ContentType = "application/json";
                        await context.Response.WriteAsync(json);
                    }
                }
            }
            catch (Exception e)
            {
                _logger.LogError($"Error processing: " + e.Message);
                throw e;
            }
        }
예제 #3
0
        public async Task HandleAsync(HttpContext context)
        {
            _logger.LogInformation("Function received request");

            try
            {
                var(bucket, file) = await _requestReader.ReadCloudStorageData(context);

                var storageUrl = $"gs://{bucket}/{file}";
                _logger.LogInformation($"Storage url: {storageUrl}");

                var labels = await ExtractLabelsAsync(storageUrl);

                var allLabels = string.Join(",", labels);
                _logger.LogInformation($"This picture is labelled: {allLabels}");

                using (var outputStream = new MemoryStream(Encoding.UTF8.GetBytes(allLabels)))
                {
                    var outputObjectName = $"{Path.GetFileNameWithoutExtension(file)}-labels.txt";
                    var client           = await StorageClient.CreateAsync();

                    await client.UploadObjectAsync(_outputBucket, outputObjectName, "text/plain", outputStream);

                    _logger.LogInformation($"Uploaded '{outputObjectName}' to bucket '{_outputBucket}'");
                }

                var topThreeLabels = string.Join(",", labels.Take(3));
                var replyData      = new { labels = topThreeLabels };
                var json           = JsonConvert.SerializeObject(replyData);
                _logger.LogInformation($"Replying back with json: {json}");

                context.Response.ContentType = "application/json";
                await context.Response.WriteAsync(json);
            }
            catch (Exception e)
            {
                _logger.LogError($"Error processing: " + e.Message);
                throw e;
            }
        }