From 7016a0a9433f87e41b15c6f4221a91c5d8c54b69 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Fri, 21 Nov 2025 01:55:22 +0800 Subject: [PATCH] :sparkles: Render self-managed site --- DysonNetwork.Zone/.gitignore | 3 +- DysonNetwork.Zone/Pages/Index.cshtml | 3 +- DysonNetwork.Zone/Pages/Index.cshtml.cs | 48 ++++++++++++++----- .../Publication/PublicationSiteManager.cs | 8 ++-- DysonNetwork.Zone/appsettings.json | 2 +- DysonNetwork.sln.DotSettings.user | 3 +- 6 files changed, 45 insertions(+), 22 deletions(-) diff --git a/DysonNetwork.Zone/.gitignore b/DysonNetwork.Zone/.gitignore index 1d8c562..705ea43 100644 --- a/DysonNetwork.Zone/.gitignore +++ b/DysonNetwork.Zone/.gitignore @@ -1,6 +1,7 @@ # dependencies (bun install) node_modules wwwroot/css/site.dist.css +wwwroot/SiteData # output out @@ -33,5 +34,3 @@ report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json # Finder (MacOS) folder config .DS_Store - -SiteData diff --git a/DysonNetwork.Zone/Pages/Index.cshtml b/DysonNetwork.Zone/Pages/Index.cshtml index e21c7e2..9235908 100644 --- a/DysonNetwork.Zone/Pages/Index.cshtml +++ b/DysonNetwork.Zone/Pages/Index.cshtml @@ -16,7 +16,8 @@ haven't configure anything that match this route.

-

Path: @Model.CurrentPath Site ID: @(Model.Site?.Id.ToString() ?? "none")

+

Path: @Model.CurrentPath

+

Site ID: @(Model.Site?.Id.ToString() ?? "none")

diff --git a/DysonNetwork.Zone/Pages/Index.cshtml.cs b/DysonNetwork.Zone/Pages/Index.cshtml.cs index 975424a..861f8c4 100644 --- a/DysonNetwork.Zone/Pages/Index.cshtml.cs +++ b/DysonNetwork.Zone/Pages/Index.cshtml.cs @@ -1,13 +1,14 @@ using DysonNetwork.Shared.Models; +using DysonNetwork.Zone.Publication; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; +using Microsoft.AspNetCore.StaticFiles; using Microsoft.EntityFrameworkCore; -using System.IO; using System.Text.Json; namespace DysonNetwork.Zone.Pages; -public class IndexModel(AppDatabase db) : PageModel +public class IndexModel(AppDatabase db, PublicationSiteManager psm) : PageModel { public SnPublicationSite? Site { get; set; } public string? SiteName { get; set; } @@ -21,25 +22,46 @@ public class IndexModel(AppDatabase db) : PageModel if (string.IsNullOrEmpty(siteNameValue)) { SiteName = null; + return Page(); } - else - { - var capturedName = siteNameValue; - Site = await db.PublicationSites.FirstOrDefaultAsync(s => s.Slug == capturedName); - if (Site == null) return Page(); - var pagePath = CurrentPath; - var page = await db.PublicationPages.FirstOrDefaultAsync(p => p.SiteId == Site.Id && p.Path == pagePath); - if (page == null) return Page(); + var capturedName = siteNameValue; + Site = await db.PublicationSites.FirstOrDefaultAsync(s => s.Slug == capturedName); + if (Site == null) return Page(); + var pagePath = CurrentPath; + + var page = await db.PublicationPages.FirstOrDefaultAsync(p => p.SiteId == Site.Id && p.Path == pagePath); + if (page != null) + { switch (page.Type) { - case PublicationPageType.HtmlPage when page.Config.TryGetValue("html", out var html) && html is JsonElement content: + case PublicationPageType.HtmlPage + when page.Config.TryGetValue("html", out var html) && html is JsonElement content: return Content(content.ToString(), "text/html"); - case PublicationPageType.Redirect when page.Config.TryGetValue("url", out var url) && url is JsonElement redirectUrl: + case PublicationPageType.Redirect + when page.Config.TryGetValue("url", out var url) && url is JsonElement redirectUrl: return Redirect(redirectUrl.ToString()); } } + // If the site is enabled the self-managed mode, try lookup the files then + if (Site.Mode == PublicationSiteMode.SelfManaged) + { + // TODO: Fix the path contains a wwwroot + var provider = new FileExtensionContentTypeProvider(); + var hostedFilePath = psm.GetValidatedFullPath(Site.Id, CurrentPath); + if (System.IO.File.Exists(hostedFilePath)) + { + if (!provider.TryGetContentType(hostedFilePath, out var mimeType)) + mimeType = "text/html"; + return File(hostedFilePath, mimeType); + } + + var hostedNotFoundPath = psm.GetValidatedFullPath(Site.Id, "404.html"); + if (System.IO.File.Exists(hostedNotFoundPath)) + return File(hostedNotFoundPath, "text/html"); + } + return Page(); } -} +} \ No newline at end of file diff --git a/DysonNetwork.Zone/Publication/PublicationSiteManager.cs b/DysonNetwork.Zone/Publication/PublicationSiteManager.cs index aec41c7..9fb34dc 100644 --- a/DysonNetwork.Zone/Publication/PublicationSiteManager.cs +++ b/DysonNetwork.Zone/Publication/PublicationSiteManager.cs @@ -25,10 +25,10 @@ public class PublicationSiteManager( { // Treat paths starting with separator as relative to site root relativePath = relativePath.TrimStart('/', '\\'); - string fullPath = Path.Combine(_basePath, siteId.ToString(), relativePath); - string normalizedPath = Path.GetFullPath(fullPath); - string siteDirFull = Path.Combine(_basePath, siteId.ToString()); - string normalizedSiteDir = Path.GetFullPath(siteDirFull); + var fullPath = Path.Combine(_basePath, siteId.ToString(), relativePath); + var normalizedPath = Path.GetFullPath(fullPath); + var siteDirFull = Path.Combine(_basePath, siteId.ToString()); + var normalizedSiteDir = Path.GetFullPath(siteDirFull); if (!normalizedPath.StartsWith(normalizedSiteDir + Path.DirectorySeparatorChar) && !normalizedPath.Equals(normalizedSiteDir)) { diff --git a/DysonNetwork.Zone/appsettings.json b/DysonNetwork.Zone/appsettings.json index f669dd6..7dd2956 100644 --- a/DysonNetwork.Zone/appsettings.json +++ b/DysonNetwork.Zone/appsettings.json @@ -15,6 +15,6 @@ "PublicBasePath": "/zone" }, "Sites": { - "BasePath": "/SiteData" + "BasePath": "SiteData" } } diff --git a/DysonNetwork.sln.DotSettings.user b/DysonNetwork.sln.DotSettings.user index e96d2ca..1150cff 100644 --- a/DysonNetwork.sln.DotSettings.user +++ b/DysonNetwork.sln.DotSettings.user @@ -1,4 +1,4 @@ - + ForceIncluded ForceIncluded ForceIncluded @@ -117,6 +117,7 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded