From 5f81b819af408da854e3beb36f5b13821e6b7a04 Mon Sep 17 00:00:00 2001 From: lezi-fun <177434121+lezi-fun@users.noreply.github.com> Date: Fri, 14 Aug 2026 10:32:24 +0800 Subject: [PATCH] fix: address automated review findings --- app/api/admin/favicon/route.js | 26 +++++++++++++++++++++----- components/AdminPage.js | 4 ++++ locales/en.json | 8 +++++++- locales/zh.json | 8 +++++++- test/admin.test.js | 30 ++++++++++++++++++++++++++++++ test/i18n-ui.test.js | 16 ++++++++++++++++ 6 files changed, 85 insertions(+), 7 deletions(-) diff --git a/app/api/admin/favicon/route.js b/app/api/admin/favicon/route.js index 6e0c6ef..b9a77eb 100644 --- a/app/api/admin/favicon/route.js +++ b/app/api/admin/favicon/route.js @@ -31,20 +31,36 @@ export async function POST(req) { 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(); + let client = null; + let committed = false; + let version = ''; try { - await pool.query( + client = await pool.connect(); + await client.query('BEGIN'); + await client.query( + `INSERT INTO settings (key, value) VALUES ('favicon_object', '') + ON CONFLICT (key) DO NOTHING` + ); + const current = await client.query( + "SELECT value FROM settings WHERE key = 'favicon_object' FOR UPDATE" + ); + oldObject = current.rows[0]?.value || ''; + version = Date.now().toString(); + await client.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] ); + await client.query('COMMIT'); + committed = true; } catch (error) { - await deleteObject(objectName).catch(() => {}); + if (client) await client.query('ROLLBACK').catch(() => {}); + if (!committed) await deleteObject(objectName).catch(() => {}); throw error; + } finally { + client?.release(); } if (oldObject && /^forumlify-favicon-[\w-]+\.(?:png|jpg|gif|webp|ico)$/.test(oldObject)) { diff --git a/components/AdminPage.js b/components/AdminPage.js index 4863232..465b779 100644 --- a/components/AdminPage.js +++ b/components/AdminPage.js @@ -93,6 +93,10 @@ export default function AdminPage() { setPanelPhase('idle'); const settledHeight = measurePanelHeight(); if (settledHeight != null) setPanelHeight(settledHeight); + if (pendingTab.current !== tab) { + const queuedTab = pendingTab.current; + transitionFrame.current = requestAnimationFrame(() => changeTab(queuedTab)); + } }, 360); }); }, [panelPhase, tab]); diff --git a/locales/en.json b/locales/en.json index 6f8fc5c..7c13565 100644 --- a/locales/en.json +++ b/locales/en.json @@ -41,7 +41,13 @@ "reportReason": "Report reason", "confirmDelete": "Are you sure you want to delete this post?", "reportTitle": "Report post", - "submitReport": "Submit report" + "submitReport": "Submit report", + "imageUpload": "Upload image", + "imageUploading": "Uploading...", + "imageCompressing": "Compressing image...", + "imageTooLarge": "The source image must be smaller than 20MB", + "imageCompressFailed": "This image could not be compressed. Try another image or reduce its dimensions first.", + "imageUploadFailed": "Upload failed: {{msg}}" }, "newPost": { "title": "Title", diff --git a/locales/zh.json b/locales/zh.json index 44ac646..86a1c57 100644 --- a/locales/zh.json +++ b/locales/zh.json @@ -41,7 +41,13 @@ "reportReason": "举报原因", "confirmDelete": "确定要删除这条帖子吗?", "reportTitle": "举报帖子", - "submitReport": "提交举报" + "submitReport": "提交举报", + "imageUpload": "上传图片", + "imageUploading": "正在上传...", + "imageCompressing": "正在压缩图片...", + "imageTooLarge": "原图大小不能超过 20MB", + "imageCompressFailed": "无法压缩这张图片,请换一张图片或先缩小尺寸", + "imageUploadFailed": "上传失败:{{msg}}" }, "newPost": { "title": "标题", diff --git a/test/admin.test.js b/test/admin.test.js index 483d636..a9d98e3 100644 --- a/test/admin.test.js +++ b/test/admin.test.js @@ -151,6 +151,36 @@ test('管理员上传网站图标', async () => { assert.equal(settings.data.favicon_object, undefined); }); +test('并发上传网站图标不会遗留未引用对象', async () => { + const png = Buffer.from( + 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=', + 'base64' + ); + const upload = async (name) => { + const form = new FormData(); + form.append('file', new Blob([png], { type: 'image/png' }), name); + const res = await fetch(BASE + '/api/admin/favicon', { + method: 'POST', + headers: { Authorization: `Bearer ${adminToken}` }, + body: form, + }); + return { status: res.status, data: await res.json() }; + }; + + const uploads = await Promise.all([upload('favicon-a.png'), upload('favicon-b.png')]); + assert.deepEqual(uploads.map(({ status }) => status), [200, 200]); + + const settings = await api('/settings'); + const currentUrl = settings.data.favicon_url; + const urls = uploads.map(({ data }) => data.favicon_url); + assert.ok(urls.includes(currentUrl)); + + const staleUrl = urls.find((url) => url !== currentUrl); + assert.ok(staleUrl); + assert.equal((await fetch(BASE + currentUrl)).status, 200); + assert.equal((await fetch(BASE + staleUrl)).status, 404); +}); + test('添加友情链接', async () => { const { status, data } = await api('/links', { method: 'POST', diff --git a/test/i18n-ui.test.js b/test/i18n-ui.test.js index dd6e651..4c4ce55 100644 --- a/test/i18n-ui.test.js +++ b/test/i18n-ui.test.js @@ -45,6 +45,22 @@ test('Chinese and English locales expose the same non-empty keys', async () => { } }); +test('statically referenced translation keys exist in both locales', async () => { + const [zh, en] = await Promise.all([readLocale('zh'), readLocale('en')]); + const files = await sourceFiles(path.join(projectRoot, 'components')); + const referenced = new Set(); + + for (const file of files) { + const source = await readFile(file, 'utf8'); + for (const match of source.matchAll(/\bt\(\s*['"]([^'"]+)['"]/g)) { + referenced.add(match[1]); + } + } + + const missing = [...referenced].filter((key) => !zh.has(key) || !en.has(key)).sort(); + assert.deepEqual(missing, []); +}); + test('runtime UI contains no emoji except the approved chat greeting', async () => { const roots = ['app', 'components', 'locales'].map((name) => path.join(projectRoot, name)); const files = (await Promise.all(roots.map(sourceFiles))).flat();