Пример #1
0
        private void ProcessQueueMessage(CloudQueueMessage msg)
        {
            Trace.TraceInformation("Processing queue message {0}", msg);

            // Queue message contains AdId.
            var adId = int.Parse(msg.AsString);
            //Ad ad = Task.Run<Ad>(async () => await repository.FetchbyAdId(adId)).Result;
            Ad ad = repository.Ads.FirstOrDefault(p => p.AdId == adId);

            if (ad == null)
            {
                throw new Exception(String.Format("AdId {0} not found, can't create thumbnail", adId.ToString()));
            }

            Uri    blobUri  = new Uri(ad.ImageURL);
            string blobName = blobUri.Segments[blobUri.Segments.Length - 1];

            CloudBlockBlob inputBlob     = this.imagesBlobContainer.GetBlockBlobReference(blobName);
            string         thumbnailName = Path.GetFileNameWithoutExtension(inputBlob.Name) + "thumb.jpg";
            CloudBlockBlob outputBlob    = this.imagesBlobContainer.GetBlockBlobReference(thumbnailName);

            using (Stream input = inputBlob.OpenRead())
                using (Stream output = outputBlob.OpenWrite())
                {
                    ConvertImageToThumbnailJPG(input, output);
                    outputBlob.Properties.ContentType = "image/jpeg";
                }
            Trace.TraceInformation("Generated thumbnail in blob {0}", thumbnailName);

            ad.ThumbnailURL = outputBlob.Uri.ToString();
            // repository.updateAd(ad);
            Task.Run <int>(async() => await repository.updateAd(ad));
            Trace.TraceInformation("Updated thumbnail URL in database: {0}", ad.ThumbnailURL);

            // Remove message from queue.
            this.imagesQueue.DeleteMessage(msg);
        }