Пример #1
0
        /// <summary>
        /// Invokes the middleware.
        /// </summary>
        /// <param name="context">The current http context</param>
        /// <param name="api">The current api</param>
        /// <param name="service">The application service</param>
        /// <returns>An async task</returns>
        public override async Task Invoke(HttpContext context, IApi api, IApplicationService service)
        {
            var useSitemapRouting = _config != null ? _config.UseSitemapRouting : true;

            if (useSitemapRouting && !IsHandled(context) && !context.Request.Path.Value.StartsWith("/manager/assets/"))
            {
                var url    = context.Request.Path.HasValue ? context.Request.Path.Value : "";
                var host   = context.Request.Host.Host;
                var scheme = context.Request.Scheme;
                var port   = context.Request.Host.Port;
                var prefix = service.Site.SitePrefix != null ?
                             $"/{ service.Site.SitePrefix }" : "";
                var baseUrl = scheme + "://" + host + (port.HasValue ? $":{port}" : "") + prefix;

                if (url.ToLower() == "/sitemap.xml")
                {
                    _logger?.LogInformation($"Sitemap.xml requested, generating");

                    // Get the requested site by hostname
                    var siteId = service.Site.Id;

                    // Get the sitemap for the site
                    var pages = await api.Sites.GetSitemapAsync(siteId);

                    if (App.Hooks.OnGenerateSitemap != null)
                    {
                        // We need to clone the sitemap as it might be cached
                        // if we're going to modify it.
                        pages = App.Hooks.OnGenerateSitemap(Utils.DeepClone(pages));
                    }

                    // Generate sitemap.xml
                    var sitemap = new Sitemap();

                    foreach (var page in pages)
                    {
                        if (!page.MetaIndex)
                        {
                            continue;
                        }

                        var urls = await GetPageUrlsAsync(api, page, baseUrl).ConfigureAwait(false);

                        if (urls.Count > 0)
                        {
                            sitemap.AddRange(urls);
                        }
                    }
                    context.Response.ContentType = "application/xml";
                    await context.Response.WriteAsync(sitemap.ToXml());

                    return;
                }
            }
            await _next.Invoke(context);
        }
