Example #1
0
        /// <summary>
        /// If any of the metadata items for this media object has its <see cref="IGalleryObject.ExtractMetadataOnSave" /> property
        /// set to true, then open the original file, extract the items, and update the <see cref="IGalleryObject.MetadataItems" />
        /// property on our media object. The <see cref="IGalleryObject.ExtractMetadataOnSave" /> property is not changed to false
        /// at this time, since the Save method uses it to know which items to persist to the data store.
        /// </summary>
        private void UpdateMetadata()
        {
            if (this._galleryObject.ExtractMetadataOnSave)
            {
                // Replace all metadata with the metadata found in the original file.
                Metadata.MediaObjectMetadataExtractor metadata;
                try
                {
                    metadata = new Metadata.MediaObjectMetadataExtractor(this._galleryObject.Original.FileNamePhysicalPath, this._galleryObject.GalleryId);
                }
                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
                    // re-throw the exception.
                    this._galleryObject.Original.Dispose();
                    throw new UnsupportedImageTypeException();
                }

                this._galleryObject.MetadataItems.Clear();
                this._galleryObject.MetadataItems.AddRange(metadata.GetGalleryObjectMetadataItemCollection());
                this._galleryObject.ExtractMetadataOnSave = true;
            }
            else
            {
                // If any individual metadata items have been set to ExtractFromFileOnSave = true, then update those selected ones with
                // the latest metadata from the file. If the metadata item is not found in the file, then set the value to an empty string.
                // The data layer will delete any items with an empty or null string.
                IGalleryObjectMetadataItemCollection metadataItemsToUpdate = this._galleryObject.MetadataItems.GetItemsToUpdate();
                if (metadataItemsToUpdate.Count > 0)
                {
                    Metadata.MediaObjectMetadataExtractor metadata;
                    try
                    {
                        metadata = new Metadata.MediaObjectMetadataExtractor(this._galleryObject.Original.FileNamePhysicalPath, this._galleryObject.GalleryId);
                    }
                    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
                        // re-throw the exception.
                        this._galleryObject.Original.Dispose();
                        throw new UnsupportedImageTypeException();
                    }

                    foreach (IGalleryObjectMetadataItem metadataItem in metadataItemsToUpdate)
                    {
                        IGalleryObjectMetadataItem extractedMetadataItem;
                        if (metadata.GetGalleryObjectMetadataItemCollection().TryGetMetadataItem(metadataItem.MetadataItemName, out extractedMetadataItem))
                        {
                            metadataItem.Value = extractedMetadataItem.Value;
                        }
                        else
                        {
                            metadataItem.Value = String.Empty;
                        }
                    }
                }
            }
        }
		/// <summary>
		/// If any of the metadata items for this media object have its ExtractFromFileOnSave property set to true, then open the 
		/// original file, extract the items, and update the MetadataItems property on our media object. The ExtractFromFileOnSave 
		/// property is not changed to false at this time, since the Save method uses it to know which items to persist to the data store.
		/// </summary>
		private void UpdateMetadata()
		{
			if (this._galleryObject.RegenerateMetadataOnSave)
			{
				// Replace all metadata with the metadata found in the original file.
				Metadata.MediaObjectMetadataExtractor metadata;
				try
				{
					metadata = new Metadata.MediaObjectMetadataExtractor(this._galleryObject.Original.FileNamePhysicalPath);
				}
				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 
					// re-throw the exception.
					this._galleryObject.Original.Dispose();
					throw new GalleryServerPro.ErrorHandler.CustomExceptions.UnsupportedImageTypeException();
				}

				this._galleryObject.MetadataItems.Clear();
				this._galleryObject.MetadataItems.AddRange(metadata.GetGalleryObjectMetadataItemCollection());
				this._galleryObject.MetadataItems.RegenerateAllOnSave = true;
			}
			else
			{
				// If any individual metadata items have been set to ExtractFromFileOnSave = true, then update those selected ones with
				// the latest metadata from the file. If the metadata item is not found in the file, then set the value to an empty string.
				// The data layer will delete any items with an empty or null string.
				IGalleryObjectMetadataItemCollection metadataItemsToUpdate = this._galleryObject.MetadataItems.GetItemsToUpdate();
				if (metadataItemsToUpdate.Count > 0)
				{
					Metadata.MediaObjectMetadataExtractor metadata;
					try
					{
						metadata = new Metadata.MediaObjectMetadataExtractor(this._galleryObject.Original.FileNamePhysicalPath);
					}
					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 
						// re-throw the exception.
						this._galleryObject.Original.Dispose();
						throw new GalleryServerPro.ErrorHandler.CustomExceptions.UnsupportedImageTypeException();
					}

					foreach (IGalleryObjectMetadataItem metadataItem in metadataItemsToUpdate)
					{
						IGalleryObjectMetadataItem extractedMetadataItem;
						if (metadata.GetGalleryObjectMetadataItemCollection().TryGetMetadataItem(metadataItem.MetadataItemName, out extractedMetadataItem))
						{
							metadataItem.Value = extractedMetadataItem.Value;
						}
						else
						{
							metadataItem.Value = String.Empty;
						}
					}
				}
			}
		}
        /// <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");

            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;
        }
Example #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="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="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 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 videoFile)
        {
            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 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);
            }

            // Optimized video
            this.Optimized = DisplayObject.CreateInstance(this, optimizedFilename, optimizedWidth, optimizedHeight, DisplayObjectType.Optimized, new VideoOptimizedCreator(this));
            this.Optimized.FileSizeKB = optimizedSizeKb;
            if (optimizedFilename.Length > 0)
            {
                // Calcululate the full file path to the optimized video. 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 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 += MediaObject_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 (IOException ex)
                {
                    ErrorHandler.Error.Record(ex, this._galleryObject.GalleryId, Factory.LoadGallerySettings(), AppSetting.Instance);
                }
                catch (UnauthorizedAccessException ex)
                {
                    ErrorHandler.Error.Record(ex, this._galleryObject.GalleryId, Factory.LoadGallerySettings(), AppSetting.Instance);
                }
                catch (NotSupportedException 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.
        }