feat: add delete options for proxy nodes and history records
This commit is contained in:
@@ -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');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user