Пример #2
0
        /// <summary>
        /// Invokes the middleware.
        /// </summary>
        /// <param name="context">The current http context</param>
        /// <returns>An async task</returns>
        public override async Task Invoke(HttpContext context)
        {
            if (!IsHandled(context) && !context.Request.Path.Value.StartsWith("/manager/assets/"))
            {
                var url     = context.Request.Path.HasValue ? context.Request.Path.Value : "";
                var host    = context.Request.Host.Host;
                var scheme  = context.Request.Scheme;
                var port    = context.Request.Host.Port;
                var baseUrl = scheme + "://" + host + (port.HasValue ? $":{port}" : "");

                if (url.ToLower() == "/sitemap.xml")
                {
                    if (logger != null)
                    {
                        logger.LogInformation($"Sitemap.xml requested, generating");
                    }

                    // Get the requested site by hostname
                    var site = api.Sites.GetByHostname(host);
                    if (site == null)
                    {
                        site = api.Sites.GetDefault();
                    }

                    // Get the sitemap for the site
                    var pages = api.Sites.GetSitemap(site.Id);

                    // Generate sitemap.xml
                    var sitemap = new Sitemap();

                    foreach (var page in pages)
                    {
                        var urls = GetPageUrls(page, baseUrl);

                        if (urls.Count > 0)
                        {
                            sitemap.AddRange(urls);
                        }
                    }
                    await context.Response.WriteAsync(sitemap.ToXml());
                }
            }
            await next.Invoke(context);
        }
        private void AppendApplicationsSitemaps(Sitemap sitemap, IEnumerable <ApplicationSitemapModel> applicationSitemapModels)
        {
            var baseUrl = baseUrlService.GetBaseUrl(Request, Url);

            // get the task results as individual sitemaps and merge into one
            foreach (var applicationSitemapModel in applicationSitemapModels)
            {
                if (applicationSitemapModel.RetrievalTask.IsCompletedSuccessfully)
                {
                    logger.LogInformation($"{nameof(Action)}: Received child Sitemap for: {applicationSitemapModel.Path}");

                    var mappings = applicationSitemapModel.RetrievalTask.Result;

                    var sitemapLocations = mappings?.ToList();
                    if (sitemapLocations == null || !sitemapLocations.Any())
                    {
                        continue;
                    }

                    foreach (var mapping in sitemapLocations)
                    {
                        // rewrite the URL to swap any child application address prefix for the composite UI address prefix
                        var pathRootUri = new Uri(applicationSitemapModel.SitemapUrl);
                        var appBaseUrl  = $"{pathRootUri.Scheme}://{pathRootUri.Authority}";

                        if (mapping.Url.StartsWith(appBaseUrl, StringComparison.InvariantCultureIgnoreCase))
                        {
                            mapping.Url = mapping.Url.Replace(appBaseUrl, baseUrl, StringComparison.InvariantCultureIgnoreCase);
                        }

                        mapping.Priority = 0.5;
                    }

                    sitemap.AddRange(sitemapLocations);
                }
                else
                {
                    logger.LogError($"{nameof(Action)}: Error getting child Sitemap for: {applicationSitemapModel.Path}");
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Invokes the middleware.
        /// </summary>
        /// <param name="context">The current http context</param>
        /// <param name="api">The current api</param>
        /// <returns>An async task</returns>
        public override async Task Invoke(HttpContext context, IApi api, IApplicationService service)
        {
            if (!IsHandled(context) && !context.Request.Path.Value.StartsWith("/manager/assets/"))
            {
                var url     = context.Request.Path.HasValue ? context.Request.Path.Value : "";
                var host    = context.Request.Host.Host;
                var scheme  = context.Request.Scheme;
                var port    = context.Request.Host.Port;
                var baseUrl = scheme + "://" + host + (port.HasValue ? $":{port}" : "");

                if (url.ToLower() == "/sitemap.xml")
                {
                    _logger?.LogInformation($"Sitemap.xml requested, generating");

                    // Get the requested site by hostname
                    var siteId = service.Site.Id;

                    // Get the sitemap for the site
                    var pages = await api.Sites.GetSitemapAsync(siteId);

                    // Generate sitemap.xml
                    var sitemap = new Sitemap();

                    foreach (var page in pages)
                    {
                        var urls = await GetPageUrlsAsync(api, page, baseUrl);

                        if (urls.Count > 0)
                        {
                            sitemap.AddRange(urls);
                        }
                    }
                    context.Response.ContentType = "application/xml";
                    await context.Response.WriteAsync(sitemap.ToXml());

                    return;
                }
            }
            await _next.Invoke(context);
        }
Пример #5
0
        public FileContentResult SiteMap()
        {
            var sitemap = new Sitemap();
            var baseURL = AccessHelper.SiteUrl;

            sitemap.Add(new Location(DateTime.Now)
            {
                Url = baseURL,

                //LastModified = DateTime.UtcNow.AddDays(-1)
            });


            var mainType = CMSPage.GetTypes("MainPage").ToList();

            if (mainType.Any())
            {
                var pages =
                    CMSPage.FullPageTable.Where(x => x.Type != mainType.First())
                    .OrderBy(x => x.OrderNum)
                    .Select(x => new Location()
                {
                    Url = baseURL + x.FullUrl, LastModified = x.LastMod
                });
                sitemap.AddRange(pages);
            }

            var cats =
                CatalogBrowser.CategoriesList.OrderBy(x => x.Value.OrderNum)
                .Select(x => new Location()
            {
                Url = baseURL + x.Value.FullUrl, LastModified = x.Value.LastMod
            }).ToList();

            if (cats.Any())
            {
                sitemap.AddRange(cats);
            }

            var prods = db.StoreProducts.Where(x => !x.Deleted).ToList().Select(x => new Location()
            {
                Url = baseURL + x.FullUrl, LastModified = x.LastMod
            }).ToList();

            if (prods.Any())
            {
                sitemap.AddRange(prods);
            }


            sitemap.Locations = sitemap.Locations.OrderByDescending(x => x.LastMod).ToArray();
            var cnt     = sitemap.Locations.Count();
            var counter = 0;

            foreach (var location in sitemap.Locations)
            {
                location.Priority = Math.Round(1.0 - (1.0 / cnt) * counter, 5);
                counter++;
            }


            using (var fs = new MemoryStream())
            {
                var xs = new XmlSerializer(typeof(Sitemap));
                xs.Serialize(fs, sitemap);
                fs.Position = 0;
                return(new FileContentResult(fs.ToArray(), "text/xml"));
            }
        }
Пример #6
0
        public ActionResult Index(string Path)
        {
            var sitemap     = new Sitemap();
            var siteMapPath = Server.MapPath("/sitemap.xml");

            var baseURL = AccessHelper.SiteUrl;

            sitemap.Add(new Location(DateTime.Now)
            {
                Url = baseURL,

                //LastModified = DateTime.UtcNow.AddDays(-1)
            });


            var mainType = CMSPage.GetTypes("MainPage").ToList();

            if (mainType.Any())
            {
                var pages =
                    CMSPage.FullPageTable.Where(x => x.Type != mainType.First())
                    .OrderBy(x => x.OrderNum)
                    .Select(x => new Location()
                {
                    Url = baseURL + x.FullUrl, LastModified = x.LastMod
                });
                sitemap.AddRange(pages);
            }

            var images = db.StoreImages.ToList();

            var cats =
                CatalogBrowser.CategoriesList.OrderBy(x => x.Value.OrderNum)
                .Select(
                    x =>
                    new Location()
            {
                Url          = baseURL + x.Value.FullUrl,
                LastModified = x.Value.LastMod,
                Images       = new[] { new MapImage()
                                       {
                                           loc = System.Web.HttpContext.Current.Request.Url.Scheme + "://" + System.Web.HttpContext.Current.Request.Url.Host + x.Value.ImageUrl
                                       } }
            })
                .OrderBy(x => x.LastMod)
                .ToList();

            if (cats.Any())
            {
                sitemap.AddRange(cats);
            }

            var prods =
                db.StoreProducts.Where(x => !x.Deleted) /*.Take(100)*/
                .ToList()
                .Select(
                    x =>
                    new Location()
            {
                Url          = baseURL + x.FullUrl,
                LastModified = x.LastMod,
                Images       =
                    images.Where(c => c.ProductID == x.ID)
                    .ToList()
                    .Select(
                        z =>
                        new MapImage()
                {
                    loc =
                        System.Web.HttpContext.Current.Request.Url.Scheme + "://" +
                        System.Web.HttpContext.Current.Request.Url.Host +
                        x.GetImgURL(z, 450, 450)
                })
                    .ToArray()
            })
                .ToList();


            if (prods.Any())
            {
                sitemap.AddRange(prods);
            }


            //sitemap.Locations = sitemap.Locations.OrderBy(x => x.LastMod).ToArray();

            /*
             *          var cnt = sitemap.Locations.Count();
             *          var counter = 0;
             *          foreach (var location in sitemap.Locations)
             *          {
             *              location.Priority = Math.Round(1.0 - (1.0 / cnt) * counter, 5);
             *              counter++;
             *          }
             */
            var maxCnt = 2500;

            if (sitemap.Locations.Count() < maxCnt)
            {
                /*
                 *              using (var fs = new FileStream(siteMapPath, FileMode.Create, FileAccess.ReadWrite))
                 *              {
                 */

                using (var sw = new StreamWriter(siteMapPath, false, new UTF8Encoding(false)))
                {
                    var ns = new XmlSerializerNamespaces();

                    //Add an empty namespace and empty value
                    ns.Add("", "http://www.sitemaps.org/schemas/sitemap/0.9");

                    var xs = new XmlSerializer(typeof(Sitemap));
                    xs.Serialize(sw, sitemap, ns);
                }


                /*
                 *              }
                 */
            }
            else
            {
                var parts = Math.Ceiling((decimal)sitemap.Locations.Count() / (decimal)maxCnt);

                var smi = new SiteMapIndex();

                for (int i = 1; i <= parts; i++)
                {
                    smi.Add(new Location()
                    {
                        Url = Request.Url.Scheme + "://" + Request.Url.Host + "/sitemap_" + i + ".xml", LastModified = DateTime.Now
                    });
                }

                using (var sw = new StreamWriter(siteMapPath, false, new UTF8Encoding(false)))
                {
                    var ns = new XmlSerializerNamespaces();

                    //Add an empty namespace and empty value
                    ns.Add("", "http://www.sitemaps.org/schemas/sitemap/0.9");

                    var xs = new XmlSerializer(typeof(SiteMapIndex));
                    xs.Serialize(sw, smi, ns);
                }


                for (int i = 1; i <= parts; i++)
                {
                    var sm = new Sitemap();
                    sm.AddRange(sitemap.Locations.Skip((i - 1) * maxCnt).Take(maxCnt));

                    using (var sw = new StreamWriter(Server.MapPath("~/sitemap_" + i + ".xml"), false, new UTF8Encoding(false)))
                    {
                        var ns = new XmlSerializerNamespaces();

                        ns.Add("", "http://www.sitemaps.org/schemas/sitemap/0.9");

                        var xs = new XmlSerializer(typeof(Sitemap));
                        xs.Serialize(sw, sm, ns);
                    }
                }
            }
            var path = Server.MapPath("~/sitemap.xml");

            if (System.IO.File.Exists(path))
            {
                using (var sr = new StreamReader(path, Encoding.UTF8))
                {
                    ViewBag.FileContent = sr.ReadToEnd();
                }
            }
            else
            {
                ViewBag.FileContent = "";
            }

            ModelState.AddModelError("", "Карта сайта успешно обновлена");

            return(PartialView());
            //return new FilePathResult(siteMapPath, "text/xml");
        }