示例#1
0
        /// <summary>
        /// Initializes a new instance of an <see cref="Image" /> object.
        /// </summary>
        /// <param name="id">The <see cref="GalleryObject.Id">ID</see> that uniquely identifies this object. Specify int.MinValue for a new object.</param>
        /// <param name="parentId">The <see cref="IGalleryObject.Id">ID</see> of the album that contains this object.</param>
        /// <param name="parentAlbum">The album that contains this object.</param>
        /// <param name="title">The title of this image.</param>
        /// <param name="hashKey">The hash key that uniquely identifies the original image file.</param>
        /// <param name="thumbnailFilename">The filename of the thumbnail image.</param>
        /// <param name="thumbnailWidth">The width (px) of the thumbnail image.</param>
        /// <param name="thumbnailHeight">The height (px) of the thumbnail image.</param>
        /// <param name="thumbnailSizeKb">The size (KB) of the thumbnail image.</param>
        /// <param name="optimizedFilename">The filename of the optimized image.</param>
        /// <param name="optimizedWidth">The width (px) of the optimized image.</param>
        /// <param name="optimizedHeight">The height (px) of the optimized image.</param>
        /// <param name="optimizedSizeKb">The size (KB) of the optimized image.</param>
        /// <param name="originalFilename">The filename of the original image.</param>
        /// <param name="originalWidth">The width (px) of the original image.</param>
        /// <param name="originalHeight">The height (px) of the original image.</param>
        /// <param name="originalSizeKb">The size (KB) of the original image.</param>
        /// <param name="sequence">An integer that represents the order in which this image should appear when displayed.</param>
        /// <param name="createdByUsername">The user name of the account that originally added this object to the data store.</param>
        /// <param name="dateAdded">The date this image was added to the data store.</param>
        /// <param name="lastModifiedByUsername">The user name of the account that last modified this object.</param>
        /// <param name="dateLastModified">The date this object was last modified.</param>
        /// <param name="isPrivate">Indicates whether this object should be hidden from un-authenticated (anonymous) users.</param>
        /// <param name="isInflated">A bool indicating whether this object is fully inflated.</param>
        /// <param name="imageFile">A <see cref="FileInfo"/> object containing the original image for this object. This is intended to be
        /// specified when creating a new media object from a file. Specify null when instantiating an object for an existing database
        /// record.</param>
        /// <exception cref="GalleryServerPro.ErrorHandler.CustomExceptions.InvalidMediaObjectException">Thrown when
        /// <paramref name="imageFile"/> is specified (not null) and the file it refers to is not in the same directory
        /// as the parent album's directory.</exception>
        /// <exception cref="GalleryServerPro.ErrorHandler.CustomExceptions.UnsupportedMediaObjectTypeException">Thrown when
        /// <paramref name="imageFile"/> is specified (not null) and its file extension does not correspond to an image MIME
        /// type, as determined by the MIME type definition in the configuration file.</exception>
        /// <exception cref="GalleryServerPro.ErrorHandler.CustomExceptions.UnsupportedImageTypeException">Thrown when the
        /// .NET Framework is unable to load an image file into the <see cref="System.Drawing.Bitmap"/> class. This is
        /// probably because it is corrupted, not an image supported by the .NET Framework, or the server does not have
        /// enough memory to process the image. The file cannot, therefore, be handled using the <see cref="Image"/>
        /// class; use <see cref="GenericMediaObject"/> instead. This exception is thrown only when <paramref name="imageFile"/>
        /// is specified (non-null).</exception>
        /// <remarks>This constructor does not verify that <paramref name="imageFile"/> refers to a file type that is enabled in the
        /// configuration file.</remarks>
        internal Image(int id, int parentId, IAlbum parentAlbum, string title, string hashKey, string thumbnailFilename, int thumbnailWidth, int thumbnailHeight, int thumbnailSizeKb, string optimizedFilename, int optimizedWidth, int optimizedHeight, int optimizedSizeKb, string originalFilename, int originalWidth, int originalHeight, int originalSizeKb, int sequence, string createdByUsername, DateTime dateAdded, string lastModifiedByUsername, DateTime dateLastModified, bool isPrivate, bool isInflated, FileInfo imageFile)
        {
            System.Diagnostics.Debug.Assert(((originalFilename.Length > 0) || (imageFile != null)), "Invalid Image constructor arguments: The original filename or a FileInfo reference to the original file must be passed to the Image constructor.");

            this.Id                     = id;
            this.Title                  = title;
            this.Sequence               = sequence;
            this.Hashkey                = hashKey;
            this.CreatedByUserName      = createdByUsername;
            this.DateAdded              = dateAdded;
            this.LastModifiedByUserName = lastModifiedByUsername;
            this.DateLastModified       = dateLastModified;
            this.IsPrivate              = isPrivate;

            // Set parent to album instance if present, otherwise create default Album instance if the album ID
            // is specified. If neither are specified, do not set parent (client will, however, need to set the
            // parent before saving).
            if (parentAlbum != null)
            {
                this.Parent = parentAlbum;
            }
            else if (parentId != int.MinValue)
            {
                this.Parent = Factory.CreateAlbumInstance(parentId);
            }

            string parentPhysicalPath = this.Parent.FullPhysicalPathOnDisk;

            // Thumbnail image
            this.Thumbnail            = DisplayObject.CreateInstance(this, thumbnailFilename, thumbnailWidth, thumbnailHeight, DisplayObjectType.Thumbnail, new ImageThumbnailCreator(this));
            this.Thumbnail.FileSizeKB = thumbnailSizeKb;
            if (thumbnailFilename.Length > 0)
            {
                // The thumbnail is stored in either the album's physical path or an alternate location (if thumbnailPath config setting is specified) .
                string thumbnailPath = HelperFunctions.MapAlbumDirectoryStructureToAlternateDirectory(parentPhysicalPath, AppSetting.Instance.ThumbnailPath);
                this.Thumbnail.FileNamePhysicalPath = System.IO.Path.Combine(thumbnailPath, thumbnailFilename);
            }

            // Optimized image
            this.Optimized            = DisplayObject.CreateInstance(this, optimizedFilename, optimizedWidth, optimizedHeight, DisplayObjectType.Optimized, new ImageOptimizedCreator(this));
            this.Optimized.FileSizeKB = optimizedSizeKb;
            if (optimizedFilename.Length > 0)
            {
                // Calcululate the full file path to the optimized image. If the optimized filename is equal to the original filename, then no
                // optimized version exists, and we'll just point to the original. If the names are different, then there is a separate optimized
                // image file, and it is stored in either the album's physical path or an alternate location (if optimizedPath config setting is specified).
                string optimizedPath = parentPhysicalPath;

                if (optimizedFilename != originalFilename)
                {
                    optimizedPath = HelperFunctions.MapAlbumDirectoryStructureToAlternateDirectory(parentPhysicalPath, AppSetting.Instance.OptimizedPath);
                }

                this.Optimized.FileNamePhysicalPath = System.IO.Path.Combine(optimizedPath, optimizedFilename);
            }

            // Original image
            this.Original = DisplayObject.CreateInstance(this, originalFilename, originalWidth, originalHeight, DisplayObjectType.Original, new ImageOriginalCreator(this));
            this.Original.ExternalHtmlSource = String.Empty;
            this.Original.ExternalType       = MimeTypeCategory.NotSet;

            if (imageFile != null)
            {
                this.Hashkey           = HelperFunctions.GetHashKeyUnique(imageFile);
                this.Original.FileInfo = imageFile;                 // Will throw InvalidMediaObjectException if the file's directory is not the same as the album's directory.

                if (this.Original.MimeType.TypeCategory != MimeTypeCategory.Image)
                {
                    throw new GalleryServerPro.ErrorHandler.CustomExceptions.UnsupportedMediaObjectTypeException(this.Original.FileInfo);
                }

                try
                {
                    this.Original.Width  = this.Original.Bitmap.Width;
                    this.Original.Height = this.Original.Bitmap.Height;
                }
                catch (GalleryServerPro.ErrorHandler.CustomExceptions.UnsupportedImageTypeException)
                {
                    // .NET Framework can't generate a bitmap from this image, so change the thumbnail creator to a generic one and null out
                    // the optimized object.
                    this.Original.Dispose();
                    this.Parent.Remove(this);
                    MediaObjectHashKeys.Instance.HashKeys.Remove(this.Hashkey);
                    throw;
                }

                int fileSize = (int)(imageFile.Length / 1024);
                this.Original.FileSizeKB = (fileSize < 1 ? 1 : fileSize);                 // Very small files should be 1, not 0.

                // Get metadata from the image file.
                if (Configuration.ConfigManager.GetGalleryServerProConfigSection().Core.EnableImageMetadata)
                {
                    try
                    {
                        MediaObjectMetadataExtractor metadata = new MediaObjectMetadataExtractor(imageFile.FullName);
                        this.MetadataItems.AddRange(metadata.GetGalleryObjectMetadataItemCollection());
                    }
                    catch (OutOfMemoryException)
                    {
                        // Normally, the Dispose method is called during the Image_Saved event. But when we get this exception, it
                        // never executes and therefore doesn't release the file lock. So we explicitly do so here and then
                        // throw a new exception.
                        this.Original.Dispose();
                        this.Parent.Remove(this);
                        throw new GalleryServerPro.ErrorHandler.CustomExceptions.UnsupportedImageTypeException(this);
                    }
                }
                this.RegenerateMetadataOnSave = false;

                // If there is a title in the metadata, use that for the title of this media object. Otherwise, use the filename.
                if (String.IsNullOrEmpty(title))
                {
                    IGalleryObjectMetadataItem metadataTitle;
                    if (this.MetadataItems.TryGetMetadataItem(FormattedMetadataItemName.Title, out metadataTitle))
                    {
                        this.Title = metadataTitle.Value;
                    }
                    else
                    {
                        this.Title = imageFile.Name;
                    }
                }
            }
            else
            {
                this.Original.FileNamePhysicalPath = System.IO.Path.Combine(parentPhysicalPath, originalFilename);
                this.Original.FileSizeKB           = originalSizeKb;
            }

            this.SaveBehavior   = Factory.GetMediaObjectSaveBehavior(this);
            this.DeleteBehavior = Factory.GetMediaObjectDeleteBehavior(this);

            this.IsInflated = isInflated;

            // Setting the previous properties has caused HasChanges = true, but we don't want this while
            // we're instantiating a new object. Reset to false.
            this.HasChanges = false;

            // Set up our event handlers.
            //this.Saving += new EventHandler(Image_Saving); // Don't need
            this.Saved += Image_Saved;
        }
        /// <summary>
        /// Generate the thumbnail image for this display object and save it to the file system. The routine may decide that
        /// a file does not need to be generated, usually because it already exists. However, it will always be
        /// created if the relevant flag is set on the parent <see cref="IGalleryObject" />. (Example: If
        /// <see cref="IGalleryObject.RegenerateThumbnailOnSave" /> = true, the thumbnail file will always be created.) No data is
        /// persisted to the data store.
        /// </summary>
        public void GenerateAndSaveFile()
        {
            // If necessary, generate and save the thumbnail version of the video.
            if (!(IsThumbnailImageRequired()))
            {
                return;                 // No thumbnail image required.
            }

            IGallerySettings gallerySetting = Factory.LoadGallerySetting(_galleryObject.GalleryId);

            // Generate a temporary filename to store the thumbnail created by FFmpeg.
            string tmpVideoThumbnailPath = Path.Combine(AppSetting.Instance.TempUploadDirectory, String.Concat(Guid.NewGuid().ToString(), ".jpg"));

            // Request that FFmpeg create the thumbnail. If successful, the file will be created.
            string ffmpegOutput = FFmpeg.GenerateThumbnail(this._galleryObject.Original.FileNamePhysicalPath, tmpVideoThumbnailPath, gallerySetting.VideoThumbnailPosition, this._galleryObject.GalleryId);

            if (!String.IsNullOrEmpty(ffmpegOutput) && this._galleryObject.IsNew && gallerySetting.ExtractMetadata && this._galleryObject.ExtractMetadataOnSave)
            {
                // When metadata extraction is enabled and we have a new video where we have some output from FFmpeg, parse the data.
                MediaObjectMetadataExtractor metadata = new MediaObjectMetadataExtractor(this._galleryObject.Original.FileNamePhysicalPath, this._galleryObject.GalleryId, ffmpegOutput);
                this._galleryObject.MetadataItems.AddRange(metadata.GetGalleryObjectMetadataItemCollection());
                this._galleryObject.ExtractMetadataOnSave = false;                 // Sends signal to save routine to not re-extract metadata
            }

            // Verify image was created from video, trying again using a different video position setting if necessary.
            ValidateVideoThumbnail(tmpVideoThumbnailPath, gallerySetting.VideoThumbnailPosition);

            // Determine file name and path of the thumbnail image.
            string thumbnailPath = HelperFunctions.MapAlbumDirectoryStructureToAlternateDirectory(this._galleryObject.Original.FileInfo.DirectoryName, gallerySetting.FullThumbnailPath, gallerySetting.FullMediaObjectPath);
            string newFilename   = GenerateNewFilename(thumbnailPath, ImageFormat.Jpeg, gallerySetting.ThumbnailFileNamePrefix);
            string newFilePath   = Path.Combine(thumbnailPath, newFilename);

            int newWidth, newHeight;

            if (File.Exists(tmpVideoThumbnailPath))
            {
                // FFmpeg successfully created a thumbnail image the same size as the video. Now resize it to the width and height we need.
                using (Bitmap originalBitmap = new Bitmap(tmpVideoThumbnailPath))
                {
                    ImageHelper.CalculateThumbnailWidthAndHeight(originalBitmap.Width, originalBitmap.Height, out newWidth, out newHeight, false, gallerySetting.MaxThumbnailLength);

                    // Get JPEG quality value (0 - 100). This is ignored if imgFormat = GIF.
                    int jpegQuality = gallerySetting.ThumbnailImageJpegQuality;

                    // Generate the new image and save to disk.
                    ImageHelper.SaveImageFile(originalBitmap, newFilePath, ImageFormat.Jpeg, newWidth, newHeight, jpegQuality);
                }

                try
                {
                    // Now delete the thumbnail image created by FFmpeg, but no worries if an error happens. The file is in the temp directory
                    // which is cleaned out each time the app starts anyway.
                    File.Delete(tmpVideoThumbnailPath);
                }
                catch (Exception ex)
                {
                    ErrorHandler.Error.Record(ex, this._galleryObject.GalleryId, Factory.LoadGallerySettings(), AppSetting.Instance);
                }
            }
            else
            {
                // FFmpeg didn't run or no thumbnail image was created by FFmpeg. Build a generic video thumbnail.
                using (Bitmap originalBitmap = Resources.GenericThumbnailImage_Video)
                {
                    ImageHelper.CalculateThumbnailWidthAndHeight(originalBitmap.Width, originalBitmap.Height, out newWidth, out newHeight, true, gallerySetting.MaxThumbnailLength);

                    // Get JPEG quality value (0 - 100).
                    int jpegQuality = gallerySetting.ThumbnailImageJpegQuality;

                    // Generate the new image and save to disk.
                    ImageHelper.SaveImageFile(originalBitmap, newFilePath, ImageFormat.Jpeg, newWidth, newHeight, jpegQuality);
                }
            }

            this._galleryObject.Thumbnail.Width                = newWidth;
            this._galleryObject.Thumbnail.Height               = newHeight;
            this._galleryObject.Thumbnail.FileName             = newFilename;
            this._galleryObject.Thumbnail.FileNamePhysicalPath = newFilePath;

            int fileSize = (int)(this._galleryObject.Thumbnail.FileInfo.Length / 1024);

            this._galleryObject.Thumbnail.FileSizeKB = (fileSize < 1 ? 1 : fileSize);             // Very small files should be 1, not 0.
        }
        /// <summary>
        /// Initializes a new instance of a <see cref="GenericMediaObject" /> object.
        /// </summary>
        /// <param name="id">The ID that uniquely identifies this object. Specify int.MinValue for a new object.</param>
        /// <param name="parentAlbum">The album that contains this object. This is a required parameter.</param>
        /// <param name="title">The title of this image.</param>
        /// <param name="hashKey">The hash key that uniquely identifies the original file.</param>
        /// <param name="thumbnailFilename">The filename of the thumbnail image.</param>
        /// <param name="thumbnailWidth">The width (px) of the thumbnail image.</param>
        /// <param name="thumbnailHeight">The height (px) of the thumbnail image.</param>
        /// <param name="thumbnailSizeKb">The size (KB) of the thumbnail image.</param>
        /// <param name="originalFilename">The filename of the original image.</param>
        /// <param name="originalWidth">The width (px) of the original image.</param>
        /// <param name="originalHeight">The height (px) of the original image.</param>
        /// <param name="originalSizeKb">The size (KB) of the original image.</param>
        /// <param name="sequence">An integer that represents the order in which this image should appear when displayed.</param>
        /// <param name="createdByUsername">The user name of the account that originally added this object to the data store.</param>
        /// <param name="dateAdded">The date this image was added to the data store.</param>
        /// <param name="lastModifiedByUsername">The user name of the account that last modified this object.</param>
        /// <param name="dateLastModified">The date this object was last modified.</param>
        /// <param name="isPrivate">Indicates whether this object should be hidden from un-authenticated (anonymous) users.</param>
        /// <param name="isInflated">A bool indicating whether this object is fully inflated.</param>
        /// <param name="file">A <see cref="FileInfo"/> object containing the original file for this object. This is intended to be
        /// specified when creating a new media object from a file. Specify null when instantiating an object for an existing database
        /// record.</param>
        /// <exception cref="GalleryServerPro.ErrorHandler.CustomExceptions.InvalidMediaObjectException">Thrown when
        /// the <paramref name="file"/> parameter is specified (not null) and the file it refers to is not in the same directory
        /// as the parent album's directory.</exception>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="parentAlbum" /> is null.</exception>
        /// <remarks>This constructor does not verify that <paramref name="file"/> refers to a file type that is enabled in the
        /// configuration file.</remarks>
        internal GenericMediaObject(int id, IAlbum parentAlbum, string title, string hashKey, string thumbnailFilename,
                                    int thumbnailWidth, int thumbnailHeight, int thumbnailSizeKb, string originalFilename,
                                    int originalWidth, int originalHeight, int originalSizeKb, int sequence,
                                    string createdByUsername, DateTime dateAdded, string lastModifiedByUsername, DateTime dateLastModified,
                                    bool isPrivate, bool isInflated, System.IO.FileInfo file)
        {
            if (parentAlbum == null)
            {
                throw new ArgumentNullException("parentAlbum");
            }

            System.Diagnostics.Debug.Assert(((originalFilename.Length > 0) || (file != null)), "Invalid GenericMediaObject constructor arguments: The original filename or a FileInfo reference to the original file must be passed to the GenericMediaObject constructor.");

            this.Id                     = id;
            this.Parent                 = parentAlbum;
            this.GalleryId              = this.Parent.GalleryId;
            this.Title                  = title;
            this.Sequence               = sequence;
            this.Hashkey                = hashKey;
            this.CreatedByUserName      = createdByUsername;
            this.DateAdded              = dateAdded;
            this.LastModifiedByUserName = lastModifiedByUsername;
            this.DateLastModified       = dateLastModified;
            this.IsPrivate              = isPrivate;

            string parentPhysicalPath = this.Parent.FullPhysicalPathOnDisk;

            IGallerySettings gallerySetting = Factory.LoadGallerySetting(GalleryId);

            // Thumbnail image
            this.Thumbnail            = DisplayObject.CreateInstance(this, thumbnailFilename, thumbnailWidth, thumbnailHeight, DisplayObjectType.Thumbnail, new GenericThumbnailCreator(this));
            this.Thumbnail.FileSizeKB = thumbnailSizeKb;
            if (thumbnailFilename.Length > 0)
            {
                // The thumbnail is stored in either the album's physical path or an alternate location (if thumbnailPath config setting is specified) .
                string thumbnailPath = HelperFunctions.MapAlbumDirectoryStructureToAlternateDirectory(parentPhysicalPath, gallerySetting.FullThumbnailPath, gallerySetting.FullMediaObjectPath);
                this.Thumbnail.FileNamePhysicalPath = System.IO.Path.Combine(thumbnailPath, thumbnailFilename);
            }

            // GenericMediaObject instances do not have an optimized version.
            this.Optimized = new NullObjects.NullDisplayObject();

            // Original file
            this.Original = DisplayObject.CreateInstance(this, originalFilename, originalWidth, originalHeight, DisplayObjectType.Original, new NullObjects.NullDisplayObjectCreator());
            this.Original.ExternalHtmlSource = String.Empty;
            this.Original.ExternalType       = MimeTypeCategory.NotSet;

            if (file != null)
            {
                this.Hashkey           = HelperFunctions.GetHashKeyUnique(file);
                this.Original.FileInfo = file;                 // Will throw InvalidMediaObjectException if the file's directory is not the same as the album's directory.

                if (this.MimeType.TypeCategory == MimeTypeCategory.Other)
                {
                    // Specify a default width and height for any object other than audio, video, and image. We leave those to their default
                    // value of int.MinValue because we do not accurately know their real width and height. For example, a corrupt image file
                    // will be rejected by the Image class (an UnsupportedImageTypeException is thrown) and will be routed to this class instead.
                    // In this case, we don't know it's real width and height. Similarly, audio and video files are normally handled by the
                    // Audio and Video classes. If one of them ends up here, we need to treat it generically and not assign a width and height.
                    this.Original.Width  = gallerySetting.DefaultGenericObjectWidth;
                    this.Original.Height = gallerySetting.DefaultGenericObjectHeight;
                }

                int fileSize = (int)(file.Length / 1024);
                this.Original.FileSizeKB = (fileSize < 1 ? 1 : fileSize);                 // Very small files should be 1, not 0.

                if (gallerySetting.ExtractMetadata)
                {
                    // Get the metadata found in the original file.
                    MediaObjectMetadataExtractor metadata = new MediaObjectMetadataExtractor(file.FullName, this.GalleryId);
                    MetadataItems.AddRange(metadata.GetGalleryObjectMetadataItemCollection());
                }

                // Assign the title based on the template, resorting to the filename if necessary.
                if (String.IsNullOrEmpty(title))
                {
                    SetTitle(gallerySetting.MediaObjectCaptionTemplate);

                    if (String.IsNullOrEmpty(this.Title))
                    {
                        this.Title = file.Name;
                    }
                }
            }
            else
            {
                this.Original.FileNamePhysicalPath = System.IO.Path.Combine(parentPhysicalPath, originalFilename);
                this.Original.FileSizeKB           = originalSizeKb;
            }

            this.SaveBehavior   = Factory.GetMediaObjectSaveBehavior(this);
            this.DeleteBehavior = Factory.GetMediaObjectDeleteBehavior(this);

            this.IsInflated = isInflated;

            // Setting the previous properties has caused HasChanges = true, but we don't want this while
            // we're instantiating a new object. Reset to false.
            this.HasChanges = false;

            // Set up our event handlers.
            //this.Saving += new EventHandler(Image_Saving); // Don't need
            this.Saved += Image_Saved;
        }
