-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathNavigationTreeSiteMapNodeService.cs
More file actions
163 lines (141 loc) · 6.43 KB
/
NavigationTreeSiteMapNodeService.cs
File metadata and controls
163 lines (141 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (c) Source Tree Solutions, LLC. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Author: Joe Audette
// Created: 2016-04-20
// Last Modified: 2019-07-31
//
using cloudscribe.Web.Navigation;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace cloudscribe.Web.SiteMap
{
/// <summary>
/// for those of using cloudscribe.Web.Navigation, we already have this tree object that typically contains
/// most or all the urls that we want to have indexed by search engines so rather than building a new list
/// it is more efficient to dual purpose this same data in order to build our sitemap.
/// That is what this class is for.
///
/// blog items are typically not in a main navigation menu though so we typically also need a separate
/// ISiteMapNodeService for blog posts
/// </summary>
public class NavigationTreeSiteMapNodeService : ISiteMapNodeService
{
public NavigationTreeSiteMapNodeService(
NavigationTreeBuilderService siteMapTreeBuilder,
IEnumerable<INavigationNodePermissionResolver> permissionResolvers,
IUrlHelperFactory urlHelperFactory,
IHttpContextAccessor contextAccessor,
ILogger<NavigationTreeSiteMapNodeService> logger)
{
this.siteMapTreeBuilder = siteMapTreeBuilder;
this.urlHelperFactory = urlHelperFactory;
this.contextAccessor = contextAccessor;
this.permissionResolvers = permissionResolvers;
log = logger;
}
private NavigationTreeBuilderService siteMapTreeBuilder;
private IUrlHelperFactory urlHelperFactory;
private ILogger log;
private IHttpContextAccessor contextAccessor;
private string baseUrl = string.Empty;
private List<string> addedUrls = new List<string>();
private IEnumerable<INavigationNodePermissionResolver> permissionResolvers;
// this should not be needed in rc2 because there will be urlhelper methods for absolute url
public string BaseUrl
{
get
{
if(string.IsNullOrEmpty(baseUrl))
{
baseUrl = string.Concat(contextAccessor.HttpContext.Request.Scheme,
"://",
contextAccessor.HttpContext.Request.Host.ToUriComponent());
}
return baseUrl;
}
}
private async Task<bool> ShouldRenderNode(NavigationNode node)
{
TreeNode<NavigationNode> treeNode = new TreeNode<NavigationNode>(node);
foreach(var permission in permissionResolvers)
{
bool ok = await permission.ShouldAllowView(treeNode);
if (!ok) return false;
}
return true;
}
public async Task<IEnumerable<ISiteMapNode>> GetSiteMapNodes(
CancellationToken cancellationToken = default(CancellationToken))
{
var rootNode = await siteMapTreeBuilder.GetTree();
var mapNodes = new List<SiteMapNode>();
var actionContext = new ActionContext(contextAccessor.HttpContext, contextAccessor.HttpContext.GetRouteData(), new Microsoft.AspNetCore.Mvc.Abstractions.ActionDescriptor());
var urlHelper = urlHelperFactory.GetUrlHelper(actionContext);
foreach (var navNode in rootNode.Flatten())
{
if (navNode.ExcludeFromSearchSiteMap) continue;
if(await ShouldRenderNode(navNode))
{
var url = ResolveUrl(navNode, urlHelper);
if(string.IsNullOrEmpty(url))
{
log.LogWarning("failed to resolve url for node " + navNode.Key + ", skipping this node for sitemap");
continue;
}
if(!url.StartsWith("http"))
{
log.LogWarning("skipping relative url " + url + ", sitemap urls must be absolute");
continue;
}
if (addedUrls.Contains(url)) continue;
if(navNode is ISiteMapNode)
{
var smNode = navNode as ISiteMapNode;
mapNodes.Add(
new SiteMapNode(url)
{
ChangeFrequency = smNode.ChangeFrequency,
LastModified = smNode.LastModified,
Priority = smNode.Priority
}
);
}
else
{
mapNodes.Add(new SiteMapNode(url) {
LastModified = navNode.LastModifiedUtc
});
}
addedUrls.Add(url);
}
}
return mapNodes;
}
private string ResolveUrl(NavigationNode node, IUrlHelper urlHelper)
{
if (node.HideFromAnonymous) return string.Empty;
// if url is already fully resolved just return it
if (node.Url.StartsWith("http")) return node.Url;
string urlToUse = string.Empty;
if ((node.Action.Length > 0) && (node.Controller.Length > 0))
{
var a = node.Area == null ? "" : node.Area;
urlToUse = urlHelper.Action(node.Action, node.Controller, new {area = a });
}
else if (node.NamedRoute.Length > 0)
{
urlToUse = urlHelper.RouteUrl(node.NamedRoute);
}
if (string.IsNullOrEmpty(urlToUse)) urlToUse = node.Url;
if (urlToUse.StartsWith("http")) return urlToUse;
return BaseUrl + urlToUse;
}
}
}