refactor: clean up unused variables and empty blocks in file-manager.ts

- Remove 22 unused variables (linkCount, hostId, userId, content, escapedTempFile, index, code)
- Fix 1 empty catch block
- Simplify multiple route handlers by removing unused destructured parameters

Reduced errors from 878 to 855 (23 fixes)
This commit is contained in:
ZacharyZcR
2025-10-05 20:23:15 +08:00
parent ffaccaadf2
commit 93a74277dc

View File

@@ -101,7 +101,9 @@ function cleanupSession(sessionId: string) {
if (session) { if (session) {
try { try {
session.client.end(); session.client.end();
} catch {} } catch {
// Ignore connection close errors
}
clearTimeout(session.timeout); clearTimeout(session.timeout);
delete sshSessions[sessionId]; delete sshSessions[sessionId];
} }
@@ -455,7 +457,6 @@ app.get("/ssh/file_manager/ssh/listFiles", (req, res) => {
const parts = line.split(/\s+/); const parts = line.split(/\s+/);
if (parts.length >= 9) { if (parts.length >= 9) {
const permissions = parts[0]; const permissions = parts[0];
const linkCount = parts[1];
const owner = parts[2]; const owner = parts[2];
const group = parts[3]; const group = parts[3];
const size = parseInt(parts[4], 10); const size = parseInt(parts[4], 10);
@@ -694,7 +695,7 @@ app.get("/ssh/file_manager/ssh/readFile", (req, res) => {
}); });
app.post("/ssh/file_manager/ssh/writeFile", async (req, res) => { app.post("/ssh/file_manager/ssh/writeFile", async (req, res) => {
const { sessionId, path: filePath, content, hostId, userId } = req.body; const { sessionId, path: filePath, content } = req.body;
const sshConn = sshSessions[sessionId]; const sshConn = sshSessions[sessionId];
if (!sessionId) { if (!sessionId) {
@@ -881,14 +882,7 @@ app.post("/ssh/file_manager/ssh/writeFile", async (req, res) => {
}); });
app.post("/ssh/file_manager/ssh/uploadFile", async (req, res) => { app.post("/ssh/file_manager/ssh/uploadFile", async (req, res) => {
const { const { sessionId, path: filePath, content, fileName } = req.body;
sessionId,
path: filePath,
content,
fileName,
hostId,
userId,
} = req.body;
const sshConn = sshSessions[sessionId]; const sshConn = sshSessions[sessionId];
if (!sessionId) { if (!sessionId) {
@@ -1022,8 +1016,6 @@ app.post("/ssh/file_manager/ssh/uploadFile", async (req, res) => {
} }
if (chunks.length === 1) { if (chunks.length === 1) {
const tempFile = `/tmp/upload_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
const escapedTempFile = tempFile.replace(/'/g, "'\"'\"'");
const escapedPath = fullPath.replace(/'/g, "'\"'\"'"); const escapedPath = fullPath.replace(/'/g, "'\"'\"'");
const writeCommand = `echo '${chunks[0]}' | base64 -d > '${escapedPath}' && echo "SUCCESS"`; const writeCommand = `echo '${chunks[0]}' | base64 -d > '${escapedPath}' && echo "SUCCESS"`;
@@ -1088,13 +1080,11 @@ app.post("/ssh/file_manager/ssh/uploadFile", async (req, res) => {
}); });
}); });
} else { } else {
const tempFile = `/tmp/upload_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
const escapedTempFile = tempFile.replace(/'/g, "'\"'\"'");
const escapedPath = fullPath.replace(/'/g, "'\"'\"'"); const escapedPath = fullPath.replace(/'/g, "'\"'\"'");
let writeCommand = `> '${escapedPath}'`; let writeCommand = `> '${escapedPath}'`;
chunks.forEach((chunk, index) => { chunks.forEach((chunk) => {
writeCommand += ` && echo '${chunk}' | base64 -d >> '${escapedPath}'`; writeCommand += ` && echo '${chunk}' | base64 -d >> '${escapedPath}'`;
}); });
@@ -1177,14 +1167,7 @@ app.post("/ssh/file_manager/ssh/uploadFile", async (req, res) => {
}); });
app.post("/ssh/file_manager/ssh/createFile", async (req, res) => { app.post("/ssh/file_manager/ssh/createFile", async (req, res) => {
const { const { sessionId, path: filePath, fileName } = req.body;
sessionId,
path: filePath,
fileName,
content = "",
hostId,
userId,
} = req.body;
const sshConn = sshSessions[sessionId]; const sshConn = sshSessions[sessionId];
if (!sessionId) { if (!sessionId) {
@@ -1285,7 +1268,7 @@ app.post("/ssh/file_manager/ssh/createFile", async (req, res) => {
}); });
app.post("/ssh/file_manager/ssh/createFolder", async (req, res) => { app.post("/ssh/file_manager/ssh/createFolder", async (req, res) => {
const { sessionId, path: folderPath, folderName, hostId, userId } = req.body; const { sessionId, path: folderPath, folderName } = req.body;
const sshConn = sshSessions[sessionId]; const sshConn = sshSessions[sessionId];
if (!sessionId) { if (!sessionId) {
@@ -1386,7 +1369,7 @@ app.post("/ssh/file_manager/ssh/createFolder", async (req, res) => {
}); });
app.delete("/ssh/file_manager/ssh/deleteItem", async (req, res) => { app.delete("/ssh/file_manager/ssh/deleteItem", async (req, res) => {
const { sessionId, path: itemPath, isDirectory, hostId, userId } = req.body; const { sessionId, path: itemPath, isDirectory } = req.body;
const sshConn = sshSessions[sessionId]; const sshConn = sshSessions[sessionId];
if (!sessionId) { if (!sessionId) {
@@ -1488,7 +1471,7 @@ app.delete("/ssh/file_manager/ssh/deleteItem", async (req, res) => {
}); });
app.put("/ssh/file_manager/ssh/renameItem", async (req, res) => { app.put("/ssh/file_manager/ssh/renameItem", async (req, res) => {
const { sessionId, oldPath, newName, hostId, userId } = req.body; const { sessionId, oldPath, newName } = req.body;
const sshConn = sshSessions[sessionId]; const sshConn = sshSessions[sessionId];
if (!sessionId) { if (!sessionId) {
@@ -1596,7 +1579,7 @@ app.put("/ssh/file_manager/ssh/renameItem", async (req, res) => {
}); });
app.put("/ssh/file_manager/ssh/moveItem", async (req, res) => { app.put("/ssh/file_manager/ssh/moveItem", async (req, res) => {
const { sessionId, oldPath, newPath, hostId, userId } = req.body; const { sessionId, oldPath, newPath } = req.body;
const sshConn = sshSessions[sessionId]; const sshConn = sshSessions[sessionId];
if (!sessionId) { if (!sessionId) {
@@ -1985,7 +1968,7 @@ app.post("/ssh/file_manager/ssh/copyItem", async (req, res) => {
}); });
app.post("/ssh/file_manager/ssh/executeFile", async (req, res) => { app.post("/ssh/file_manager/ssh/executeFile", async (req, res) => {
const { sessionId, filePath, hostId, userId } = req.body; const { sessionId, filePath } = req.body;
const sshConn = sshSessions[sessionId]; const sshConn = sshSessions[sessionId];
if (!sshConn || !sshConn.isConnected) { if (!sshConn || !sshConn.isConnected) {
@@ -2022,7 +2005,7 @@ app.post("/ssh/file_manager/ssh/executeFile", async (req, res) => {
checkResult += data.toString(); checkResult += data.toString();
}); });
checkStream.on("close", (code) => { checkStream.on("close", () => {
if (!checkResult.includes("EXECUTABLE")) { if (!checkResult.includes("EXECUTABLE")) {
return res.status(400).json({ error: "File is not executable" }); return res.status(400).json({ error: "File is not executable" });
} }