/// <summary>
        ///     Creates the thumbnail image entity.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="originalImageEntity">The original image entity.</param>
        /// <param name="fileExtension">The file extension.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="scaleEnum">The scale enum.</param>
        /// <returns></returns>
        private ThumbnailFileType CreateThumbnailImageEntity(Stream stream, ImageFileType originalImageEntity, string fileExtension,
                                                             int width, int height, ImageScaleEnum scaleEnum)
        {
            using (DatabaseContext context = DatabaseContext.GetContext(true))
            {
                stream.Position = 0;

                string dataHash = FileRepositoryHelper.AddTemporaryFile(stream);

                var thumbnailEntity = new ThumbnailFileType( );

                string thumbnailName = originalImageEntity.Name;
                if (string.IsNullOrEmpty(thumbnailName))
                {
                    thumbnailName = "Thumbnail";
                }
                else
                {
                    thumbnailName += " Thumbnail";
                }

                thumbnailEntity.Name                = thumbnailName;
                thumbnailEntity.FileDataHash        = dataHash;
                thumbnailEntity.FileExtension       = fileExtension;
                thumbnailEntity.ImageWidth          = width;
                thumbnailEntity.ImageHeight         = height;
                thumbnailEntity.ThumbnailScaling    = scaleEnum;
                thumbnailEntity.IsThumbnailForImage = originalImageEntity;
                thumbnailEntity.Save( );

                context.CommitTransaction( );

                return(thumbnailEntity);
            }
        }
 public ThumbnailViewer(bool visible,bool collapsed,bool showBrowseButton,ThumbnailFileType thumbnailFileType)
 {
     this._visible = visible;
     this._collapsed = collapsed;
     this._showBrowseButton = showBrowseButton;
     this._thumbnailFileType = thumbnailFileType;
     _ctrl = null;
 }
Exemple #3
0
 public ThumbnailViewer(bool visible, bool collapsed, bool showBrowseButton, ThumbnailFileType thumbnailFileType)
 {
     this._visible           = visible;
     this._collapsed         = collapsed;
     this._showBrowseButton  = showBrowseButton;
     this._thumbnailFileType = thumbnailFileType;
     _ctrl = null;
 }
        /// <summary>
        ///     Gets the image thumbnail.
        /// </summary>
        /// <param name="imageId">The image id.</param>
        /// <param name="sizeId">The size id.</param>
        /// <param name="scaleId">The scale id.</param>
        /// <returns></returns>
        /// <exception cref="System.InvalidOperationException">The thumbnail size has invalid dimensions specified.</exception>
        public ImageFileType GetImageThumbnail(EntityRef imageId, EntityRef sizeId, EntityRef scaleId)
        {
            int               thumbnailWidth;
            int               thumbnailHeight;
            ImageScaleEnum    imageScaleEnum;
            ThumbnailSizeEnum thumbnailSizeEnum;

            // Quickly fail on invalid IDs
            if (_invalidImageIds.ContainsKey(imageId.Id))
            {
                throw new ArgumentException("The image entity was not found.");
            }

            // We don't really need to secure thumbnail sizes and image scale in this context.
            using (new SecurityBypassContext())
            {
                // Get the size and scale entities
                thumbnailSizeEnum = Entity.Get <ThumbnailSizeEnum>(sizeId);
                imageScaleEnum    = Entity.Get <ImageScaleEnum>(scaleId);

                if (thumbnailSizeEnum == null)
                {
                    throw new ArgumentNullException("Invalid thumbnail size");
                }

                if (imageScaleEnum == null)
                {
                    throw new ArgumentNullException("Invalid image scale");
                }

                // Get the width and height from the thumbnail size enum
                thumbnailWidth  = thumbnailSizeEnum.ThumbnailWidth ?? -1;
                thumbnailHeight = thumbnailSizeEnum.ThumbnailHeight ?? -1;
            }

            if (thumbnailHeight <= 0 || thumbnailWidth <= 0)
            {
                throw new ArgumentNullException("The thumbnail size has invalid dimensions specified.");
            }

            // Get the requested image
            var originalImageEntity = Entity.Get <ImageFileType>(imageId);

            // Handle missing images
            if (originalImageEntity == null)
            {
                // Verify if an admin can access it.
                using (new SecurityBypassContext( ))
                {
                    var unsecuredEntity = Entity.Get <ImageFileType>(imageId);
                    if (unsecuredEntity == null)
                    {
                        _invalidImageIds [imageId.Id] = null;
                    }
                }
                throw new ArgumentException("The image entity was not found.");
            }

            // Allow user access to thumbnails if they have access to the image
            using (new SecurityBypassContext( ))
            {
                // SVG can scale themselves fine as they are vector based. No need for thumbs.
                if (!string.IsNullOrEmpty(originalImageEntity.FileExtension) &&
                    originalImageEntity.FileExtension.ToLower().EndsWith(".svg"))
                {
                    return(originalImageEntity);
                }

                ThumbnailFileType thumbnail;

                // Create a thumbnail create key for this combination of image, size and scale.
                ThumbnailCreationKey   createKey   = new ThumbnailCreationKey(originalImageEntity.Id, thumbnailSizeEnum.Id, imageScaleEnum.Id);
                ThumbnailCreationValue createValue = null;

                try
                {
                    // Get or add a create value
                    createValue = _activeThumbnailCreations.GetOrAdd(createKey, k => new ThumbnailCreationValue());

                    // Increment the ref count
                    createValue.IncrementRefCount();

                    // Lock on this syncroot.
                    // Note: we are locking on this create value. This will only block parallel requests for the same image, size and scale combinations.
                    lock (createValue.SyncRoot)
                    {
                        // Find the first thumbnail with a matching size and scale
                        thumbnail = originalImageEntity.ImageHasThumbnails.FirstOrDefault(tt =>
                        {
                            int height = tt.ImageHeight ?? -1;
                            int width  = tt.ImageWidth ?? -1;

                            if (height == -1 ||
                                width == -1 ||
                                width != thumbnailWidth ||
                                height != thumbnailHeight)
                            {
                                // The thumbnail width does not match.
                                return(false);
                            }

                            long thumbnailScalingId = tt.ThumbnailScaling != null ? tt.ThumbnailScaling.Id : -1;

                            if (thumbnailScalingId == -1 || thumbnailScalingId != imageScaleEnum.Id)
                            {
                                // The thumbnail scale does not match.
                                return(false);
                            }

                            return(true);
                        });

                        if (thumbnail == null || (string.IsNullOrEmpty(thumbnail.FileDataHash)))
                        {
                            using (Stream thumbnailStream = CreateThumbnailImage(originalImageEntity, thumbnailWidth, thumbnailHeight, imageScaleEnum.Alias))
                            {
                                // Create the thumbnail entity and add the file to the database
                                ThumbnailFileType thumbnailFileType = CreateThumbnailImageEntity(thumbnailStream,
                                                                                                 originalImageEntity, originalImageEntity.FileExtension,
                                                                                                 thumbnailWidth, thumbnailHeight, imageScaleEnum);

                                return(thumbnailFileType.As <ImageFileType>());
                            }
                        }
                    }
                }
                finally
                {
                    // Decrement the ref count and remove from the active creations dictionary.
                    if (createValue != null &&
                        createValue.DecrementRefCount() <= 0)
                    {
                        ThumbnailCreationValue removedValue;
                        _activeThumbnailCreations.TryRemove(createKey, out removedValue);
                    }
                }

                // Found a thumbnail. Return it.
                return(thumbnail.As <ImageFileType>());
            }
        }