Skip to content

Commit 037ffdc

Browse files
committed
fix: 优化服务器错误提示,读取文件显示权限信息
1 parent 7adf065 commit 037ffdc

File tree

3 files changed

+49
-29
lines changed

3 files changed

+49
-29
lines changed

backend/src/router/read.js

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,44 @@ module.exports = async function read({ query }) {
44
const { path } = query;
55

66
if (!path) {
7-
return { code: 400, msg: "缺少文件路径参数" };
7+
return { code: 400, msg: "缺少文件路径参数", data: query };
88
}
99

10-
if (!fs.existsSync(path)) {
11-
return { code: 404, msg: "文件不存在" };
12-
}
10+
try {
11+
if (!fs.existsSync(path)) {
12+
return { code: 404, msg: "文件不存在", data: query };
13+
}
1314

14-
const stat = fs.statSync(path);
15-
if (!stat.isFile()) {
16-
return { code: 400, msg: "路径不是文件" };
17-
}
15+
const stat = fs.statSync(path);
16+
if (!stat.isFile()) {
17+
return { code: 400, msg: "路径不是文件", data: query };
18+
}
19+
20+
const testFd = fs.openSync(path, "r");
21+
fs.closeSync(testFd);
22+
23+
return {
24+
code: 200,
25+
msg: "操作成功",
26+
data: {
27+
size: stat.size,
28+
filename: path.split("/").pop(),
29+
stream: fs.createReadStream(path),
30+
},
31+
};
32+
} catch (error) {
33+
if (error.code === "EACCES" || error.code === "EPERM") {
34+
return { code: 403, msg: "权限不足,无法读取文件", data: query };
35+
}
1836

19-
return {
20-
code: 200,
21-
msg: "操作成功",
22-
data: {
23-
size: stat.size,
24-
filename: path.split("/").pop(),
25-
stream: fs.createReadStream(path),
26-
},
27-
};
37+
if (error.code === "ENOENT") {
38+
return { code: 404, msg: "文件不存在", data: query };
39+
}
40+
41+
if (error.code === "EISDIR") {
42+
return { code: 400, msg: "路径是目录而不是文件", data: query };
43+
}
44+
45+
return { code: 500, msg: `读取文件失败: ${error.message}`, data: query };
46+
}
2847
};

backend/src/router/save.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,29 @@ module.exports = async function read({ body }) {
55
const { path, encode, value } = body;
66

77
if (!path) {
8-
return { code: 400, msg: "缺少文件路径参数" };
8+
return { code: 400, msg: "缺少文件路径参数", data: body };
99
}
1010

11-
if (!fs.existsSync(path)) {
12-
return { code: 404, msg: "文件不存在" };
13-
}
11+
try {
12+
if (!fs.existsSync(path)) {
13+
return { code: 404, msg: "文件不存在", data: body };
14+
}
1415

15-
const stat = fs.statSync(path);
16-
if (!stat.isFile()) {
17-
return { code: 400, msg: "路径不是文件" };
18-
}
16+
const stat = fs.statSync(path);
17+
if (!stat.isFile()) {
18+
return { code: 400, msg: "路径不是文件", data: body };
19+
}
1920

20-
try {
2121
fs.writeFileSync(path, iconv.encode(value, encode));
22+
2223
return { code: 200, msg: "操作成功", data: null };
2324
} catch (err) {
2425
if (err.code === "EACCES") {
25-
return { code: 401, msg: "权限不足,无法写入文件" };
26+
return { code: 401, msg: "权限不足,无法写入文件", data: body };
2627
} else if (err.code === "ENOENT") {
27-
return { code: 400, msg: "目录不存在,无法写入文件" };
28+
return { code: 400, msg: "目录不存在,无法写入文件", data: body };
2829
} else {
29-
return { code: 400, msg: "文件操作错误" };
30+
return { code: 400, msg: "文件操作错误", data: body };
3031
}
3132
}
3233
};

code.editor.fpk

1.39 KB
Binary file not shown.

0 commit comments

Comments
 (0)