protected virtual void PopulateOpenGraph(ResultExecutingContext filterContext, IPageViewModel <SitePage> model)
        {
            var siteUrl = SiteDefinition.Current.SiteUrl.ToString();

            siteUrl = siteUrl.TrimEnd('/');

            // When in preview mode, this is also run for some of the block, they have no CurrentPage
            if (model.CurrentPage == null)
            {
                return;
            }

            OpenGraphModel openGraph = new OpenGraphModel();

            openGraph.Url         = siteUrl + _urlResolver.GetUrl(model.CurrentPage.ContentLink);
            openGraph.ContentType = "article";

            // The list view model works best
            var listItem = model.CurrentPage as IHasListViewContentItem;

            if (listItem != null)
            {
                var listItemModel = listItem.GetListViewContentItem();
                openGraph.Description = listItemModel.Intro;
                openGraph.Title       = listItemModel.Title;
                if (listItemModel.ImageUrl != null)
                {
                    openGraph.ImageUrl = siteUrl +
                                         _urlResolver.GetUrl(new UrlBuilder(listItemModel.ImageUrl), ContextMode.Default)
                                         + "?preset=ogpage";
                }
            }
            else
            {
                openGraph.Title       = model.CurrentPage.MetaTitle;
                openGraph.Description = model.CurrentPage.MetaDescription;
                // See if there is an image we can use
                var imageUrlProp = model.CurrentPage.GetPropertyValue <Url>("ListViewImage");
                if (imageUrlProp != null)
                {
                    var imageUrl = _urlResolver.GetUrl(new UrlBuilder(imageUrlProp), ContextMode.Default);
                    openGraph.ImageUrl = siteUrl + imageUrl + "?preset=ogpage";
                }
            }
            openGraph.Description = HtmlHelpers.ScrubHtml(openGraph.Description);

            filterContext.Controller.ViewBag.OpenGraph = openGraph;
        }
        // GET: /g/<category>/<galleryId>/<name>
        public async Task <ActionResult> Details(string categoryName, string galleryId, string name)
        {
            ViewData["Configuration"] = _configuration;
            var decodedCategoryName = Helpers.DecodeParameterFromUrl(categoryName);
            var category            = Server.Instance.Categories.Categories.SingleOrDefault(c => c.Name.Equals(decodedCategoryName, StringComparison.CurrentCultureIgnoreCase));

            if (category == null)
            {
                return(RedirectToAction("Index"));
            }

            var gallery = await Server.Instance.Galleries.GetGalleryAsync(category.Id, galleryId);

            if (gallery == null)
            {
                return(RedirectToAction("Index"));
            }

            var images = Utilities.OrderImages(await Server.Instance.Images.GetGalleryImagesAsync(gallery.Id)).ToList();

            ViewData["images"] = images;

            if (gallery.CreatedByUserId.HasValue())
            {
                // migrated galleries won't have a user id
                ViewData["user"] = await Server.Instance.Users.GetUserAsync(gallery.CreatedByUserId);
            }

            ViewData.Model = gallery;

            long pixels = 0;

            foreach (var image in (List <Image>)ViewData["images"])
            {
                if (image.Metadata.Width.HasValue && image.Metadata.Height.HasValue)
                {
                    pixels += image.Metadata.Width.Value * image.Metadata.Height.Value;
                }
            }

            var megapixels = Convert.ToInt32(pixels / 1000000);

            ViewData["megapixels"] = megapixels;

            // build the open-graph model to enable great presentation when pages are indexed/shared
            var openGraphModel = new OpenGraphModel {
                Title = gallery.Name, Url = Request.GetRawUrl().AbsoluteUri
            };

            if (images.Count > 0)
            {
                var image          = images.SingleOrDefault(i => i.Position == 0) ?? images[0];
                var openGraphImage = new OpenGraphModel.OpenGraphImageModel {
                    Url = $"{_configuration["BaseUrl"]}diog/{image.Files.OriginalId}"
                };
                if (image.Metadata.Width.HasValue && image.Metadata.Height.HasValue)
                {
                    int width;
                    int height;

                    if (image.Metadata.Width > image.Metadata.Height)
                    {
                        width = 2048;
                        var dHeight = (decimal)image.Metadata.Height / ((decimal)image.Metadata.Width / (decimal)2048);
                        height = (int)Math.Round(dHeight);
                    }
                    else
                    {
                        height = 2048;
                        var dWidth = (decimal)image.Metadata.Width / ((decimal)image.Metadata.Height / (decimal)2048);
                        width = (int)Math.Round(dWidth);
                    }

                    openGraphImage.Width       = width;
                    openGraphImage.Height      = height;
                    openGraphImage.ContentType = OpenGraphModel.OpenGraphImageContentTypes.Jpeg;
                }

                openGraphModel.Images.Add(openGraphImage);
            }

            if (!string.IsNullOrEmpty(gallery.Description))
            {
                openGraphModel.Description = Helpers.GetFirstParagraph(gallery.Description);
            }
            ViewData["openGraphModel"] = openGraphModel;

            return(View());
        }
 public IViewComponentResult Invoke(OpenGraphModel openGraphModel)
 {
     return(View(openGraphModel));
 }
