private bool CleanupPhoto(CloudQueueMessage msg) { Trace.TraceInformation("CleanupPhoto called with {0}", msg.AsString); var parts = msg.AsString.Split('|'); if (parts.Length != 6) { Trace.TraceError("Unexpected input to the photo cleanup queue: {0}", msg.AsString); return false; } try { // interpret the string var photoid = parts[0]; var owner = parts[1]; var url = parts[2]; var rawTags = parts[3]; var thumbnailUrl = parts[4]; var albumId = parts[5]; // the photoRow is already deleted by the frontend to remove from view // now we need to clean the binaries and the tag information var repository = new PhotoRepository(); repository.UpdateAlbumData(owner, albumId, thumbnailUrl); // this cleans up the tag to photo relationship. we will intentionally not // remove the tag however in here since it doesn't matter var tags = rawTags.Split(';') .Where(s => s.Trim().Length > 0) .Select(s => new Tag() { Name = s.Trim().ToLowerInvariant() }) .ToArray(); repository.RemoveTags(photoid, tags); // next, let's remove the blobs from storage var filename = Path.GetFileName(url); var thumbname = Path.Combine("thumb", filename); if (!string.IsNullOrEmpty(filename)) { Trace.TraceWarning("Attempting to delete {0}", filename); var client = this.storageAccount.CreateCloudBlobClient(); var container = client.GetContainerReference(owner); var blobGone = container.GetBlobReference(filename).DeleteIfExists(); var thumbGone = container.GetBlobReference(thumbname).DeleteIfExists(); if (!blobGone || !thumbGone) { Trace.TraceWarning(string.Format(CultureInfo.InvariantCulture, "Failed to {0}", blobGone ? "Kill Thumb" : thumbGone ? "Kill both" : "Kill blob")); } } } catch (Exception ex) { Trace.TraceError("Cleanup Photo Failure"); Trace.TraceError(ex.ToString()); return false; } return true; }
private bool CreateThumbnail(CloudQueueMessage msg) { Trace.TraceInformation("CreateThumbnail called with {0}", msg.AsString); var parts = msg.AsString.Split('|'); if (parts.Length != 4) { Trace.TraceError("Unexpected input to the photo cleanup queue: {0}", msg.AsString); return false; } // interpret the string var owner = parts[0]; var albumName = parts[1]; var photoid = parts[2]; var file = parts[3]; var repository = new PhotoRepository(); var photo = repository.GetPhotoByOwner(owner, albumName, photoid); if (photo != null) { // create the thumbnail try { this.CreateThumb(owner, file); } catch (Exception ex) { // creating the thumbnail failed for some reason Trace.TraceError("CreateThumb failed for {0} and {1}", owner, file); Trace.TraceError(ex.ToString()); return false; // bail out } // update table storage with blob data URLs var client = this.storageAccount.CreateCloudBlobClient(); var blobUri = client.GetContainerReference(owner).GetBlobReference(file).Uri.ToString(); var thumbUri = client.GetContainerReference(owner).GetBlobReference(Path.Combine("thumb", file)).Uri.ToString(); // update the photo entity with thumb and blob location photo.ThumbnailUrl = thumbUri; photo.Url = blobUri; repository.UpdatePhotoData(photo); var album = repository.GetAlbumsByOwner(owner).Single(a => a.AlbumId == albumName); if (!album.HasPhotos || string.IsNullOrEmpty(album.ThumbnailUrl)) { // update the album album.HasPhotos = true; album.ThumbnailUrl = photo.ThumbnailUrl; repository.UpdateAlbum(album); } // parse the tags and save them off var tags = photo.RawTags.Split(';') .Where(s => s.Trim().Length > 0) .Select(s => new Tag { Name = s.Trim().ToLowerInvariant() }) .ToArray(); repository.CreateTags(photoid, tags); // TODO, aggregate stats return true; } // default Trace.TraceError("CreateThumbnail error, cannot find {0}", photoid); return false; }
private static bool CleanupAlbum(CloudQueueMessage msg) { Trace.TraceInformation("CleanupAlbum called with {0}", msg.AsString); var parts = msg.AsString.Split('|'); if (parts.Length != 2) { Trace.TraceError("Unexpected input to the album cleanup queue: {0}", msg.AsString); return false; } // interpret the message var owner = parts[0]; var album = parts[1]; var repository = new PhotoRepository(); var photos = repository.GetPhotosByAlbum(owner, album); // this will trigger another message to the queue for more scale! foreach (var photo in photos) { repository.Delete(photo); } return true; }