Files
slp-modular-cms/src/SlpModularCms.Core/Hosting/Website/WebsiteFileAccess.cs
T
SluijsensandClaude Sonnet 5.5 de3032fbbd
Continuous Integration / config-sls (pull_request) Successful in 19s
Continuous Integration / changes (pull_request) Successful in 16s
Continuous Integration / frontend-prepare (pull_request) Skipped
Continuous Integration / frontend-build (pull_request) Skipped
Continuous Integration / frontend-test (pull_request) Skipped
Continuous Integration / frontend-lint (pull_request) Skipped
Continuous Integration / config-asf (pull_request) Successful in 17s
Continuous Integration / compile-sites (pull_request) Successful in 1m23s
Continuous Integration / vulnerability-scan (pull_request) Successful in 1m13s
Continuous Integration / backend-build (pull_request) Successful in 13m17s
Continuous Integration / backend-test (pull_request) Successful in 3m53s
Continuous Integration / publish-sls-test (pull_request) Skipped
Continuous Integration / publish-sls-production (pull_request) Skipped
Continuous Integration / deploy-sls-test (pull_request) Skipped
Continuous Integration / deploy-sls-production (pull_request) Skipped
Continuous Integration / publish-asf-test (pull_request) Skipped
Continuous Integration / deploy-asf-test (pull_request) Skipped
Continuous Integration / smoke-sls-test (pull_request) Skipped
Continuous Integration / smoke-asf-test (pull_request) Skipped
Website: redirect-map (redirects.json) voor oude URL's in PrerenderedPages-modus
301/404/410 per exact pad, hoofdletter- en slot-slash-ongevoelig, afgevlakte ketens, hooguit elke 5 s herlezen,
ongeldig bestand laat de vorige regels staan, bestand zelf niet opvraagbaar.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-18 21:28:01 +01:00

110 lines
3.7 KiB
C#

using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
namespace SlpModularCms.Core.Hosting.Website;
/// <summary>
/// Read-only access to the website files, confined to <c>wwwroot/web</c>.
/// </summary>
/// <remarks>
/// A file provider rather than <c>Path.Combine</c> with request data: <see cref="PhysicalFileProvider"/>
/// refuses anything that resolves outside its root, which is the second line of defence behind
/// <see cref="WebsitePathResolver"/>. Exclusion filters are switched off on purpose, because the
/// default ones hide every dot-prefixed name and would make <c>/.well-known/</c> unreachable; dotfiles
/// are refused earlier, by rule R3 of the resolver.
///
/// The root is looked up on every call instead of once at startup: the website is delivered
/// independently of the host (a CI copy, or a symlink switched to a new release) and may appear or
/// be replaced while the process runs. A missing root, a vanished file or an IO error is "not
/// found", never an exception.
/// </remarks>
public sealed class WebsiteFileAccess : IFileProvider, IDisposable
{
private readonly string _root;
private readonly ILogger _logger;
private readonly Lock _gate = new();
private PhysicalFileProvider? _provider;
private bool _warnedMissing;
public WebsiteFileAccess(string root, ILogger logger)
{
_root = root;
_logger = logger;
}
/// <summary>True when <paramref name="relativePath"/> names an existing file (not a directory).</summary>
public bool Exists(string relativePath)
{
try
{
var info = GetFileInfo(relativePath);
return info.Exists && !info.IsDirectory;
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Checking a website file failed; treating it as not found.");
return false;
}
}
/// <summary>The directory the website is served from (also where <see cref="WebsiteRedirectMap.FileName"/> lives).</summary>
public string RootPath => _root;
public IFileInfo GetFileInfo(string subpath)
{
// The redirect map is data for the host, not a page of the site: it is never served to visitors.
if (string.Equals(subpath.Trim('/'), WebsiteRedirectMap.FileName, StringComparison.OrdinalIgnoreCase))
{
return new NotFoundFileInfo(subpath);
}
var provider = GetProvider();
return provider is null ? new NotFoundFileInfo(subpath) : provider.GetFileInfo(subpath);
}
public IDirectoryContents GetDirectoryContents(string subpath) => NotFoundDirectoryContents.Singleton;
public IChangeToken Watch(string filter) => NullChangeToken.Singleton;
public void Dispose()
{
lock (_gate)
{
_provider?.Dispose();
_provider = null;
}
}
private PhysicalFileProvider? GetProvider()
{
lock (_gate)
{
if (_provider is not null && Directory.Exists(_root))
{
return _provider;
}
_provider?.Dispose();
_provider = null;
if (!Directory.Exists(_root))
{
if (!_warnedMissing)
{
_warnedMissing = true;
_logger.LogWarning(
"The website directory {Root} does not exist; every website path answers 404 until it is delivered.",
_root);
}
return null;
}
_warnedMissing = false;
_provider = new PhysicalFileProvider(_root, Microsoft.Extensions.FileProviders.Physical.ExclusionFilters.None);
return _provider;
}
}
}