public void Configure()

in services/thumbnails/csharp/Startup.cs [40:117]


        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            logger.LogInformation("Service is starting...");

            app.UseRouting();

            var thumbBucket = GetEnvironmentVariable("BUCKET_THUMBNAILS");
            var projectId = GetEnvironmentVariable("PROJECT_ID");

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapPost("/", async context =>
                {
                    JToken pubSubMessage;
                    using (var reader = new StreamReader(context.Request.Body))
                    {
                        pubSubMessage = JValue.Parse(await reader.ReadToEndAsync());
                        logger.LogInformation("PubSub message: " + pubSubMessage);
                    }

                    var eventType = (string)pubSubMessage["message"]["attributes"]["eventType"];
                    var data = (string)pubSubMessage["message"]["data"];
                    var fileEvent = JValue.Parse(Encoding.UTF8.GetString(Convert.FromBase64String(data)));

                    if (eventType != "OBJECT_FINALIZE")
                    {
                        logger.LogInformation($"Event type {eventType} is not OBJECT_FINALIZE. Skipping file.");
                        return;
                    }

                    logger.LogInformation("Base 64 decoded file event: " + fileEvent);

                    var bucket = (string)fileEvent["bucket"];
                    var name = (string)fileEvent["name"];

                    using (var inputStream = new MemoryStream())
                    {
                        var client = await StorageClient.CreateAsync();
                        await client.DownloadObjectAsync(bucket, name, inputStream);
                        logger.LogInformation($"Downloaded '{name}' 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 '{name}' to {ThumbWidth}x{ThumbHeight}");

                                image.SaveAsPng(outputStream);
                            }

                            await client.UploadObjectAsync(thumbBucket, name, "image/png", outputStream);
                            logger.LogInformation($"Uploaded '{name}' to bucket '{thumbBucket}'");

                            var firestore = await FirestoreDb.CreateAsync(projectId);
                            var pictureStore = firestore.Collection("pictures");
                            var doc = pictureStore.Document(name);
                            var metadata = new Dictionary<string, object>()
                            {
                                {"thumbnail", true},
                            };
                            await doc.SetAsync(metadata, SetOptions.MergeAll);

                            logger.LogInformation($"Updated Firestore about thumbnail creation for {name}");
                        }
                    }

                });
            });
        }