public static async Task Run(
            [EventGridTrigger] EventGridEvent eventGridEvent,
            [Blob("{data.url}", FileAccess.Read)] Stream input,
            ILogger log)
        {
            try
            {
                if (input != null)
                {
                    var createdEvent = ((JObject)eventGridEvent.Data).ToObject <StorageBlobCreatedEventData>();
                    var extension    = Path.GetExtension(createdEvent.Url);
                    var encoder      = SupportFuncs.GetEncoder(extension);

                    if (encoder != null)
                    {
                        Task <string> storeImgInfoTask = SupportFuncs.StoreImgInfo(createdEvent.Url, input);
                        await         storeImgInfoTask;
                        if (storeImgInfoTask.Result == "")
                        {
                            log.LogInformation($"Something went wrong trying to create ImgInfo.");
                        }
                    }
                    else
                    {
                        log.LogInformation($"No encoder support for: {createdEvent.Url}");
                    }
                }
            }
            catch (Exception ex)
            {
                log.LogInformation(ex.Message);
                throw;
            }
        }
        public static async Task Run(
            [EventGridTrigger] EventGridEvent eventGridEvent,
            [Blob("{data.url}", FileAccess.Read)] Stream input,
            ILogger log)
        {
            try
            {
                if (input != null)
                {
                    var createdEvent = ((JObject)eventGridEvent.Data).ToObject <StorageBlobCreatedEventData>();
                    var extension    = Path.GetExtension(createdEvent.Url);
                    var encoder      = SupportFuncs.GetEncoder(extension);

                    if (encoder != null)
                    {
                        var thumbnailWidth     = Convert.ToInt32(Environment.GetEnvironmentVariable("THUMBNAIL_WIDTH"));
                        var thumbContainerName = Environment.GetEnvironmentVariable("THUMBNAIL_CONTAINER_NAME");
                        var storageAccount     = CloudStorageAccount.Parse(BLOB_STORAGE_CONNECTION_STRING);
                        var blobClient         = storageAccount.CreateCloudBlobClient();
                        var container          = blobClient.GetContainerReference(thumbContainerName);
                        var blobName           = SupportFuncs.GetBlobNameFromUrl(createdEvent.Url);
                        var blockBlob          = container.GetBlockBlobReference(blobName);

                        using (var output = new MemoryStream())
                            using (Image <Rgba32> image = Image.Load(input))
                            {
                                var divisor = image.Width / thumbnailWidth;
                                var height  = Convert.ToInt32(Math.Round((decimal)(image.Height / divisor)));

                                image.Mutate(x => x.Resize(thumbnailWidth, height));
                                image.Save(output, encoder);
                                output.Position = 0;
                                await blockBlob.UploadFromStreamAsync(output);
                            }
                    }
                    else
                    {
                        log.LogInformation($"No encoder support for: {createdEvent.Url}");
                    }
                }
            }
            catch (Exception ex)
            {
                log.LogInformation(ex.Message);
                throw;
            }
        }
Esempio n. 3
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string imgname = req.Query["imgname"];

            string output = "";

            string connectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
            CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(connectionString);
            CloudBlobClient     blobClient          = cloudStorageAccount.CreateCloudBlobClient();
            string             containerName        = Environment.GetEnvironmentVariable("IMGINFO_CONTAINER_NAME");
            CloudBlobContainer container            = blobClient.GetContainerReference(containerName);
            CloudBlockBlob     blob = container.GetBlockBlobReference(imgname);

            Task <bool> existsTask = blob.ExistsAsync();

            existsTask.Wait();
            if (existsTask.Result)
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    await blob.DownloadToStreamAsync(stream);

                    stream.Position = 0;
                    StreamReader reader = new StreamReader(stream);
                    output = reader.ReadToEnd();
                }
            }
            else
            {
                //Generate info for image, if possible.
                string             imgContainerName = Environment.GetEnvironmentVariable("IMAGES_CONTAINER_NAME");
                CloudBlobContainer imgContainer     = blobClient.GetContainerReference(imgContainerName);
                CloudBlockBlob     imgBlob          = imgContainer.GetBlockBlobReference(imgname);
                if (await imgBlob.ExistsAsync())
                {
                    var input = new MemoryStream();
                    await imgBlob.DownloadToStreamAsync(input);

                    input.Position = 0;
                    string res = await SupportFuncs.StoreImgInfo(imgBlob.Uri.ToString(), input);

                    if (res == "")
                    {
                        output = "no info for image (tried to create it but failed)";
                    }
                    else
                    {
                        output = res;
                    }
                }
                else
                {
                    output = "no info for image";
                }
            }

            return
                (output != null
                ? (ActionResult) new OkObjectResult(output)
                : new BadRequestObjectResult("Please pass a name on the query string or in the request body")
                );
        }