示例#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   = db.Ads.Find(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();
            db.SaveChanges();
            Trace.TraceInformation("Updated thumbnail URL in database: {0}", ad.ThumbnailURL);

            // Remove message from queue.
            this.imagesQueue.DeleteMessage(msg);
        }
示例#2
0
        public static void GenerateThumbnail(
            [QueueTrigger("thumbnailrequest")] BlobInformation blobInfo,
            [Blob("images/{BlobName}", FileAccess.Read)] Stream input,
            [Blob("images/{BlobNameWithoutExtension}_thumbnail.jpg")] CloudBlockBlob outputBlob)
        {
            using (Stream output = outputBlob.OpenWrite())
            {
                ConvertImageToThumbnailJPG(input, output);
                outputBlob.Properties.ContentType = "image/jpeg";
            }

            // Entity Framework context class is not thread-safe, so it must
            // be instantiated and disposed within the function.
            using (ContosoAdsContext db = new ContosoAdsContext())
            {
                var id = blobInfo.AdId;
                Ad  ad = db.Ads.Find(id);
                if (ad == null)
                {
                    throw new Exception(String.Format("AdId {0} not found, can't create thumbnail", id.ToString()));
                }
                ad.ThumbnailURL = outputBlob.Uri.ToString();
                db.SaveChanges();
            }
        }
示例#3
0
        private void ProcessTimestampQueue(CloudQueueMessage msg)
        {
            Trace.TraceInformation("Processing queue message {0}", msg);

            var adId = int.Parse(msg.AsString);
            Ad  ad   = db.Ads.Find(adId);

            if (ad == null)
            {
                throw new Exception(String.Format("AdId {0} not found", adId.ToString()));
            }
            Uri            blobUri    = new Uri(ad.ImageURL);
            string         blobName   = blobUri.Segments[blobUri.Segments.Length - 1];
            CloudBlockBlob inputBlob  = this.imagesBlobContainer.GetBlockBlobReference(blobName);
            string         newName    = Path.GetFileNameWithoutExtension(inputBlob.Name) + "new.jpg";
            CloudBlockBlob outputBlob = this.imagesBlobContainer.GetBlockBlobReference(newName);

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

            ad.ImageURL = outputBlob.Uri.ToString();
            db.SaveChanges();
            Trace.TraceInformation("Updated timestamp URL in database: {0}", ad.ImageURL);

            this.timeQueue.DeleteMessage(msg);
        }