feat: refine forum settings uploads and transitions

This commit is contained in:
lezi-fun committed 2026-08-14 10:16:34 +08:00
1 parent e85ced7319
commit f5d173350b
23 files changed
+985 -54

No files matched your search

+58
View File
@@ -0,0 +1,58 @@
import pool from '@/lib/db';
import { randomUUID } from 'crypto';
import { getUser, requireAdmin } from '@/lib/auth';
import { sniffImage } from '@/lib/image-type';
import { deleteObject, saveObject } from '@/lib/storage';
export const runtime = 'nodejs';
const MAX_SIZE = 5 * 1024 * 1024;
export async function POST(req) {
const user = getUser(req);
const forbidden = await requireAdmin(user);
if (forbidden) return forbidden;
const form = await req.formData();
const file = form.get('file');
if (!file || typeof file === 'string') {
return Response.json({ error: '请选择图标文件' }, { status: 400 });
}
if (file.size > MAX_SIZE) {
return Response.json({ error: '图标大小不能超过 5MB' }, { status: 400 });
}
const buffer = Buffer.from(await file.arrayBuffer());
const image = sniffImage(buffer, { allowIcon: true });
if (!image) {
return Response.json({ error: '仅支持 PNG、JPEG、GIF、WebP 或 ICO 图标' }, { status: 400 });
}
const objectName = `forumlify-favicon-${randomUUID()}${image.ext}`;
let oldObject = '';
try {
const current = await pool.query("SELECT value FROM settings WHERE key = 'favicon_object'");
oldObject = current.rows[0]?.value || '';
const { url } = await saveObject(objectName, buffer, image.mime);
const version = Date.now().toString();
try {
await pool.query(
`INSERT INTO settings (key, value) VALUES ($1, $2), ($3, $4), ($5, $6)
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, updated_at = NOW()`,
['favicon_url', url, 'favicon_version', version, 'favicon_object', objectName]
);
} catch (error) {
await deleteObject(objectName).catch(() => {});
throw error;
}
if (oldObject && /^forumlify-favicon-[\w-]+\.(?:png|jpg|gif|webp|ico)$/.test(oldObject)) {
await deleteObject(oldObject).catch(() => {});
}
return Response.json({ favicon_url: url, favicon_version: version });
} catch {
return Response.json({ error: '上传失败,请稍后重试' }, { status: 500 });
}
}
+4
View File
@@ -3,6 +3,8 @@ import pool from '@/lib/db';
import { getUser, requireAdmin } from '@/lib/auth';
import { jsonWithEtag } from '@/lib/http-cache';
const VERSION_COMMIT = (process.env.FORUMLIFY_COMMIT || 'unknown').slice(0, 7);
// 避免 GET 被静态优化导致 PUT 405(动态接口,不能缓存)
export const dynamic = 'force-dynamic';
@@ -16,8 +18,10 @@ export async function GET(req) {
const r = await pool.query('SELECT key, value FROM settings ORDER BY key');
const settings = {};
r.rows.forEach((row) => { settings[row.key] = row.value; });
delete settings.favicon_object;
// 兜底:即使插入失败也保证返回 forum_name
if (!settings.forum_name) settings.forum_name = 'Forumlify';
settings.version_commit = VERSION_COMMIT;
return jsonWithEtag(req, settings);
} catch {
return Response.json({ error: '服务器错误' }, { status: 500 });
+1 -14
View File
@@ -3,24 +3,11 @@
import path from 'path';
import { randomUUID } from 'crypto';
import { getUser } from '@/lib/auth';
import { sniffImage } from '@/lib/image-type';
import { saveObject } from '@/lib/storage';
const MAX_SIZE = 5 * 1024 * 1024;
// 从文件头字节嗅探真实图片类型(JPEG/PNG/GIF/WebP),拒绝伪造内容
function sniffImage(buf) {
if (buf.length < 12) return null;
// JPEG: FF D8 FF
if (buf[0] === 0xff && buf[1] === 0xd8 && buf[2] === 0xff) return { ext: '.jpg', mime: 'image/jpeg' };
// PNG: 89 50 4E 47 0D 0A 1A 0A
if (buf[0] === 0x89 && buf[1] === 0x50 && buf[2] === 0x4e && buf[3] === 0x47 && buf[4] === 0x0d && buf[5] === 0x0a && buf[6] === 0x1a && buf[7] === 0x0a) return { ext: '.png', mime: 'image/png' };
// GIF: "GIF8"
if (buf[0] === 0x47 && buf[1] === 0x49 && buf[2] === 0x46 && buf[3] === 0x38) return { ext: '.gif', mime: 'image/gif' };
// WebP: "RIFF" .... "WEBP"
if (buf[0] === 0x52 && buf[1] === 0x49 && buf[2] === 0x46 && buf[3] === 0x46 && buf[8] === 0x57 && buf[9] === 0x45 && buf[10] === 0x42 && buf[11] === 0x50) return { ext: '.webp', mime: 'image/webp' };
return null;
}
export const runtime = 'nodejs';
export async function POST(req) {
+1
View File
@@ -12,6 +12,7 @@ const MIME = {
'.jpeg': 'image/jpeg',
'.gif': 'image/gif',
'.webp': 'image/webp',
'.ico': 'image/x-icon',
};
export async function GET(req, { params }) {
+293 -3
View File
@@ -94,6 +94,10 @@
}
/* ===== 全局基础 ===== */
html {
scrollbar-gutter: stable;
}
* {
margin: 0;
padding: 0;
@@ -510,6 +514,12 @@ svg {
margin-right: 6px;
}
.new-post-launch-label,
.new-post-title-label {
display: inline-block;
white-space: pre-wrap;
}
.feed-posts-drawer {
display: grid;
grid-template-rows: 1fr;
@@ -524,6 +534,8 @@ svg {
.feed-posts-drawer-inner {
min-height: 0;
overflow: hidden;
padding-top: 4px;
margin-top: -4px;
}
.feed-posts-drawer.closing,
@@ -908,6 +920,26 @@ svg {
resize: vertical;
}
.new-post-heading {
min-width: 0;
overflow-wrap: anywhere;
}
.new-post-title-icon {
flex: 0 0 auto;
}
.new-post-terminal-cursor {
display: inline-block;
width: 2px;
height: 1.05em;
margin-left: 3px;
vertical-align: -0.12em;
background: currentColor;
border-radius: 1px;
animation: newPostCursorBlink 0.82s steps(1, end) infinite;
}
/* ===== 独立页面(滑入) ===== */
.page-slide {
display: none;
@@ -921,12 +953,12 @@ svg {
overflow-y: auto;
transition: background 0.3s ease;
}
.page-slide.active:not(#pagePost):not(#pageUser):not(#pageSettings) {
.page-slide.active:not(#pagePost):not(#pageUser):not(#pageSettings):not(#pageNew) {
display: block;
animation: slideIn 0.35s cubic-bezier(0.22, 1, 0.36, 1);
}
.page-slide.active:is(#pagePost, #pageUser, #pageSettings) {
.page-slide.active:is(#pagePost, #pageUser, #pageSettings, #pageNew) {
display: block;
}
@@ -1065,6 +1097,89 @@ html.user-transition-settled #pageUser .user-profile-posts {
animation: userPageContentReveal 0.28s ease-out both;
}
/* ===== 发布新帖共享元素过渡 ===== */
html.new-post-view-transition #navbar {
view-transition-name: persistent-navbar;
}
html.new-post-view-transition #pageNew .new-post-title-icon {
view-transition-name: new-post-plus;
}
html.new-post-view-transition #pageNew .new-post-title-label {
view-transition-name: new-post-title;
}
html.new-post-view-transition::view-transition {
background: linear-gradient(145deg, var(--bg-start) 0%, var(--bg-end) 100%);
pointer-events: none;
}
html.new-post-view-transition::view-transition-group(new-post-plus),
html.new-post-view-transition::view-transition-group(new-post-title) {
z-index: 20;
animation-duration: 0.82s;
animation-timing-function: cubic-bezier(0.16, 1, 0.3, 1);
}
html.new-post-view-transition::view-transition-image-pair(new-post-plus),
html.new-post-view-transition::view-transition-image-pair(new-post-title),
html.new-post-view-transition::view-transition-old(new-post-plus),
html.new-post-view-transition::view-transition-new(new-post-plus),
html.new-post-view-transition::view-transition-old(new-post-title),
html.new-post-view-transition::view-transition-new(new-post-title) {
overflow: visible;
mix-blend-mode: normal;
}
html.new-post-view-transition::view-transition-old(root) {
animation: newPostFeedFadeOut 0.36s 0.08s ease-out both;
}
html.new-post-view-transition::view-transition-new(root) {
animation: newPostPageReveal 0.3s 0.44s ease-out both;
}
html.new-post-view-transition.returning-new-post-home::view-transition-old(root) {
animation: newPostPageHide 0.3s 0.04s ease-in both;
}
html.new-post-view-transition.returning-new-post-home::view-transition-new(root) {
animation: newPostFeedReveal 0.34s 0.36s ease-out both;
}
html.new-post-view-transition::view-transition-group(persistent-navbar),
html.new-post-view-transition::view-transition-old(persistent-navbar),
html.new-post-view-transition::view-transition-new(persistent-navbar) {
animation: none;
mix-blend-mode: normal;
}
@keyframes newPostFeedFadeOut {
from { opacity: 1; }
to { opacity: 0; }
}
@keyframes newPostPageReveal {
from { opacity: 0; }
to { opacity: 1; }
}
@keyframes newPostPageHide {
from { opacity: 1; }
to { opacity: 0; }
}
@keyframes newPostFeedReveal {
from { opacity: 0; }
to { opacity: 1; }
}
@keyframes newPostCursorBlink {
0%, 48% { opacity: 1; }
49%, 100% { opacity: 0; }
}
/* ===== 设置页共享元素过渡 ===== */
html.settings-view-transition #navbar {
view-transition-name: persistent-navbar;
@@ -1774,7 +1889,8 @@ html.home-view-transition.returning-home::view-transition-new(post-body) {
@media (prefers-reduced-motion: reduce) {
.page-slide.active,
html.settings-navbar-avatar-reveal #navbar #userDropdown .avatar {
html.settings-navbar-avatar-reveal #navbar #userDropdown .avatar,
.new-post-terminal-cursor {
animation: none !important;
}
}
@@ -2167,6 +2283,46 @@ html.home-view-transition.returning-home::view-transition-new(post-body) {
border-radius: var(--radius);
padding: 20px 24px;
box-shadow: var(--glass-shadow);
overflow: hidden;
transition: height 0.36s cubic-bezier(0.22, 1, 0.36, 1);
}
.admin-content-drawer {
min-height: 0;
min-width: 0;
transform-origin: left center;
transition:
clip-path 0.36s cubic-bezier(0.22, 1, 0.36, 1),
opacity 0.18s ease,
transform 0.36s cubic-bezier(0.22, 1, 0.36, 1);
will-change: clip-path, opacity, transform;
}
.admin-content-drawer-inner {
min-height: 0;
min-width: 0;
overflow: hidden;
}
.admin-content-drawer.closing {
clip-path: inset(0 100% 0 0);
opacity: 0;
transform: translateX(-10px) scaleX(0.985);
transition-duration: 0.22s, 0.16s, 0.22s;
}
.admin-content-drawer.hidden {
clip-path: inset(0 100% 0 0);
opacity: 0;
transform: translateX(-10px) scaleX(0.985);
transition: none;
}
.admin-content-drawer.opening,
.admin-content-drawer.idle {
clip-path: inset(0 0 0 0);
opacity: 1;
transform: translateX(0) scaleX(1);
}
/* ===== 移动端适配:底部导航栏(磨砂玻璃风格) ===== */
@@ -2597,6 +2753,140 @@ html.home-view-transition.returning-home::view-transition-new(post-body) {
background: #181826;
border-color: var(--glass-border);
}
.admin-forum-settings {
width: min(100%, 560px);
overflow-wrap: anywhere;
}
.admin-forum-divider {
height: 1px;
margin: 24px 0;
background: var(--border);
}
.admin-forum-hint {
margin: 0 0 12px;
color: var(--text-secondary);
font-size: 13px;
line-height: 1.6;
}
.admin-favicon-row {
display: grid;
grid-template-columns: 64px minmax(0, 1fr);
gap: 14px;
align-items: stretch;
}
.admin-favicon-preview {
width: 64px;
height: 64px;
display: grid;
place-items: center;
overflow: hidden;
color: var(--text-secondary);
background: var(--bg);
border: 1px solid var(--border);
border-radius: 8px;
}
.admin-favicon-preview img {
width: 100%;
height: 100%;
object-fit: contain;
}
.admin-favicon-upload {
min-width: 0;
min-height: 64px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 8px;
padding: 16px 14px;
color: var(--text-secondary);
font: inherit;
font-size: 13px;
line-height: 1.4;
text-align: center;
overflow-wrap: anywhere;
background: var(--bg);
border: 1.5px dashed var(--border);
border-radius: 8px;
cursor: pointer;
transition: border-color 0.2s ease, background 0.2s ease, color 0.2s ease;
}
.admin-favicon-upload:hover,
.admin-favicon-upload.drag-over {
color: var(--primary);
border-color: var(--primary);
background: var(--primary-bg);
}
.admin-favicon-upload svg {
display: block;
color: var(--text-secondary);
}
.admin-favicon-upload:hover svg,
.admin-favicon-upload.drag-over svg {
color: var(--primary);
}
.admin-favicon-upload:disabled {
cursor: wait;
opacity: 0.65;
}
.admin-version-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
color: var(--text-secondary);
font-size: 13px;
line-height: 1.5;
}
.admin-version-label {
display: inline-flex;
align-items: center;
gap: 6px;
}
.admin-version-label svg {
color: var(--primary);
}
.admin-version-value {
flex-shrink: 0;
padding: 3px 8px;
color: var(--text-secondary);
background: color-mix(in srgb, var(--bg) 72%, transparent);
border: 1px solid var(--border);
border-radius: 999px;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
letter-spacing: 0.02em;
}
@media (max-width: 520px) {
.admin-favicon-row {
grid-template-columns: 56px minmax(0, 1fr);
}
.admin-favicon-preview {
width: 56px;
height: 56px;
}
.admin-favicon-upload {
min-height: 56px;
}
}
[data-theme="dark"] .admin-sidebar {
border: 1px solid var(--glass-border);
}