Esempio n. 1
0
        public virtual ActionResult ViewGallery(string folderName)
        {
            Contract.Requires(!String.IsNullOrEmpty(folderName));

            ViewGalleryVM model = this.ImageService.GetGallery(folderName);

            return(View(model));
        }
Esempio n. 2
0
        /// <summary>
        /// Gets the details of a gallery.
        /// </summary>
        /// <param name="folderName">The name of the folder which contains the gallery.</param>
        /// <returns>The details of the specified gallery.</returns>
        public ViewGalleryVM GetGallery(string folderName)
        {
            // Input validation: the folder name must be specified.
            if (String.IsNullOrEmpty(folderName))
            {
                throw new ArgumentNullException("folderName");
            }

            // Input validation: the folder name must not contain any invalid character.
            if (folderName.IndexOfAny(Path.GetInvalidPathChars()) > -1 || folderName.IndexOfAny(Path.GetInvalidFileNameChars()) > -1)
            {
                throw new ArgumentException("The folder name contains invalid characters!", "folderName");
            }

            // Get the application relative virtual path of the gallery folder (eg. ~/Photos/First).
            string folderVirtualPath = Path.Combine(this.ConfigService.StorageFolderVirtualPath, folderName);

            // Get the full physical path of the gallery folder (eg. C:\inetpub\wwwroot\Photos\First).
            string folderPhysicalPath = HttpContext.Current.Server.MapPath(folderVirtualPath);

            // Integrity check: the folder must exist.
            if (!Directory.Exists(folderPhysicalPath))
            {
                throw new ArgumentException("The specified folder does not exist!", "folderName");
            }

            // Integrity check: the requested folder must be the direct child of the storage folder.
            string storagePhysicalPath      = HttpContext.Current.Server.MapPath(this.ConfigService.StorageFolderVirtualPath);
            string folderParentPhysicalPath = Directory.GetParent(folderPhysicalPath).FullName;

            if (!folderParentPhysicalPath.Equals(storagePhysicalPath, StringComparison.OrdinalIgnoreCase))
            {
                throw new ArgumentOutOfRangeException("folderName", "Invalid folder name!");
            }

            // Get the names of the image files in the requested folder.
            IEnumerable <string> imageFileNames = this.GetImageFiles(folderPhysicalPath);

            List <ViewGalleryImageVM> images = new List <ViewGalleryImageVM>();

            // Ensure that all image has thumbnails.
            foreach (string imageFileName in imageFileNames)
            {
                string thumbnailFileName     = "th_" + imageFileName;
                string thumbnailPhysicalPath = Path.Combine(folderPhysicalPath, thumbnailFileName);

                // Create the thumbnail if it does not exist.
                if (!File.Exists(thumbnailPhysicalPath))
                {
                    string imagePhysicalPath = Path.Combine(folderPhysicalPath, imageFileName);

                    if (ConfigService.AutoGenerateThumbnails)
                    {
                        Instructions instructions = new Instructions
                        {
                            Width  = 80,
                            Height = 60,
                            Mode   = FitMode.Max
                        };
                        ImageJob job = new ImageJob(imagePhysicalPath, thumbnailPhysicalPath, instructions);

                        try
                        {
                            job.Build();
                        }
                        catch
                        {
                            // In case of any error, use the original image as the thumbnail without resizing it.
                            thumbnailFileName = imageFileName;
                        }
                    }
                    else
                    {
                        thumbnailFileName = imageFileName;
                    }
                }

                images.Add(new ViewGalleryImageVM
                {
                    ThumbnailUrl = Path.Combine(folderVirtualPath, thumbnailFileName),
                    ImageUrl     = Path.Combine(folderVirtualPath, imageFileName)
                });
            }

            // Build the returned model.
            ViewGalleryVM model = new ViewGalleryVM
            {
                Title  = folderName,
                Images = images
            };

            return(model);
        }