Exemple #4
0
        // GET: /gi/{galleryId}/{imageId}/{name}
        public async Task <ActionResult> Details(string galleryId, string imageId, string name)
        {
            var image = await Server.Instance.Images.GetImageAsync(galleryId, imageId);

            if (image == null)
            {
                return(RedirectToActionPermanent("Index", "Home"));
            }

            ViewData.Model      = image;
            ViewData["gallery"] = await Server.Instance.Galleries.GetGalleryAsync(image.GalleryCategoryId, galleryId);

            ViewData["category"] = Server.Instance.Categories.Categories.Single(c => c.Id == image.GalleryCategoryId);
            ViewData["mapsKey"]  = _configuration["Google:MapsApiKey"];

            if (User.Identity.IsAuthenticated)
            {
                ViewData["user"] = await Server.Instance.Users.GetUserAsync(Helpers.GetUserId(User));
            }

            // work out the previous image in the gallery for navigation purposes
            ViewData["previousImage"] = await Server.Instance.Images.GetPreviousImageInGalleryAsync(image);

            ViewData["nextImage"] = await Server.Instance.Images.GetNextImageInGalleryAsync(image);

            // record the image view if the user hasn't viewed the image before in their current session
            var viewedImages = HttpContext.Session.Get <List <string> >("viewedImages") ?? new List <string>();

            if (!viewedImages.Contains(image.Id))
            {
                await Server.Instance.Images.IncreaseImageViewsAsync(galleryId, imageId);

                viewedImages.Add(image.Id);
                HttpContext.Session.Set("viewedImages", viewedImages);
            }

            // build the open-graph model to enable great presentation when pages are indexed/shared
            var openGraphModel = new OpenGraphModel {
                Title = image.Name, Url = Request.GetRawUrl().AbsoluteUri
            };

            if (!string.IsNullOrEmpty(image.Caption))
            {
                openGraphModel.Description = Helpers.GetFirstParagraph(image.Caption);
            }

            var openGraphImage = new OpenGraphModel.OpenGraphImageModel {
                Url = $"{_configuration["BaseUrl"]}diog/{image.Files.OriginalId}"
            };

            if (image.Metadata.Width.HasValue && image.Metadata.Height.HasValue)
            {
                int width;
                int height;

                if (image.Metadata.Width > image.Metadata.Height)
                {
                    width = 1200;
                    var dHeight = (decimal)image.Metadata.Height / ((decimal)image.Metadata.Width / (decimal)1200);
                    height = (int)Math.Round(dHeight);
                }
                else
                {
                    height = 1200;
                    var dWidth = (decimal)image.Metadata.Width / ((decimal)image.Metadata.Height / (decimal)1200);
                    width = (int)Math.Round(dWidth);
                }

                openGraphImage.Width       = width;
                openGraphImage.Height      = height;
                openGraphImage.ContentType = OpenGraphModel.OpenGraphImageContentTypes.Jpeg;
            }

            openGraphModel.Images.Add(openGraphImage);
            ViewData["openGraphModel"] = openGraphModel;
            return(View());
        }