protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         albumHandler.Dispose();
     }
     base.Dispose(disposing);
 }
        /// <summary>
        /// Creates an album, assigns the user name as the owner, saves it, and returns the newly created album.
        /// A profile entry is created containing the album ID. Returns null if the ID specified in the gallery settings
        /// for the parent album does not represent an existing album. That is, returns null if <see cref="IGallerySettings.UserAlbumParentAlbumId" />
        /// does not match an existing album.
        /// </summary>
        /// <param name="userName">The user name representing the user who is the owner of the album.</param>
        /// <param name="galleryId">The gallery ID for the gallery in which the album is to be created.</param>
        /// <returns>
        /// Returns the newly created user album. It has already been persisted to the database.
        /// Returns null if the ID specified in the gallery settings for the parent album does not represent an existing album.
        /// That is, returns null if <see cref="IGallerySettings.UserAlbumParentAlbumId" />
        /// does not match an existing album.
        /// </returns>
        public static IAlbum CreateUserAlbum(string userName, int galleryId)
        {
            IGallerySettings gallerySetting = Factory.LoadGallerySetting(galleryId);

            string albumNameTemplate = gallerySetting.UserAlbumNameTemplate;

            IAlbum parentAlbum;

            try
            {
                parentAlbum = AlbumController.LoadAlbumInstance(gallerySetting.UserAlbumParentAlbumId, false);
            }
            catch (InvalidAlbumException ex)
            {
                // The parent album does not exist. Record the error and return null.
                string galleryDescription = Utils.HtmlEncode(Factory.LoadGallery(gallerySetting.GalleryId).Description);
                string msg = String.Format(CultureInfo.CurrentCulture, Resources.GalleryServerPro.Error_User_Album_Parent_Invalid_Ex_Msg, galleryDescription, gallerySetting.UserAlbumParentAlbumId);
                AppErrorController.LogError(new WebException(msg, ex), galleryId);
                return(null);
            }

            IAlbum album = null;

            try
            {
                album = Factory.CreateEmptyAlbumInstance(parentAlbum.GalleryId);

                album.Title         = albumNameTemplate.Replace("{UserName}", userName);
                album.Summary       = gallerySetting.UserAlbumSummaryTemplate;
                album.OwnerUserName = userName;
                //newAlbum.ThumbnailMediaObjectId = 0; // not needed
                album.Parent    = parentAlbum;
                album.IsPrivate = parentAlbum.IsPrivate;
                GalleryObjectController.SaveGalleryObject(album, userName);

                SaveAlbumIdToProfile(album.Id, userName, album.GalleryId);

                HelperFunctions.PurgeCache();
            }
            catch
            {
                if (album != null)
                {
                    album.Dispose();
                }

                throw;
            }

            return(album);
        }
        /// <summary>
        /// Create a sample album and media object. This method is intended to be invoked once just after the application has been
        /// installed.
        /// </summary>
        /// <param name="galleryId">The ID for the gallery where the sample objects are to be created.</param>
        public static void CreateSampleObjects(int galleryId)
        {
            if (Factory.LoadGallerySetting(galleryId).MediaObjectPathIsReadOnly)
            {
                return;
            }

            DateTime currentTimestamp = DateTime.Now;
            IAlbum   sampleAlbum      = null;

            try
            {
                foreach (IAlbum album in Factory.LoadRootAlbumInstance(galleryId).GetChildGalleryObjects(GalleryObjectType.Album))
                {
                    if (album.DirectoryName == "Samples")
                    {
                        sampleAlbum = album;
                        break;
                    }
                }
                if (sampleAlbum == null)
                {
                    // Create sample album.
                    sampleAlbum = Factory.CreateEmptyAlbumInstance(galleryId);

                    sampleAlbum.Parent                 = Factory.LoadRootAlbumInstance(galleryId);
                    sampleAlbum.Title                  = "Samples";
                    sampleAlbum.DirectoryName          = "Samples";
                    sampleAlbum.Summary                = "Welcome to Gallery Server Pro!";
                    sampleAlbum.CreatedByUserName      = "******";
                    sampleAlbum.DateAdded              = currentTimestamp;
                    sampleAlbum.LastModifiedByUserName = "******";
                    sampleAlbum.DateLastModified       = currentTimestamp;
                    sampleAlbum.Save();
                }

                // Look for sample image in sample album.
                IGalleryObject sampleImage = null;
                foreach (IGalleryObject image in sampleAlbum.GetChildGalleryObjects(GalleryObjectType.Image))
                {
                    if (image.Original.FileName == Constants.SAMPLE_IMAGE_FILENAME)
                    {
                        sampleImage = image;
                        break;
                    }
                }

                if (sampleImage == null)
                {
                    // Sample image not found. Pull image from assembly and save to disk (if needed), then create a media object from it.
                    string sampleDirPath       = Path.Combine(Factory.LoadGallerySetting(galleryId).FullMediaObjectPath, sampleAlbum.DirectoryName);
                    string sampleImageFilepath = Path.Combine(sampleDirPath, Constants.SAMPLE_IMAGE_FILENAME);

                    if (!File.Exists(sampleImageFilepath))
                    {
                        Assembly asm = Assembly.GetExecutingAssembly();
                        using (Stream stream = asm.GetManifestResourceStream(String.Concat("GalleryServerPro.Web.gs.images.", Constants.SAMPLE_IMAGE_FILENAME)))
                        {
                            if (stream != null)
                            {
                                using (FileStream fileStream = File.Create(sampleImageFilepath))
                                {
                                    using (BinaryWriter bw = new BinaryWriter(fileStream))
                                    {
                                        byte[] buffer = new byte[stream.Length];
                                        stream.Read(buffer, 0, (int)stream.Length);
                                        bw.Write(buffer);
                                        bw.Flush();
                                        bw.Close();
                                    }
                                }
                            }
                        }
                    }

                    if (File.Exists(sampleImageFilepath))
                    {
                        // Temporarily change a couple settings so that the thumbnail and compressed images are high quality.
                        IGallerySettings gallerySettings = Factory.LoadGallerySetting(galleryId);
                        int optTriggerSizeKb             = gallerySettings.OptimizedImageTriggerSizeKb;
                        int thumbImageJpegQuality        = gallerySettings.ThumbnailImageJpegQuality;
                        gallerySettings.ThumbnailImageJpegQuality   = 95;
                        gallerySettings.OptimizedImageTriggerSizeKb = 200;

                        // Create the media object from the file.
                        using (IGalleryObject image = Factory.CreateImageInstance(new FileInfo(sampleImageFilepath), sampleAlbum))
                        {
                            image.Title                  = "Margaret, Skyler and Roger Martin (July 2010)";
                            image.CreatedByUserName      = "******";
                            image.DateAdded              = currentTimestamp;
                            image.LastModifiedByUserName = "******";
                            image.DateLastModified       = currentTimestamp;
                            image.Save();
                        }

                        // Restore the default settings.
                        gallerySettings.OptimizedImageTriggerSizeKb = optTriggerSizeKb;
                        gallerySettings.ThumbnailImageJpegQuality   = thumbImageJpegQuality;
                    }
                }
            }
            catch
            {
                if (sampleAlbum != null)
                {
                    sampleAlbum.Dispose();
                }

                throw;
            }
        }
