1
1
using Microsoft . AspNetCore . Authorization ;
2
2
using Microsoft . AspNetCore . Mvc ;
3
+ using Microsoft . EntityFrameworkCore ;
3
4
using System . Diagnostics ;
5
+ using System . Xml ;
6
+ using Waffle . Data ;
7
+ using Waffle . Extensions ;
4
8
using Waffle . Foundations ;
5
9
6
10
namespace Waffle . Controllers ;
7
11
8
12
public class MetaController : BaseController
9
13
{
14
+ private readonly ApplicationDbContext _context ;
15
+ private readonly IWebHostEnvironment _webHostEnvironment ;
16
+
17
+ public MetaController ( ApplicationDbContext context , IWebHostEnvironment webHostEnvironment )
18
+ {
19
+ _context = context ;
20
+ _webHostEnvironment = webHostEnvironment ;
21
+ }
22
+
10
23
[ HttpGet ( "/info" ) , AllowAnonymous ]
11
24
public IActionResult Info ( )
12
25
{
@@ -17,4 +30,39 @@ public IActionResult Info()
17
30
18
31
return Ok ( $ "Version: { version } , Last Updated: { creationDate } ") ;
19
32
}
33
+
34
+ [ HttpGet ( "sitemap-generator" ) , AllowAnonymous ]
35
+ public async Task < IActionResult > SitemapGeneratorAsync ( )
36
+ {
37
+ var catalogs = await _context . Catalogs . Where ( x => x . Active ) . ToListAsync ( ) ;
38
+ // Create an XmlWriter to generate the sitemap XML file
39
+ var path = Path . Combine ( _webHostEnvironment . WebRootPath , "sitemap.xml" ) ;
40
+ using ( XmlWriter writer = XmlWriter . Create ( path ) )
41
+ {
42
+ // Start the document
43
+ writer . WriteStartDocument ( ) ;
44
+ writer . WriteStartElement ( "urlset" , "http://www.sitemaps.org/schemas/sitemap/0.9" ) ;
45
+
46
+ // Add URLs and metadata
47
+ foreach ( var item in catalogs )
48
+ {
49
+ var url = item . GetUrl ( ) ;
50
+ AddUrl ( writer , $ "https://{ Request . Host . Value } { url } ", item . ModifiedDate . ToString ( "yyyy-MM-dd" ) , "0.8" ) ;
51
+ }
52
+
53
+ // End the document
54
+ writer . WriteEndElement ( ) ;
55
+ writer . WriteEndDocument ( ) ;
56
+ }
57
+ return Ok ( ) ;
58
+ }
59
+
60
+ static void AddUrl ( XmlWriter writer , string url , string lastModified , string priority )
61
+ {
62
+ writer . WriteStartElement ( "url" ) ;
63
+ writer . WriteElementString ( "loc" , url ) ;
64
+ writer . WriteElementString ( "lastmod" , lastModified ) ;
65
+ writer . WriteElementString ( "priority" , priority ) ;
66
+ writer . WriteEndElement ( ) ;
67
+ }
20
68
}
0 commit comments