示例#4
0
        /// <summary>
        /// Initializes a new instance of an <see cref="Video" /> object.
        /// </summary>
        /// <param name="id">The ID that uniquely identifies this object. Specify int.MinValue for a new object.</param>
        /// <param name="parentAlbum">The album that contains this object. This is a required parameter.</param>
        /// <param name="title">The title of this image.</param>
        /// <param name="hashKey">The hash key that uniquely identifies the original image file.</param>
        /// <param name="thumbnailFilename">The filename of the thumbnail image.</param>
        /// <param name="thumbnailWidth">The width (px) of the thumbnail image.</param>
        /// <param name="thumbnailHeight">The height (px) of the thumbnail image.</param>
        /// <param name="thumbnailSizeKb">The size (KB) of the thumbnail image.</param>
        /// <param name="originalFilename">The filename of the original image.</param>
        /// <param name="originalWidth">The width (px) of the original image.</param>
        /// <param name="originalHeight">The height (px) of the original image.</param>
        /// <param name="originalSizeKb">The size (KB) of the original image.</param>
        /// <param name="sequence">An integer that represents the order in which this image should appear when displayed.</param>
        /// <param name="createdByUsername">The user name of the account that originally added this object to the data store.</param>
        /// <param name="dateAdded">The date this image was added to the data store.</param>
        /// <param name="lastModifiedByUsername">The user name of the account that last modified this object.</param>
        /// <param name="dateLastModified">The date this object was last modified.</param>
        /// <param name="isPrivate">Indicates whether this object should be hidden from un-authenticated (anonymous) users.</param>
        /// <param name="isInflated">A bool indicating whether this object is fully inflated.</param>
        /// <param name="videoFile">A <see cref="FileInfo"/> object containing the original video file for this object. This is intended to be
        /// specified when creating a new media object from a file. Specify null when instantiating an object for an existing database
        /// record.</param>
        /// <exception cref="GalleryServerPro.ErrorHandler.CustomExceptions.InvalidMediaObjectException">Thrown when
        /// <paramref name="videoFile"/> is specified (not null) and the file it refers to is not in the same directory
        /// as the parent album's directory.</exception>
        /// <exception cref="GalleryServerPro.ErrorHandler.CustomExceptions.UnsupportedMediaObjectTypeException">Thrown when
        /// <paramref name="videoFile"/> is specified (not null) and its file extension does not correspond to an video MIME
        /// type, as determined by the MIME type definition in the configuration file.</exception>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="parentAlbum" /> is null.</exception>
        /// <remarks>This constructor does not verify that <paramref name="videoFile"/> refers to a file type that is enabled in the
        /// configuration file.</remarks>
        internal Video(int id, IAlbum parentAlbum, string title, string hashKey, string thumbnailFilename, int thumbnailWidth, int thumbnailHeight, int thumbnailSizeKb, string originalFilename, int originalWidth, int originalHeight, int originalSizeKb, int sequence, string createdByUsername, DateTime dateAdded, string lastModifiedByUsername, DateTime dateLastModified, bool isPrivate, bool isInflated, FileInfo videoFile)
        {
            if (parentAlbum == null)
            {
                throw new ArgumentNullException("parentAlbum");
            }

            System.Diagnostics.Debug.Assert(((originalFilename.Length > 0) || (videoFile != null)), "Invalid Video constructor arguments: The original filename or a FileInfo reference to the original file must be passed to the Video constructor.");

            this.Id                     = id;
            this.Parent                 = parentAlbum;
            this.GalleryId              = this.Parent.GalleryId;
            this.Title                  = title;
            this.Sequence               = sequence;
            this.Hashkey                = hashKey;
            this.CreatedByUserName      = createdByUsername;
            this.DateAdded              = dateAdded;
            this.LastModifiedByUserName = lastModifiedByUsername;
            this.DateLastModified       = dateLastModified;
            this.IsPrivate              = isPrivate;

            string parentPhysicalPath = this.Parent.FullPhysicalPathOnDisk;

            IGallerySettings gallerySetting = Factory.LoadGallerySetting(GalleryId);

            // Thumbnail image
            this.Thumbnail            = DisplayObject.CreateInstance(this, thumbnailFilename, thumbnailWidth, thumbnailHeight, DisplayObjectType.Thumbnail, new VideoThumbnailCreator(this));
            this.Thumbnail.FileSizeKB = thumbnailSizeKb;
            if (thumbnailFilename.Length > 0)
            {
                // The thumbnail is stored in either the album's physical path or an alternate location (if thumbnailPath config setting is specified) .
                string thumbnailPath = HelperFunctions.MapAlbumDirectoryStructureToAlternateDirectory(parentPhysicalPath, gallerySetting.FullThumbnailPath, gallerySetting.FullMediaObjectPath);
                this.Thumbnail.FileNamePhysicalPath = System.IO.Path.Combine(thumbnailPath, thumbnailFilename);
            }

            // Videos do not have an optimized version.
            this.Optimized = new NullObjects.NullDisplayObject();

            // Original video file
            this.Original = DisplayObject.CreateInstance(this, originalFilename, originalWidth, originalHeight, DisplayObjectType.Original, new NullObjects.NullDisplayObjectCreator());
            this.Original.ExternalHtmlSource = String.Empty;
            this.Original.ExternalType       = MimeTypeCategory.NotSet;

            if (videoFile != null)
            {
                this.Hashkey           = HelperFunctions.GetHashKeyUnique(videoFile);
                this.Original.FileInfo = videoFile;                 // Will throw InvalidMediaObjectException if the file's directory is not the same as the album's directory.

                if (this.MimeType.TypeCategory != MimeTypeCategory.Video)
                {
                    throw new GalleryServerPro.ErrorHandler.CustomExceptions.UnsupportedMediaObjectTypeException(this.Original.FileInfo);
                }

                this.Original.Width  = gallerySetting.DefaultVideoPlayerWidth;
                this.Original.Height = gallerySetting.DefaultVideoPlayerHeight;

                int fileSize = (int)(videoFile.Length / 1024);
                this.Original.FileSizeKB = (fileSize < 1 ? 1 : fileSize);                 // Very small files should be 1, not 0.

                if (gallerySetting.ExtractMetadata)
                {
                    // Get the metadata found in the original file.
                    MediaObjectMetadataExtractor metadata = new MediaObjectMetadataExtractor(videoFile.FullName, this.GalleryId);
                    MetadataItems.AddRange(metadata.GetGalleryObjectMetadataItemCollection());
                }

                // Assign the title based on the template, resorting to the filename if necessary.
                if (String.IsNullOrEmpty(title))
                {
                    SetTitle(gallerySetting.MediaObjectCaptionTemplate);

                    if (String.IsNullOrEmpty(this.Title))
                    {
                        this.Title = videoFile.Name;
                    }
                }
            }
            else
            {
                this.Original.FileNamePhysicalPath = System.IO.Path.Combine(parentPhysicalPath, originalFilename);
                this.Original.FileSizeKB           = originalSizeKb;
            }

            this.SaveBehavior   = Factory.GetMediaObjectSaveBehavior(this);
            this.DeleteBehavior = Factory.GetMediaObjectDeleteBehavior(this);

            this.IsInflated = isInflated;

            // Setting the previous properties has caused HasChanges = true, but we don't want this while
            // we're instantiating a new object. Reset to false.
            this.HasChanges = false;

            // Set up our event handlers.
            //this.Saving += new EventHandler(Image_Saving); // Don't need
            this.Saved += Image_Saved;
        }
