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

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

            app.UseRouting();

            var eventReader = new CloudEventReader(logger);

            var                    configReader          = new ConfigReader(logger);
            var                    bucketExpected        = configReader.Read("BUCKET");
            IEventWriter           eventWriter           = configReader.ReadEventWriter(CloudEventSource, CloudEventType);
            IBucketEventDataReader bucketEventDataReader = configReader.ReadEventDataReader();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapPost("/", async context =>
                {
                    logger.LogInformation("Reading event");
                    var cloudEvent = await eventReader.Read(context);
                    logger.LogInformation("Getting Bucket");
                    var(bucket, name) = bucketEventDataReader.Read(cloudEvent);

                    // This is only needed in Cloud Run (Managed) when the
                    // events are not filtered by bucket yet.
                    if (bucket != bucketExpected)
                    {
                        logger.LogInformation($"Input bucket '{bucket}' does not match with expected bucket '{bucketExpected}'");
                        return;
                    }

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

                    var safe = await IsPictureSafe(storageUrl);
                    logger.LogInformation($"Is the picture safe? {safe}");

                    if (!safe)
                    {
                        return;
                    }

                    var replyData = JsonConvert.SerializeObject(new { bucket = bucket, name = name });
                    await eventWriter.Write(replyData, context);
                });
            });
        }
Esempio n. 2
0
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger <Startup> logger)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

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

            app.UseRouting();

            var eventReader = new CloudEventReader(logger);

            var configReader = new ConfigReader(logger);
            var outputBucket = configReader.Read("BUCKET");
            IBucketEventDataReader bucketEventDataReader = configReader.ReadEventDataReader();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapPost("/", async context =>
                {
                    try
                    {
                        var cloudEvent    = await eventReader.Read(context);
                        var(bucket, name) = bucketEventDataReader.Read(cloudEvent);

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

                        var labels = await ExtractLabelsAsync(storageUrl);
                        logger.LogInformation($"This picture is labelled: {labels}");

                        using (var outputStream = new MemoryStream(Encoding.UTF8.GetBytes(labels)))
                        {
                            var outputObjectName = $"{Path.GetFileNameWithoutExtension(name)}-labels.txt";
                            var client           = await StorageClient.CreateAsync();
                            await client.UploadObjectAsync(outputBucket, outputObjectName, "text/plain", outputStream);
                            logger.LogInformation($"Uploaded '{outputObjectName}' to bucket '{outputBucket}'");
                        }
                    }
                    catch (Exception e)
                    {
                        logger.LogError($"Error processing: " + e.Message);
                        throw e;
                    }
                });
            });
        }
Esempio n. 3
0
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger <Startup> logger)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

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

            app.UseRouting();

            var eventReader = new CloudEventReader(logger);

            var                    configReader          = new ConfigReader(logger);
            var                    outputBucket          = configReader.Read("BUCKET");
            IEventWriter           eventWriter           = configReader.ReadEventWriter(CloudEventSource, CloudEventType);
            IBucketEventDataReader bucketEventDataReader = configReader.ReadEventDataReader();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapPost("/", async context =>
                {
                    try
                    {
                        var cloudEvent    = await eventReader.Read(context);
                        var(bucket, name) = bucketEventDataReader.Read(cloudEvent);

                        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);
                                }

                                var outputObjectName = $"{Path.GetFileNameWithoutExtension(name)}-{ThumbWidth}x{ThumbHeight}.png";
                                await client.UploadObjectAsync(outputBucket, outputObjectName, "image/png", outputStream);
                                logger.LogInformation($"Uploaded '{outputObjectName}' to bucket '{outputBucket}'");

                                var replyData = JsonConvert.SerializeObject(new { bucket = outputBucket, name = outputObjectName });
                                await eventWriter.Write(replyData, context);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        logger.LogError($"Error processing: " + e.Message);
                        throw e;
                    }
                });
            });
        }
Esempio n. 4
0
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger <Startup> logger)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

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

            app.UseRouting();

            var eventReader = new CloudEventReader(logger);

            var configReader = new ConfigReader(logger);
            var outputBucket = configReader.Read("BUCKET");
            IBucketEventDataReader bucketEventDataReader = configReader.ReadEventDataReader();

            var fontCollection = new FontCollection();

            fontCollection.Install("Arial.ttf");
            var font = fontCollection.CreateFont("Arial", 10);

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapPost("/", async context =>
                {
                    try
                    {
                        var cloudEvent    = await eventReader.Read(context);
                        var(bucket, name) = bucketEventDataReader.Read(cloudEvent);

                        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 (var image = Image.Load(inputStream))
                                {
                                    using (var imageProcessed = image.Clone(ctx => ApplyScalingWaterMarkSimple(ctx, font, Watermark, Color.DeepSkyBlue, 5)))
                                    {
                                        logger.LogInformation($"Added watermark to image '{name}'");
                                        imageProcessed.SaveAsJpeg(outputStream);
                                    }
                                }

                                var outputObjectName = $"{Path.GetFileNameWithoutExtension(name)}-watermark.jpeg";
                                await client.UploadObjectAsync(outputBucket, outputObjectName, "image/jpeg", outputStream);
                                logger.LogInformation($"Uploaded '{outputObjectName}' to bucket '{outputBucket}'");
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        logger.LogError($"Error processing: " + e.Message);
                        throw e;
                    }
                });
            });
        }