Exemplo n.º 1
0
        public virtual ActionResult ViewGalleries()
        {
            ViewGalleriesVM model = this.ImageService.GetGalleries();

            return(View(model));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the list of the available galleries.
        /// </summary>
        /// <returns>The list of galleries.</returns>
        public ViewGalleriesVM GetGalleries()
        {
            ViewGalleriesVM model = new ViewGalleriesVM
            {
                Galleries = new List <ViewGallerySummaryVM>()
            };

            // Get the full physical path of the folder which contains the galleries (eg. C:\inetpub\wwwroot\Photos).
            string storageFolderPhysicalPath = HttpContext.Current.Server.MapPath(this.ConfigService.StorageFolderVirtualPath);

            // Get the full physical paths of each galleries (eg. list of C:\inetpub\wwwroot\Photos\First, C:\inetpub\wwwroot\Photos\Second etc.).
            IOrderedEnumerable <string> folderPhysicalPaths = Directory.GetDirectories(storageFolderPhysicalPath).OrderByDescending(p => p);

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

                // Get the number of images in the requested folder.
                int imageCount = imageFileNames.Count();

                // If there is at least one image in the gallery.
                if (imageCount > 0)
                {
                    // Get the name of the gallery folder (eg. First).
                    string folderName = Path.GetFileName(folderPhysicalPath);

                    // Get the full physical path of the gallery thumbnail image (eg. C:\inetpub\wwwroot\First\folder.jpg).
                    string thumbnailPhysicalPath = Path.Combine(folderPhysicalPath, "folder.jpg");

                    // If the thumbnail file does not exist, create it by resizing the first image and saving as "folder.jpg".
                    if (!File.Exists(thumbnailPhysicalPath))
                    {
                        string firstImagePhysicalPath = Path.Combine(folderPhysicalPath, imageFileNames.First());

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

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

                    // Get the file name of the thumbnail (eg. folder.jpg or img1.jpg).
                    string thumbnailFileName = Path.GetFileName(thumbnailPhysicalPath);

                    // Get the application relative virtual path of the gallery thumbnail image (eg. ~/Photos/First/img1.jpg);
                    string thumbnailVirtualPath = Path.Combine(Path.Combine(this.ConfigService.StorageFolderVirtualPath, folderName), thumbnailFileName);

                    // Build the returned model item for the gallery.
                    ViewGallerySummaryVM gallery = new ViewGallerySummaryVM
                    {
                        FolderName   = folderName,
                        Count        = imageCount,
                        ThumbnailUrl = thumbnailVirtualPath
                    };
                    model.Galleries.Add(gallery);
                }
            }

            return(model);
        }