Migrate Laysense to upstream LaySite 1.4.3 with external site data

This commit is contained in:
2026-11-27 17:47:31 +08:00
parent b8eb9b35eb
commit 1d2cb2ed1e
277 changed files with 9049 additions and 3222 deletions
+155
View File
@@ -0,0 +1,155 @@
<?php
declare(strict_types=1);
namespace LaysenseSite;
use App\Config\Environment;
use App\Content\Assets;
use App\Content\Repository;
use App\Content\View;
use Webman\Http\Request;
use Webman\Http\Response;
use Webman\MiddlewareInterface;
final class Site implements MiddlewareInterface
{
private const FAVICON = '/assets/favicon.png';
private const SOCIAL_IMAGE = '/assets/original/home-hero.webp';
private const MIME = [
'css' => 'text/css; charset=utf-8', 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg',
'png' => 'image/png', 'webp' => 'image/webp', 'svg' => 'image/svg+xml', 'mp4' => 'video/mp4',
];
private const PAGE_REDIRECTS = [
'/main' => '/articles',
'/articles/category/services' => '/articles/category/catalog',
'/articles/category/supported-projects' => '/articles/category/projects',
];
private const SECTION_STYLES = ['home' => ['product' => 'projects', 'about' => 'about'], 'pagepan' => ['demo' => 'demos']];
private const PHOTO_CREDITS = [
'/assets/page-templates/studio-letterpress.webp' => ['Adam Lusch', 'CC BY-SA 2.0', 'https://commons.wikimedia.org/wiki/File:Wood_Type_close-up.jpg', 'https://creativecommons.org/licenses/by-sa/2.0/'],
'/assets/page-templates/dusk-dunes.webp' => ['Bob Wick / BLM Utah', '公有领域', 'https://commons.wikimedia.org/wiki/File:Utah_Dunes_Landscape_-_West_Desert_District.jpg', ''],
];
public static function assets(): array
{
static $assets;
return $assets ??= ['assets/laysense-site-compat.css', ...json_decode(file_get_contents(__DIR__ . '/assets.json'), true, flags: JSON_THROW_ON_ERROR)];
}
public function process(Request $request, callable $handler): Response
{
$path = $request->path();
$asset = ltrim($path, '/');
$read = in_array($request->method(), ['GET', 'HEAD'], true);
if ($read && in_array($asset, self::assets(), true)) {
return $this->legacyAsset($request, $asset);
}
$response = $handler($request);
if ($read && isset(self::PAGE_REDIRECTS[$path]) && $response->getStatusCode() === 404) {
return View::redirect(self::PAGE_REDIRECTS[$path], 301);
}
if ($read && $path === '/favicon.ico' && $response->getStatusCode() === 301) {
return $response->withHeader('Location', self::FAVICON);
}
$preview = preg_match('~^/admin/pages/([a-z0-9-]+)/preview$~D', $path, $match) === 1;
if (!$read || (str_starts_with($path, '/admin') && !$preview)
|| !str_starts_with((string) $response->getHeader('Content-Type'), 'text/html')
|| !str_contains($body = $response->rawBody(), '</head>')) {
return $response;
}
$slug = $preview ? $match[1] : ($path === '/' ? 'home' : trim($path, '/'));
$body = $this->brand($body, $preview);
$body = $this->theme($body, $slug, $preview && $request->get('canvas') === '1');
return $response->withBody($body);
}
private function legacyAsset(Request $request, string $asset): Response
{
if ($asset === 'assets/laysense-site-compat.css') {
return (new Response(200, ['Content-Type' => self::MIME['css'], 'X-Content-Type-Options' => 'nosniff']))
->withFile(__DIR__ . '/compat.css');
}
$directory = realpath(Environment::path('LAYSENSE_SITE_DIR', 'storage/laysense-site') . '/assets');
$path = $directory === false ? false : realpath($directory . '/' . substr($asset, strlen('assets/')));
if ($path === false || !str_starts_with($path, $directory . '/') || !is_file($path)) {
return new Response(404);
}
$size = filesize($path);
$headers = [
'Content-Type' => self::MIME[pathinfo($asset, PATHINFO_EXTENSION)] ?? 'application/octet-stream',
'X-Content-Type-Options' => 'nosniff',
'Content-Security-Policy' => "default-src 'none'; sandbox",
'Cache-Control' => 'public, max-age=3600',
'Access-Control-Allow-Origin' => '*',
'Accept-Ranges' => 'bytes',
];
$range = $request->header('range');
if ($range === null) {
return (new Response(200, $headers))->withFile($path);
}
$invalid = new Response(416, $headers + ['Content-Range' => "bytes */$size"]);
if (!preg_match('/^bytes=(\d*)-(\d*)$/D', $range, $parts) || ($parts[1] === '' && $parts[2] === '')) {
return $invalid;
}
$start = $parts[1] === '' ? max(0, $size - (int) $parts[2]) : (int) $parts[1];
$stop = $parts[1] === '' || $parts[2] === '' ? $size - 1 : min($size - 1, (int) $parts[2]);
if ($start > $stop) {
return $invalid;
}
return (new Response(206, $headers + ['Content-Range' => "bytes $start-$stop/$size"]))
->withFile($path, $start, $stop - $start + 1);
}
private function brand(string $html, bool $preview): string
{
$cdn = $preview ? '' : ((new Repository())->site()['cdn']['origin'] ?? '');
$favicon = View::escape(Assets::url(self::FAVICON, $cdn));
$html = preg_replace('~<link rel="icon"[^>]+>~', '<link rel="icon" href="' . $favicon . '" type="image/png">', $html, 1) ?? $html;
$style = '<link rel="stylesheet" href="/assets/laysense.css"><link rel="stylesheet" href="/assets/laysense-site-compat.css">';
if (!str_contains(substr($html, 0, strpos($html, '</head>')), 'property="og:image"')) {
$url = $cdn === '' ? View::absolute(self::SOCIAL_IMAGE) : Assets::url(self::SOCIAL_IMAGE, $cdn);
$style .= '<meta property="og:image" content="' . View::escape($url) . '">'
. '<meta property="og:image:alt" content="来笙 Laysense">';
}
return preg_replace('~</head>~', $style . '</head>', $html, 1) ?? $html;
}
private function theme(string $html, string $slug, bool $canvas): string
{
if (in_array($slug, ['sc', 'shnu-sc'], true)) {
$html = str_replace(['<body>', 'class="hero-visual app-frame'], ['<body class="copyright-service">', 'class="hero-visual app-frame hero-service-art'], $html);
}
if ($slug === 'home') {
$html = preg_replace('~class="hero-visual app-frame~', 'class="hero-visual hero-illustration', $html, 1) ?? $html;
if (!$canvas) {
$html = preg_replace('~(<div class="hero-copy">.*)</h1>~sU', '$1<span class="home-cursor" aria-hidden="true">_</span></h1>', $html, 1) ?? $html;
$html = preg_replace_callback('~<div class="hero-visual hero-illustration[^>]*>\s*<img\b[^>]+src="[^"]*/assets/original/home-hero\.webp"[^>]*>~',
static fn (array $match): string => $match[0] . file_get_contents(__DIR__ . '/home-motion.svg'), $html, 1) ?? $html;
}
}
foreach (self::SECTION_STYLES[$slug] ?? [] as $id => $style) {
$html = preg_replace('~<section class="(section [^"]*)" id="' . $id . '"~',
'<section class="$1 section-' . $style . '" id="' . $id . '"' . ($style === 'about' ? ' data-moment="iris-opens"' : ''),
$html, 1) ?? $html;
}
if ($slug === 'home' || $slug === 'network') {
$html = str_replace(['class="network-group" id="projects"', 'class="network-group" id="network-group-projects"', 'class="network-group" id="network-group-partners"'],
['class="network-group" id="supported-projects"', 'class="network-group" id="supported-projects"', 'class="network-group" id="partners"'], $html);
}
foreach (self::PHOTO_CREDITS as $image => [$author, $license, $source, $terms]) {
if (preg_match('~class="hero-visual [^"]*"[^>]*>\s*<img[^>]*src="[^"]*' . preg_quote($image, '~') . '"~', $html)) {
$credit = '<p class="wrap page-photo-credit">摄影 <a href="' . View::escape($source) . '">' . View::escape($author) . '</a> · '
. ($terms === '' ? View::escape($license) : '<a href="' . View::escape($terms) . '">' . View::escape($license) . '</a>') . '</p>';
$html = str_replace('</main>', $credit . '</main>', $html);
break;
}
}
return $html;
}
}
+65
View File
@@ -0,0 +1,65 @@
[
"assets/favicon.png",
"assets/laysense.css",
"assets/original/10-1.png",
"assets/original/10-2.png",
"assets/original/10-3.png",
"assets/original/1s.png",
"assets/original/DNSPod.svg",
"assets/original/IP.png",
"assets/original/IdealShark_B.png",
"assets/original/RAM.png",
"assets/original/Redis.png",
"assets/original/backup.png",
"assets/original/chinadomain.png",
"assets/original/component-element.mp4",
"assets/original/copyright-wechat.png",
"assets/original/copyright.webp",
"assets/original/download.png",
"assets/original/drag-in-components.mp4",
"assets/original/edit-components.mp4",
"assets/original/fast.png",
"assets/original/footer-wordmark.webp",
"assets/original/fw-banner.svg",
"assets/original/highspeaker-logo-optimized.webp",
"assets/original/highspeaker-ui-optimized.webp",
"assets/original/highspeakerrz-optimized.webp",
"assets/original/home-hero.webp",
"assets/original/icon_rewrite.png",
"assets/original/jeo-optimized.webp",
"assets/original/locate.png",
"assets/original/logo_img.png",
"assets/original/mimirlib.png",
"assets/original/nonat-optimized.webp",
"assets/original/oneprovider_invert.svg",
"assets/original/orange-optimized.webp",
"assets/original/pagepan-editor-optimized.webp",
"assets/original/pagepan-example.webp",
"assets/original/pagepan-wechat.png",
"assets/original/pagepan/cdn.svg",
"assets/original/pagepan/cms.svg",
"assets/original/pagepan/code.svg",
"assets/original/pagepan/files.svg",
"assets/original/pagepan/fonts.svg",
"assets/original/pagepan/gradient.svg",
"assets/original/pagepan/languages.svg",
"assets/original/pagepan/seo.svg",
"assets/original/pagepan/sitemap.svg",
"assets/original/papepan-logo.svg",
"assets/original/payment-alipay.png",
"assets/original/payment-taobao.png",
"assets/original/payment-wechat.png",
"assets/original/qywx.png",
"assets/original/shnu-hero.webp",
"assets/original/smartroute.png",
"assets/original/sorting-components.mp4",
"assets/original/teo-optimized.webp",
"assets/original/traffic.png",
"assets/original/waf.png",
"assets/page-templates/dusk-dunes.webp",
"assets/page-templates/studio-letterpress.webp",
"assets/service/code.svg",
"assets/service/project.svg",
"assets/service/support.svg",
"assets/wordmark.webp"
]
+1
View File
@@ -0,0 +1 @@
.footer-company .site-address { margin: .25rem 0 0; color: var(--muted); max-width: 26rem; font-style: italic; }
+3
View File
@@ -0,0 +1,3 @@
<?php
return ['enable' => true];
+3
View File
@@ -0,0 +1,3 @@
<?php
return ['files' => [dirname(__DIR__) . '/Site.php']];
+3
View File
@@ -0,0 +1,3 @@
<?php
return ['@' => [LaysenseSite\Site::class]];
+32
View File
@@ -0,0 +1,32 @@
<?php
use App\Content\Repository;
use App\Content\View;
use App\Controller\SiteController;
use LaysenseSite\Site;
use support\Request;
use Webman\Route;
foreach (Site::assets() as $asset) {
// The global site middleware serves these entries before the maintenance-page middleware.
Route::add(['GET', 'HEAD'], '/' . $asset, static fn () => response('', 404));
}
foreach ([
'/main/category/1.html' => '/articles/category/product',
'/main/category/2.html' => '/articles/category/news',
'/main/category/3.html' => '/articles/category/announcement',
'/main/category/5.html' => '/#supported-projects',
'/main/category/6.html' => '/#partners',
] as $old => $new) {
Route::add(['GET', 'HEAD'], $old, static fn () => View::redirect($new, 301));
}
Route::add(['GET', 'HEAD'], '/main/tag/{tag:[^/]+}', static function (Request $request, string $tag) {
return View::redirect('/articles/tag/' . rawurlencode(rawurldecode($tag)), 301);
});
Route::add(['GET', 'HEAD'], '/news/id/{id:[0-9]+}.html', static function (Request $request, string $id) {
$article = (new Repository())->legacyArticle('/news/id/' . $id . '.html');
return $article ? View::redirect('/articles/' . $article['slug'], 301) : (new SiteController())->missing($request);
});
+12
View File
@@ -0,0 +1,12 @@
<svg class="home-pipe-flow" viewBox="0 0 640 480" aria-hidden="true" focusable="false">
<defs>
<path id="hero-pipe-tower" pathLength="100" d="M174 347 241 312 Q246 310 246 305 L246 299 Q246 295 252 293 L273 282"/>
<path id="hero-pipe-disk" pathLength="100" d="M409 282 428 293 Q433 296 433 300 L433 304 Q433 307 438 309 L500 337"/>
<path id="hero-pipe-node" pathLength="100" d="M119 201 125 205 Q129 207 129 212 L129 214 Q130 217 134 219 L190 243"/>
<path id="hero-pipe-chart" pathLength="100" d="M443 226 471 213 Q478 208 483 209 L488 207 Q492 205 492 200 L492 182"/>
</defs>
<g class="home-pipe-route"><use href="#hero-pipe-tower"/><use href="#hero-pipe-tower"/></g>
<g class="home-pipe-route"><use href="#hero-pipe-disk"/><use href="#hero-pipe-disk"/></g>
<g class="home-pipe-route"><use href="#hero-pipe-node"/><use href="#hero-pipe-node"/></g>
<g class="home-pipe-route"><use href="#hero-pipe-chart"/><use href="#hero-pipe-chart"/></g>
</svg>

After

Width:  |  Height:  |  Size: 1004 B