private async Task DeleteAdBlobsAsync(Ad ad) { if (!string.IsNullOrWhiteSpace(ad.ImageURL)) { Trace.TraceInformation("Deleting image blob {0}", ad.ImageURL); CloudBlockBlob blobToDelete = imagesBlobContainer.GetBlockBlobReference(ad.GetImageFileName()); await blobToDelete.DeleteAsync(); } if (!string.IsNullOrWhiteSpace(ad.ThumbnailURL)) { Trace.TraceInformation("Deleting thumbnail blob {0}", ad.ThumbnailURL); CloudBlockBlob blobToDelete = imagesBlobContainer.GetBlockBlobReference(ad.GetThumbnailFileName()); await blobToDelete.DeleteAsync(); } }
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())); } CloudBlockBlob inputBlob = this.imagesBlobContainer.GetBlockBlobReference(ad.GetImageFileName()); 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); }