示例#5
0
        /// <summary>
        /// Initializes a new instance of an <see cref="Image" /> object.
        /// </summary>
        /// <param name="id">The <see cref="GalleryObject.Id">ID</see> that uniquely identifies this object. Specify int.MinValue for a new object.</param>
        /// <param name="parentAlbum">The album that contains this object. This is a required parameter.</param>
        /// <param name="title">The title of this image.</param>
        /// <param name="hashKey">The hash key that uniquely identifies the original image file.</param>
        /// <param name="thumbnailFilename">The filename of the thumbnail image.</param>
        /// <param name="thumbnailWidth">The width (px) of the thumbnail image.</param>
        /// <param name="thumbnailHeight">The height (px) of the thumbnail image.</param>
        /// <param name="thumbnailSizeKb">The size (KB) of the thumbnail image.</param>
        /// <param name="optimizedFilename">The filename of the optimized image.</param>
        /// <param name="optimizedWidth">The width (px) of the optimized image.</param>
        /// <param name="optimizedHeight">The height (px) of the optimized image.</param>
        /// <param name="optimizedSizeKb">The size (KB) of the optimized image.</param>
        /// <param name="originalFilename">The filename of the original image.</param>
        /// <param name="originalWidth">The width (px) of the original image.</param>
        /// <param name="originalHeight">The height (px) of the original image.</param>
        /// <param name="originalSizeKb">The size (KB) of the original image.</param>
        /// <param name="sequence">An integer that represents the order in which this image should appear when displayed.</param>
        /// <param name="createdByUsername">The user name of the account that originally added this object to the data store.</param>
        /// <param name="dateAdded">The date this image was added to the data store.</param>
        /// <param name="lastModifiedByUsername">The user name of the account that last modified this object.</param>
        /// <param name="dateLastModified">The date this object was last modified.</param>
        /// <param name="isPrivate">Indicates whether this object should be hidden from un-authenticated (anonymous) users.</param>
        /// <param name="isInflated">A bool indicating whether this object is fully inflated.</param>
        /// <param name="imageFile">A <see cref="FileInfo"/> object containing the original image for this object. This is intended to be
        /// specified when creating a new media object from a file. Specify null when instantiating an object for an existing database
        /// record.</param>
        /// <exception cref="GalleryServerPro.ErrorHandler.CustomExceptions.InvalidMediaObjectException">Thrown when
        /// <paramref name="imageFile"/> is specified (not null) and the file it refers to is not in the same directory
        /// as the parent album's directory.</exception>
        /// <exception cref="GalleryServerPro.ErrorHandler.CustomExceptions.UnsupportedMediaObjectTypeException">Thrown when
        /// <paramref name="imageFile"/> is specified (not null) and its file extension does not correspond to an image MIME
        /// type, as determined by the MIME type definition in the configuration file.</exception>
        /// <exception cref="GalleryServerPro.ErrorHandler.CustomExceptions.UnsupportedImageTypeException">Thrown when the
        /// .NET Framework is unable to load an image file into the <see cref="System.Drawing.Bitmap"/> class. This is
        /// probably because it is corrupted, not an image supported by the .NET Framework, or the server does not have
        /// enough memory to process the image. The file cannot, therefore, be handled using the <see cref="Image"/>
        /// class; use <see cref="GenericMediaObject"/> instead. This exception is thrown only when <paramref name="imageFile"/>
        /// is specified (non-null).</exception>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="parentAlbum" /> is null.</exception>
        /// <remarks>This constructor does not verify that <paramref name="imageFile"/> refers to a file type that is enabled in the
        /// configuration file.</remarks>
        internal Image(int id, IAlbum parentAlbum, string title, string hashKey, string thumbnailFilename, int thumbnailWidth, int thumbnailHeight, int thumbnailSizeKb, string optimizedFilename, int optimizedWidth, int optimizedHeight, int optimizedSizeKb, string originalFilename, int originalWidth, int originalHeight, int originalSizeKb, int sequence, string createdByUsername, DateTime dateAdded, string lastModifiedByUsername, DateTime dateLastModified, bool isPrivate, bool isInflated, FileInfo imageFile)
        {
            if (parentAlbum == null)
            {
                throw new ArgumentNullException("parentAlbum");
            }

            this.Id                     = id;
            this.Parent                 = parentAlbum;
            this.GalleryId              = this.Parent.GalleryId;
            this.Title                  = title;
            this.Sequence               = sequence;
            this.Hashkey                = hashKey;
            this.CreatedByUserName      = createdByUsername;
            this.DateAdded              = dateAdded;
            this.LastModifiedByUserName = lastModifiedByUsername;
            this.DateLastModified       = dateLastModified;
            this.IsPrivate              = isPrivate;

            string           parentPhysicalPath = this.Parent.FullPhysicalPathOnDisk;
            IGallerySettings gallerySetting     = Factory.LoadGallerySetting(GalleryId);

            // Thumbnail image
            this.Thumbnail            = DisplayObject.CreateInstance(this, thumbnailFilename, thumbnailWidth, thumbnailHeight, DisplayObjectType.Thumbnail, new ImageThumbnailCreator(this));
            this.Thumbnail.FileSizeKB = thumbnailSizeKb;
            if (thumbnailFilename.Length > 0)
            {
                // The thumbnail is stored in either the album's physical path or an alternate location (if thumbnailPath config setting is specified) .
                string thumbnailPath = HelperFunctions.MapAlbumDirectoryStructureToAlternateDirectory(parentPhysicalPath, gallerySetting.FullThumbnailPath, gallerySetting.FullMediaObjectPath);
                this.Thumbnail.FileNamePhysicalPath = System.IO.Path.Combine(thumbnailPath, thumbnailFilename);
            }

            // Optimized image
            this.Optimized            = DisplayObject.CreateInstance(this, optimizedFilename, optimizedWidth, optimizedHeight, DisplayObjectType.Optimized, new ImageOptimizedCreator(this));
            this.Optimized.FileSizeKB = optimizedSizeKb;
            if (optimizedFilename.Length > 0)
            {
                // Calcululate the full file path to the optimized image. If the optimized filename is equal to the original filename, then no
                // optimized version exists, and we'll just point to the original. If the names are different, then there is a separate optimized
                // image file, and it is stored in either the album's physical path or an alternate location (if optimizedPath config setting is specified).
                string optimizedPath = parentPhysicalPath;

                if (optimizedFilename != originalFilename)
                {
                    optimizedPath = HelperFunctions.MapAlbumDirectoryStructureToAlternateDirectory(parentPhysicalPath, gallerySetting.FullOptimizedPath, gallerySetting.FullMediaObjectPath);
                }

                this.Optimized.FileNamePhysicalPath = System.IO.Path.Combine(optimizedPath, optimizedFilename);
            }

            // Original image
            this.Original = DisplayObject.CreateInstance(this, originalFilename, originalWidth, originalHeight, DisplayObjectType.Original, new ImageOriginalCreator(this));
            this.Original.ExternalHtmlSource = String.Empty;
            this.Original.ExternalType       = MimeTypeCategory.NotSet;

            if (imageFile != null)
            {
                this.Hashkey           = HelperFunctions.GetHashKeyUnique(imageFile);
                this.Original.FileInfo = imageFile;                 // Will throw InvalidMediaObjectException if the file's directory is not the same as the album's directory.

                if (this.Original.MimeType.TypeCategory != MimeTypeCategory.Image)
                {
                    throw new GalleryServerPro.ErrorHandler.CustomExceptions.UnsupportedMediaObjectTypeException(this.Original.FileInfo);
                }

                try
                {
                    this.Original.Width  = this.Original.Bitmap.Width;
                    this.Original.Height = this.Original.Bitmap.Height;
                }
                catch (GalleryServerPro.ErrorHandler.CustomExceptions.UnsupportedImageTypeException) { }

                int fileSize = (int)(imageFile.Length / 1024);
                this.Original.FileSizeKB = (fileSize < 1 ? 1 : fileSize);                 // Very small files should be 1, not 0.

                // Get metadata from the image file.
                if (gallerySetting.ExtractMetadata)
                {
                    try
                    {
                        MediaObjectMetadataExtractor metadata = new MediaObjectMetadataExtractor(imageFile.FullName, this.GalleryId);
                        this.MetadataItems.AddRange(metadata.GetGalleryObjectMetadataItemCollection());
                    }
                    catch (OutOfMemoryException ex)
                    {
                        // Normally, the Dispose method is called during the Image_Saved event. But when we get this exception, it
                        // never executes and therefore doesn't release the file lock. So we explicitly do so here and then
                        // throw a new exception.
                        this.Original.Dispose();
                        this.Parent.Remove(this);
                        throw new GalleryServerPro.ErrorHandler.CustomExceptions.UnsupportedImageTypeException(this, ex);
                    }
                }
                this.ExtractMetadataOnSave = false;

                // Assign the title based on the template, resorting to the filename if necessary.
                if (String.IsNullOrEmpty(title))
                {
                    SetTitle(gallerySetting.MediaObjectCaptionTemplate);

                    if (String.IsNullOrEmpty(this.Title))
                    {
                        this.Title = imageFile.Name;
                    }
                }
            }
            else
            {
                this.Original.FileNamePhysicalPath = System.IO.Path.Combine(parentPhysicalPath, originalFilename);
                this.Original.FileSizeKB           = originalSizeKb;
            }

            this.SaveBehavior   = Factory.GetMediaObjectSaveBehavior(this);
            this.DeleteBehavior = Factory.GetMediaObjectDeleteBehavior(this);

            this.IsInflated = isInflated;

            // Setting the previous properties has caused HasChanges = true, but we don't want this while
            // we're instantiating a new object. Reset to false.
            this.HasChanges = false;

            // Set up our event handlers.
            //this.Saving += new EventHandler(Image_Saving); // Don't need
            this.Saved += Image_Saved;
        }