feat: add delete options for proxy nodes and history records

This commit is contained in:
jocayn
2026-06-13 15:53:09 +08:00
parent 2c0773f4f7
commit 8f28ae3c67
2 changed files with 104 additions and 5 deletions
+54
View File
@@ -733,6 +733,60 @@ function startServer() {
return;
}
// API: POST /api/history/delete
if (pathname === '/api/history/delete' && req.method === 'POST') {
try {
const { yamlFile, jsonFile } = await readJsonBody(req);
if (!yamlFile || !jsonFile) {
res.writeHead(400);
res.end('Missing yamlFile or jsonFile parameter');
return;
}
// Security check: prevent directory traversal
const safeRegex = /^CatMata-sub-\d{8}-\d{6}\.(yaml|json)$/;
if (!safeRegex.test(yamlFile) || !safeRegex.test(jsonFile)) {
res.writeHead(400);
res.end('Invalid file name');
return;
}
const historyDir = path.join(outDir, 'history');
const yamlPath = path.join(historyDir, yamlFile);
const jsonPath = path.join(historyDir, jsonFile);
// Delete physical files
if (fs.existsSync(yamlPath)) {
fs.unlinkSync(yamlPath);
}
if (fs.existsSync(jsonPath)) {
fs.unlinkSync(jsonPath);
}
// Update history.json
const historyJsonPath = path.join(historyDir, 'history.json');
let historyList = [];
if (fs.existsSync(historyJsonPath)) {
try {
historyList = JSON.parse(fs.readFileSync(historyJsonPath, 'utf-8'));
} catch (e) {
console.error("Failed to parse history.json, resetting:", e.message);
}
}
historyList = historyList.filter(item => item.yamlFile !== yamlFile || item.jsonFile !== jsonFile);
fs.writeFileSync(historyJsonPath, JSON.stringify(historyList, null, 2), 'utf-8');
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ success: true }));
} catch (e) {
res.writeHead(500);
res.end(e.message);
}
return;
}
res.writeHead(404);
res.end('Not Found');
});