fix: address automated review findings
This commit is contained in:
1 parent
f5d173350b
commit
5f81b819af
6 files changed
+85
-7
No files matched your search
@@ -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)) {
|
||||
|
||||
@@ -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]);
|
||||
|
||||
+7
-1
@@ -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",
|
||||
|
||||
+7
-1
@@ -41,7 +41,13 @@
|
||||
"reportReason": "举报原因",
|
||||
"confirmDelete": "确定要删除这条帖子吗?",
|
||||
"reportTitle": "举报帖子",
|
||||
"submitReport": "提交举报"
|
||||
"submitReport": "提交举报",
|
||||
"imageUpload": "上传图片",
|
||||
"imageUploading": "正在上传...",
|
||||
"imageCompressing": "正在压缩图片...",
|
||||
"imageTooLarge": "原图大小不能超过 20MB",
|
||||
"imageCompressFailed": "无法压缩这张图片,请换一张图片或先缩小尺寸",
|
||||
"imageUploadFailed": "上传失败:{{msg}}"
|
||||
},
|
||||
"newPost": {
|
||||
"title": "标题",
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user