コード例 #1
0
        public void CreateTags(string photoId, Tag[] tags)
        {
            var context = new PhotoAlbumDataContext();

            foreach (var tag in tags)
            {
                // add the tag and associate to a picture for later searching
                context.AddObject(PhotoAlbumDataContext.TagTable, new TagRow(tag));
                context.AddObject(PhotoAlbumDataContext.PhotoTagTable, new PhotoTagRow(photoId, tag.Name));
            }

            try
            {
                // we want to continue - even if conflict is detected...
                context.SaveChanges(SaveChangesOptions.ContinueOnError);
            }
            catch (DataServiceRequestException ex)
            {
                foreach (var resp in ex.Response)
                {
                    // we might get a conflict here, which is ok (tag exists)
                    // the alternative is to query everytime to see if the tag
                    // exists, which is less efficient that just trying and
                    // handling exception.
                    if (resp.StatusCode != (int)HttpStatusCode.Conflict &&
                        resp.StatusCode != (int)HttpStatusCode.Created)
                    {
                        throw;
                    }
                }
            }
        }
コード例 #2
0
        public void CreateAlbum(string albumName, string owner)
        {
            var context = new PhotoAlbumDataContext();

            var album = new Album
            {
                AlbumId = SlugHelper.GetSlug(albumName),
                Owner   = owner.ToLowerInvariant(),
                Title   = albumName
            };

            context.AddObject(PhotoAlbumDataContext.AlbumTable, new AlbumRow(album));
            context.SaveChanges();
        }
コード例 #3
0
        public void Add(Photo photo, Stream binary, string mimeType, string name)
        {
            // get just the file name and ignore the path
            var file = name.Substring(name.LastIndexOf("\\", StringComparison.OrdinalIgnoreCase) + 1);

            var context = new PhotoAlbumDataContext();

            try
            {
                // add the photo to table storage
                context.AddObject(PhotoAlbumDataContext.PhotoTable, new PhotoRow(photo));
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                if (ex.ToString().Contains("EntityAlreadyExists"))
                {
                    throw new PhotoNameAlreadyInUseException(photo.AlbumId, photo.Title);
                }
                else
                {
                    throw;
                }
            }

            // add the binary to blob storage
            var storage   = this.storageAccount.CreateCloudBlobClient();
            var container = storage.GetContainerReference(photo.Owner.ToLowerInvariant());

            container.CreateIfNotExist();
            container.SetPermissions(new BlobContainerPermissions {
                PublicAccess = BlobContainerPublicAccessType.Blob
            });
            var blob = container.GetBlobReference(file);

            blob.Properties.ContentType = mimeType;
            blob.UploadFromStream(binary);

            // post a message to the queue so it can process tags and the sizing operations
            this.SendToQueue(
                Constants.PhotoQueue,
                string.Format(CultureInfo.InvariantCulture, "{0}|{1}|{2}|{3}", photo.Owner, photo.AlbumId, photo.PhotoId, file));
        }
コード例 #4
0
        public void Add(Photo photo, Stream binary, string mimeType, string name)
        {
            // get just the file name and ignore the path
            var file = name.Substring(name.LastIndexOf("\\", StringComparison.OrdinalIgnoreCase) + 1);

            var context = new PhotoAlbumDataContext();

            try
            {
                // add the photo to table storage
                context.AddObject(PhotoAlbumDataContext.PhotoTable, new PhotoRow(photo));
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                if (ex.ToString().Contains("EntityAlreadyExists"))
                {
                    throw new PhotoNameAlreadyInUseException(photo.AlbumId, photo.Title);
                }
                else
                {
                    throw;
                }
            }

            // add the binary to blob storage
            var storage = this.storageAccount.CreateCloudBlobClient();
            var container = storage.GetContainerReference(photo.Owner.ToLowerInvariant());
            container.CreateIfNotExist();
            container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
            var blob = container.GetBlobReference(file);

            blob.Properties.ContentType = mimeType;
            blob.UploadFromStream(binary);

            // post a message to the queue so it can process tags and the sizing operations
            this.SendToQueue(
                Constants.PhotoQueue,
                string.Format(CultureInfo.InvariantCulture, "{0}|{1}|{2}|{3}", photo.Owner, photo.AlbumId, photo.PhotoId, file));
        }
コード例 #5
0
        public void CreateAlbum(string albumName, string owner)
        {
            var context = new PhotoAlbumDataContext();

            var album = new Album
            {
                AlbumId = SlugHelper.GetSlug(albumName),
                Owner = owner.ToLowerInvariant(),
                Title = albumName
            };

            context.AddObject(PhotoAlbumDataContext.AlbumTable, new AlbumRow(album));
            context.SaveChanges();
        }
コード例 #6
0
        public void CreateTags(string photoId, Tag[] tags)
        {
            var context = new PhotoAlbumDataContext();

            foreach (var tag in tags)
            {
                // add the tag and associate to a picture for later searching
                context.AddObject(PhotoAlbumDataContext.TagTable, new TagRow(tag));
                context.AddObject(PhotoAlbumDataContext.PhotoTagTable, new PhotoTagRow(photoId, tag.Name));
            }

            try
            {
                // we want to continue - even if conflict is detected...
                context.SaveChanges(SaveChangesOptions.ContinueOnError);
            }
            catch (DataServiceRequestException ex)
            {
                foreach (var resp in ex.Response)
                {
                    // we might get a conflict here, which is ok (tag exists)
                    // the alternative is to query everytime to see if the tag
                    // exists, which is less efficient that just trying and 
                    // handling exception.
                    if (resp.StatusCode != (int)HttpStatusCode.Conflict
                        && resp.StatusCode != (int)HttpStatusCode.Created)
                    {
                        throw;
                    }
                }
            }
        }