♻️ New error page
This commit is contained in:
77
DysonNetwork.Zone/Publication/PublicationSiteMiddleware.cs
Normal file
77
DysonNetwork.Zone/Publication/PublicationSiteMiddleware.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using System.Text.Json;
|
||||
using DysonNetwork.Shared.Models;
|
||||
using Microsoft.AspNetCore.StaticFiles;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace DysonNetwork.Zone.Publication;
|
||||
|
||||
public class PublicationSiteMiddleware(RequestDelegate next)
|
||||
{
|
||||
public const string SiteContextKey = "PubSite";
|
||||
|
||||
public async Task InvokeAsync(HttpContext context, AppDatabase db, PublicationSiteManager psm)
|
||||
{
|
||||
var siteNameValue = context.Request.Headers["X-SiteName"].ToString();
|
||||
var currentPath = context.Request.Path.Value ?? "";
|
||||
|
||||
if (string.IsNullOrEmpty(siteNameValue))
|
||||
{
|
||||
await next(context);
|
||||
return;
|
||||
}
|
||||
|
||||
var site = await db.PublicationSites
|
||||
.FirstOrDefaultAsync(s => EF.Functions.ILike(s.Name, siteNameValue));
|
||||
if (site == null)
|
||||
{
|
||||
await next(context);
|
||||
return;
|
||||
}
|
||||
|
||||
context.Items[SiteContextKey] = site;
|
||||
|
||||
var page = await db.PublicationPages
|
||||
.FirstOrDefaultAsync(p => p.SiteId == site.Id && p.Path == currentPath);
|
||||
if (page != null)
|
||||
{
|
||||
switch (page.Type)
|
||||
{
|
||||
case PublicationPageType.HtmlPage
|
||||
when page.Config.TryGetValue("html", out var html) && html is JsonElement content:
|
||||
context.Response.ContentType = "text/html";
|
||||
await context.Response.WriteAsync(content.ToString());
|
||||
return;
|
||||
case PublicationPageType.Redirect
|
||||
when page.Config.TryGetValue("url", out var url) && url is JsonElement redirectUrl:
|
||||
context.Response.Redirect(redirectUrl.ToString());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// If the site is enabled the self-managed mode, try lookup the files then
|
||||
if (site.Mode == PublicationSiteMode.SelfManaged)
|
||||
{
|
||||
var provider = new FileExtensionContentTypeProvider();
|
||||
var hostedFilePath = psm.GetValidatedFullPath(site.Id, currentPath);
|
||||
if (File.Exists(hostedFilePath))
|
||||
{
|
||||
if (!provider.TryGetContentType(hostedFilePath, out var mimeType))
|
||||
mimeType = "text/html";
|
||||
|
||||
context.Response.ContentType = mimeType;
|
||||
await context.Response.SendFileAsync(hostedFilePath);
|
||||
return;
|
||||
}
|
||||
|
||||
var hostedNotFoundPath = psm.GetValidatedFullPath(site.Id, "404.html");
|
||||
if (File.Exists(hostedNotFoundPath))
|
||||
{
|
||||
context.Response.ContentType = "text/html";
|
||||
await context.Response.SendFileAsync(hostedNotFoundPath);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
await next(context);
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,7 @@ public class PublicationSiteService(
|
||||
if (pub == null) throw new InvalidOperationException("Publisher not found.");
|
||||
pubId = pub.Id;
|
||||
}
|
||||
|
||||
|
||||
return await db.PublicationSites
|
||||
.If(pubId.HasValue, q => q.Where(s => s.PublisherId == pubId.Value))
|
||||
.Include(s => s.Pages)
|
||||
@@ -47,21 +47,31 @@ public class PublicationSiteService(
|
||||
|
||||
public async Task<SnPublicationSite> CreateSite(SnPublicationSite site, Guid accountId)
|
||||
{
|
||||
var perk = (await remoteAccounts.GetAccount(accountId)).PerkSubscription;
|
||||
var perkLevel = perk is not null ? PerkSubscriptionPrivilege.GetPrivilegeFromIdentifier(perk.Identifier) : 0;
|
||||
var existingSlugSite = await db.PublicationSites
|
||||
.AnyAsync(s => EF.Functions.ILike(s.Slug, site.Slug));
|
||||
if (existingSlugSite) throw new InvalidOperationException("Site with the slug already exists.");
|
||||
|
||||
var maxSite = perkLevel switch
|
||||
var account = await remoteAccounts.GetAccount(accountId);
|
||||
if (!account.IsSuperuser)
|
||||
{
|
||||
1 => 2,
|
||||
2 => 3,
|
||||
3 => 5,
|
||||
_ => 1
|
||||
};
|
||||
var perk = account.PerkSubscription;
|
||||
var perkLevel = perk is not null
|
||||
? PerkSubscriptionPrivilege.GetPrivilegeFromIdentifier(perk.Identifier)
|
||||
: 0;
|
||||
|
||||
// Check if account has reached the maximum number of sites
|
||||
var existingSitesCount = await db.PublicationSites.CountAsync(s => s.AccountId == accountId);
|
||||
if (existingSitesCount >= maxSite)
|
||||
throw new InvalidOperationException("Account has reached the maximum number of sites allowed.");
|
||||
var maxSite = perkLevel switch
|
||||
{
|
||||
1 => 2,
|
||||
2 => 3,
|
||||
3 => 5,
|
||||
_ => 1
|
||||
};
|
||||
|
||||
// Check if account has reached the maximum number of sites
|
||||
var existingSitesCount = await db.PublicationSites.CountAsync(s => s.AccountId == accountId);
|
||||
if (existingSitesCount >= maxSite)
|
||||
throw new InvalidOperationException("Account has reached the maximum number of sites allowed.");
|
||||
}
|
||||
|
||||
// Check if account is member of the publisher
|
||||
var isMember = await publisherService.IsMemberWithRole(site.PublisherId, accountId, PublisherMemberRole.Editor);
|
||||
@@ -205,4 +215,4 @@ public class PublicationSiteService(
|
||||
var defaultPage = site.Pages.FirstOrDefault(p => p.Path == "/");
|
||||
return defaultPage;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user