#161 building sitemap
This commit is contained in:
parent
2175762a1a
commit
c443628ca1
@ -1,3 +1,3 @@
|
||||
User-agent: *
|
||||
Disallow:
|
||||
Sitemap: http://ganjoor.net/sitemap.xml.gz
|
||||
Sitemap: http://ganjoor.net/sitemap.xml
|
||||
@ -1444,6 +1444,26 @@ namespace RMuseum.Controllers
|
||||
return BadRequest(res.ExceptionString);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// start building sitemap
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
|
||||
[HttpPost("sitemap")]
|
||||
[Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + SecurableItem.ModifyOperationShortName)]
|
||||
[ProducesResponseType((int)HttpStatusCode.OK)]
|
||||
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
|
||||
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||
public IActionResult StartBuildingSitemap()
|
||||
{
|
||||
var res = _ganjoorService.StartBuildingSitemap();
|
||||
|
||||
if (!string.IsNullOrEmpty(res.ExceptionString))
|
||||
return BadRequest(res.ExceptionString);
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ganjoor Service
|
||||
/// </summary>
|
||||
|
||||
@ -1089,6 +1089,12 @@
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Controllers.GanjoorController.StartBuildingSitemap">
|
||||
<summary>
|
||||
start building sitemap
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="F:RMuseum.Controllers.GanjoorController._ganjoorService">
|
||||
<summary>
|
||||
Ganjoor Service
|
||||
@ -7588,6 +7594,12 @@
|
||||
<param name="catId"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Services.IGanjoorService.StartBuildingSitemap">
|
||||
<summary>
|
||||
build sitemap
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="T:RMuseum.Services.Implementation.ArtifactService">
|
||||
<summary>
|
||||
IArtifactService implementation
|
||||
@ -8549,6 +8561,9 @@
|
||||
<summary>
|
||||
IGanjoorService implementation
|
||||
</summary>
|
||||
<summary>
|
||||
IGanjoorService implementation
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:RMuseum.Services.Implementation.GanjoorService.ImportFromMySql">
|
||||
<summary>
|
||||
@ -8556,6 +8571,12 @@
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Services.Implementation.GanjoorService.StartBuildingSitemap">
|
||||
<summary>
|
||||
build sitemap
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Services.Implementation.GanjoorService.ExportToSqlite(System.Int32)">
|
||||
<summary>
|
||||
export to sqlite
|
||||
|
||||
@ -439,5 +439,12 @@ namespace RMuseum.Services
|
||||
/// <param name="catId"></param>
|
||||
/// <returns></returns>
|
||||
RServiceResult<bool> StartGeneratingSubCatsTOC(Guid userId, int catId);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// build sitemap
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
RServiceResult<bool> StartBuildingSitemap();
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,134 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using RMuseum.DbContext;
|
||||
using RSecurityBackend.Models.Generic;
|
||||
using RSecurityBackend.Services.Implementation;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace RMuseum.Services.Implementation
|
||||
{
|
||||
/// <summary>
|
||||
/// IGanjoorService implementation
|
||||
/// </summary>
|
||||
public partial class GanjoorService : IGanjoorService
|
||||
{
|
||||
|
||||
private void WriteSitemap(string filePath, List<string> urls)
|
||||
{
|
||||
if (File.Exists(filePath))
|
||||
File.Delete(filePath);
|
||||
XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
|
||||
XNamespace xsiNs = "http://www.w3.org/2001/XMLSchema-instance";
|
||||
|
||||
XDocument xDoc = new XDocument(
|
||||
new XDeclaration("1.0", "UTF-8", "no"),
|
||||
new XElement(ns + "urlset",
|
||||
new XAttribute(XNamespace.Xmlns + "xsi", xsiNs),
|
||||
new XAttribute(xsiNs + "schemaLocation",
|
||||
"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"),
|
||||
from url in urls
|
||||
select new XElement(ns + "url",
|
||||
new XElement(ns + "loc", $"https://ganjoor.net{url}"))
|
||||
)
|
||||
);
|
||||
|
||||
xDoc.Save(filePath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// build sitemap
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public RServiceResult<bool> StartBuildingSitemap()
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
_backgroundTaskQueue.QueueBackgroundWorkItem
|
||||
(
|
||||
async token =>
|
||||
{
|
||||
using (RMuseumDbContext context = new RMuseumDbContext(new DbContextOptions<RMuseumDbContext>())) //this is long running job, so _context might be already been freed/collected by GC
|
||||
{
|
||||
LongRunningJobProgressServiceEF jobProgressServiceEF = new LongRunningJobProgressServiceEF(context);
|
||||
var job = (await jobProgressServiceEF.NewJob("BuildSitemap", "Query data")).Result;
|
||||
try
|
||||
{
|
||||
string xmlSitemap = _configuration.GetSection("Ganjoor")["SitemapLocation"];
|
||||
if (File.Exists(xmlSitemap))
|
||||
File.Delete(xmlSitemap);
|
||||
|
||||
string dir = Path.GetDirectoryName(xmlSitemap);
|
||||
|
||||
await jobProgressServiceEF.UpdateJob(job.Id, 0, "", false);
|
||||
|
||||
List<string> sitemaps = new List<string>();
|
||||
|
||||
string firstSitemap = Path.Combine(dir, $"1.xml");
|
||||
|
||||
sitemaps.Add(firstSitemap);
|
||||
|
||||
var urls = await context.GanjoorPages.Where(p => p.PoetId == null).OrderBy(p => p.Id).Select(p => p.FullUrl).ToListAsync();
|
||||
|
||||
urls.Remove("/audioclip");
|
||||
urls.Remove("/simi");
|
||||
urls.Remove("/tags");
|
||||
urls.Remove("/amar");
|
||||
urls.Remove("/tools");
|
||||
urls.Remove("/embed");
|
||||
|
||||
WriteSitemap(firstSitemap,
|
||||
urls
|
||||
);
|
||||
|
||||
foreach (var poet in await context.GanjoorPoets.ToListAsync())
|
||||
{
|
||||
await jobProgressServiceEF.UpdateJob(job.Id, poet.Id, "", false);
|
||||
|
||||
string poetSitemap = Path.Combine(dir, $"{poet.Id}.xml");
|
||||
|
||||
WriteSitemap(poetSitemap,
|
||||
await context.GanjoorPages.Where(p => p.PoetId == poet.Id).OrderBy(p => p.Id).Select(p => p.FullUrl).ToListAsync()
|
||||
);
|
||||
|
||||
sitemaps.Add(poetSitemap);
|
||||
}
|
||||
|
||||
XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
|
||||
XNamespace xsiNs = "http://www.w3.org/2001/XMLSchema-instance";
|
||||
XDocument xDoc = new XDocument(
|
||||
new XDeclaration("1.0", "UTF-8", "no"),
|
||||
new XElement(ns + "sitemapindex",
|
||||
new XAttribute(XNamespace.Xmlns + "xsi", xsiNs),
|
||||
new XAttribute(xsiNs + "schemaLocation",
|
||||
"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"),
|
||||
from sitemap in sitemaps
|
||||
select new XElement(ns + "sitemap",
|
||||
new XElement(ns + "loc", $"https://ganjoor.net/{Path.GetFileName(sitemap)}"))
|
||||
)
|
||||
);
|
||||
|
||||
xDoc.Save(xmlSitemap);
|
||||
|
||||
await jobProgressServiceEF.UpdateJob(job.Id, 100, "", true);
|
||||
}
|
||||
catch(Exception exp)
|
||||
{
|
||||
await jobProgressServiceEF.UpdateJob(job.Id, 100, "", false, exp.ToString());
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
return new RServiceResult<bool>(true);
|
||||
}
|
||||
catch(Exception exp)
|
||||
{
|
||||
return new RServiceResult<bool>(false, exp.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -71,7 +71,8 @@
|
||||
"ShowAccountInfo": "True"
|
||||
},
|
||||
"Ganjoor": {
|
||||
"SystemEmail": "admin@ganjoor.net"
|
||||
"SystemEmail": "admin@ganjoor.net",
|
||||
"SitemapLocation": "C:\\inetpub\\ganjoor\\wwwroot\\sitemap.xml"
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user