Exemple #4
0
        private IAlbum VerifyAlbumExistsAndReturnReference(ZipEntry zipContentFile, IAlbum rootParentAlbum)
        {
            // Get the directory path of the next file or directory within the zip file.
            // Ex: album1\album2\album3, album1
            string zipDirectoryPath = Path.GetDirectoryName(zipContentFile.Name);

            string[] directoryNames = zipDirectoryPath.Split(new char[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);

            string albumFullPhysicalPath = rootParentAlbum.FullPhysicalPathOnDisk;
            IAlbum currentAlbum          = rootParentAlbum;

            foreach (string directoryNameFromZip in directoryNames)
            {
                string shortenedDirName = GetPreviouslyCreatedTruncatedAlbumName(albumFullPhysicalPath, directoryNameFromZip);

                // Ex: c:\inetpub\wwwroot\galleryserver\mypics\2006\album1
                albumFullPhysicalPath = System.IO.Path.Combine(albumFullPhysicalPath, shortenedDirName);

                IAlbum newAlbum = null;
                try
                {
                    if (Directory.Exists(albumFullPhysicalPath))
                    {
                        // Directory exists, so there is probably an album corresponding to it. Find it.
                        IGalleryObjectCollection childGalleryObjects = currentAlbum.GetChildGalleryObjects(GalleryObjectType.Album);
                        foreach (IGalleryObject childGalleryObject in childGalleryObjects)
                        {
                            if (childGalleryObject.FullPhysicalPathOnDisk.Equals(albumFullPhysicalPath, StringComparison.OrdinalIgnoreCase))
                            {
                                newAlbum = childGalleryObject as Album; break;
                            }
                        }

                        if (newAlbum == null)
                        {
                            // No album in the database matches that directory. Add it.

                            // Before we add the album, we need to make sure the user has permission to add the album. Check if user
                            // is authenticated and if the current album is the one passed into this method. It can be assumed that any
                            // other album we encounter has been created by this method and we checked for permission when it was created.
                            if (this._isAuthenticated && (currentAlbum.Id == rootParentAlbum.Id))
                            {
                                SecurityManager.ThrowIfUserNotAuthorized(SecurityActions.AddChildAlbum, this._roles, currentAlbum.Id, currentAlbum.GalleryId, this._isAuthenticated, currentAlbum.IsPrivate);
                            }

                            newAlbum               = Factory.CreateEmptyAlbumInstance(currentAlbum.GalleryId);
                            newAlbum.Parent        = currentAlbum;
                            newAlbum.IsPrivate     = currentAlbum.IsPrivate;
                            newAlbum.DirectoryName = directoryNameFromZip;
                            HelperFunctions.UpdateAuditFields(newAlbum, this._userName);
                            newAlbum.Save();
                        }
                    }
                    else
                    {
                        // The directory doesn't exist. Create an album.

                        // Before we add the album, we need to make sure the user has permission to add the album. Check if user
                        // is authenticated and if the current album is the one passed into this method. It can be assumed that any
                        // other album we encounter has been created by this method and we checked for permission when it was created.
                        if (this._isAuthenticated && (currentAlbum.Id == rootParentAlbum.Id))
                        {
                            SecurityManager.ThrowIfUserNotAuthorized(SecurityActions.AddChildAlbum, this._roles, currentAlbum.Id, currentAlbum.GalleryId, this._isAuthenticated, currentAlbum.IsPrivate);
                        }

                        newAlbum           = Factory.CreateEmptyAlbumInstance(currentAlbum.GalleryId);
                        newAlbum.IsPrivate = currentAlbum.IsPrivate;
                        newAlbum.Parent    = currentAlbum;
                        newAlbum.Title     = directoryNameFromZip;
                        HelperFunctions.UpdateAuditFields(newAlbum, this._userName);
                        newAlbum.Save();

                        // If the directory name written to disk is different than the name from the zip file, add it to
                        // our hash table.
                        if (!directoryNameFromZip.Equals(newAlbum.DirectoryName))
                        {
                            this._albumAndDirectoryNamesLookupTable.Add(Path.Combine(currentAlbum.FullPhysicalPathOnDisk, directoryNameFromZip), Path.Combine(currentAlbum.FullPhysicalPathOnDisk, newAlbum.DirectoryName));
                        }
                    }
                    currentAlbum = newAlbum;
                }
                catch
                {
                    if (newAlbum != null)
                    {
                        newAlbum.Dispose();
                    }

                    throw;
                }
            }

            return(currentAlbum);
        }