Exemplo n.º 1
0
        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);
        }