Skip to content

Commit f76aa82

Browse files
committed
feat: 优化文件加载和图标展示
1 parent 25a92ee commit f76aa82

15 files changed

Lines changed: 385 additions & 311 deletions

File tree

backend/.prettierrc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"$schema": "https://json.schemastore.org/prettierrc",
3+
"semi": false,
4+
"singleQuote": true,
5+
"printWidth": 140
6+
}

backend/src/cgi.js

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,89 @@
1-
const querystring = require("querystring");
1+
const querystring = require('querystring')
22

3-
const exec = require("./utils/exec");
3+
const exec = require('./utils/exec')
44

55
// 获取 env、query、body
66
async function getData() {
7-
const env = process.env;
7+
const env = process.env
88

9-
const assets = env.PATH_INFO.replace(
10-
"/cgi/ThirdParty/code.editor/index.cgi",
11-
""
12-
);
9+
const assets = env.PATH_INFO.replace('/cgi/ThirdParty/code.editor/index.cgi', '')
1310

1411
if (assets) {
15-
const path = assets === "/" ? "/index.html" : assets;
16-
const cache = assets !== "/";
12+
const path = assets === '/' ? '/index.html' : assets
13+
const cache = assets !== '/'
1714

1815
return {
1916
env,
20-
api: "read",
17+
api: 'read',
2118
query: { path: `/var/apps/code.editor/target/server/dist${path}`, cache },
2219
body: {},
23-
};
20+
}
2421
}
2522

2623
const result = {
2724
env,
28-
api: env.HTTP_API_PATH || "",
29-
query: querystring.parse(env.QUERY_STRING || ""),
25+
api: env.HTTP_API_PATH || '',
26+
query: querystring.parse(env.QUERY_STRING || ''),
3027
body: {},
31-
};
28+
}
3229

33-
if (env.REQUEST_METHOD === "POST") {
34-
const contentLength = parseInt(env.CONTENT_LENGTH || "0");
30+
if (env.REQUEST_METHOD === 'POST') {
31+
const contentLength = parseInt(env.CONTENT_LENGTH || '0')
3532

3633
if (contentLength > 0) {
3734
const str = await new Promise((r) => {
38-
let str = "";
35+
let str = ''
3936

40-
process.stdin.on("data", (chunk) => {
41-
str += chunk.toString();
42-
});
37+
process.stdin.on('data', (chunk) => {
38+
str += chunk.toString()
39+
})
4340

44-
process.stdin.on("end", () => {
45-
r(str);
46-
});
47-
});
41+
process.stdin.on('end', () => {
42+
r(str)
43+
})
44+
})
4845

4946
try {
5047
if (str.trim()) {
51-
const type = env.CONTENT_TYPE || "";
48+
const type = env.CONTENT_TYPE || ''
5249

53-
if (type.includes("application/x-www-form-urlencoded")) {
54-
result.body = querystring.parse(str);
55-
} else if (type.includes("application/json")) {
56-
result.body = JSON.parse(str);
50+
if (type.includes('application/x-www-form-urlencoded')) {
51+
result.body = querystring.parse(str)
52+
} else if (type.includes('application/json')) {
53+
result.body = JSON.parse(str)
5754
} else {
58-
result.body = { raw: str };
55+
result.body = { raw: str }
5956
}
6057
}
6158
} catch (error) {
62-
result.body = {};
59+
result.body = {}
6360
}
6461
}
6562
}
6663

67-
return result;
64+
return result
6865
}
6966

7067
async function main() {
7168
try {
72-
const data = await getData();
69+
const data = await getData()
7370

74-
const { type, body } = await exec(data);
71+
const { type, body } = await exec(data)
7572

7673
if (type) {
77-
console.log(`Content-Type: ${type}`);
78-
console.log(`Content-Length: ${body.size}`);
79-
console.log(`Last-Modified: ${body.time}`);
80-
console.log("");
81-
body.stream.pipe(process.stdout);
74+
console.log(`Content-Type: ${type}`)
75+
console.log(`Content-Length: ${body.size}`)
76+
console.log(`Last-Modified: ${body.time}`)
77+
console.log('')
78+
body.stream.pipe(process.stdout)
8279
} else {
83-
console.log("Content-Type: application/json; charset=utf-8\n");
84-
console.log(JSON.stringify(body));
80+
console.log('Content-Type: application/json; charset=utf-8\n')
81+
console.log(JSON.stringify(body))
8582
}
8683
} catch (error) {
87-
console.log("Content-Type: application/json; charset=utf-8\n");
88-
console.log(JSON.stringify({ code: 400, msg: "调用错误" }));
84+
console.log('Content-Type: application/json; charset=utf-8\n')
85+
console.log(JSON.stringify({ code: 400, msg: '调用错误' }))
8986
}
9087
}
9188

92-
main();
89+
main()

backend/src/koa.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
const Koa = require("koa");
2-
const bodyParser = require("koa-bodyparser");
3-
const cors = require("@koa/cors");
1+
const Koa = require('koa')
2+
const bodyParser = require('koa-bodyparser')
3+
const cors = require('@koa/cors')
44

5-
const exec = require("./utils/exec");
5+
const exec = require('./utils/exec')
66

7-
const app = new Koa();
7+
const app = new Koa()
88

9-
app.use(cors());
9+
app.use(cors())
1010

11-
app.use(bodyParser());
11+
app.use(bodyParser())
1212

1313
app.use(async (ctx) => {
1414
const data = {
15-
api: ctx.request.headers["api-path"] || "",
15+
api: ctx.request.headers['api-path'] || '',
1616
query: ctx.query || {},
1717
body: ctx.request.body || {},
18-
};
18+
}
1919

20-
const { type, body } = await exec(data);
20+
const { type, body } = await exec(data)
2121

2222
if (type) {
23-
ctx.set("Content-Type", type);
24-
ctx.set("Content-Length", body.size);
25-
ctx.set("Last-Modified", body.time);
26-
ctx.body = body.stream;
23+
ctx.set('Content-Type', type)
24+
ctx.set('Content-Length', body.size)
25+
ctx.set('Last-Modified', body.time)
26+
ctx.body = body.stream
2727
} else {
28-
ctx.set("Content-Type", "application/json; charset=utf-8");
29-
ctx.body = body;
28+
ctx.set('Content-Type', 'application/json; charset=utf-8')
29+
ctx.body = body
3030
}
31-
});
31+
})
3232

33-
app.listen(17746);
33+
app.listen(17746)

backend/src/router/def.js

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,48 @@
1-
const fs = require("fs");
2-
const path = require("path");
1+
const fs = require('fs')
2+
const path = require('path')
33

4-
const wwwRoot = "/usr/trim/www";
5-
const wwwReg = /(?<=<script\s[^>]*?src=")\/assets\/index[^"]+(?=")/gi;
4+
const wwwRoot = '/usr/trim/www'
5+
const wwwReg = /(?<=<script\s[^>]*?src=")\/assets\/index[^"]+(?=")/gi
66

7-
const constReg =
8-
/fn\:\(\)\=\>\{const (.+)\=\{\}\;return .+\.state\.apps\.forEach/;
7+
const constReg = /fn\:\(\)\=\>\{const (.+)\=\{\}\;return .+\.state\.apps\.forEach/
98

109
module.exports = async function read({ body }) {
1110
try {
12-
const html = fs.readFileSync(path.join(wwwRoot, "./index.html"), {
13-
encoding: "utf-8",
14-
});
11+
const html = fs.readFileSync(path.join(wwwRoot, './index.html'), { encoding: 'utf-8' })
1512

16-
const jsMatch = html.match(wwwReg);
13+
const jsMatch = html.match(wwwReg)
1714

1815
if (!jsMatch || !jsMatch[0]) {
19-
return { code: 500, msg: `操作失败: 入口匹配失败`, data: body };
16+
return { code: 500, msg: `操作失败: 入口匹配失败`, data: body }
2017
}
2118

22-
const jsPath = path.join(wwwRoot, `.${jsMatch[0]}`);
19+
const jsPath = path.join(wwwRoot, `.${jsMatch[0]}`)
2320

24-
let jsText = fs.readFileSync(path.join(wwwRoot, `.${jsMatch[0]}`), {
25-
encoding: "utf-8",
26-
});
21+
let jsText = fs.readFileSync(path.join(wwwRoot, `.${jsMatch[0]}`), { encoding: 'utf-8' })
2722

28-
const jsConst = jsText.match(constReg);
23+
const jsConst = jsText.match(constReg)
2924

3025
if (!jsConst || !jsConst[1]) {
31-
return { code: 500, msg: `操作失败: 脚本匹配失败`, data: body };
26+
return { code: 500, msg: `操作失败: 脚本匹配失败`, data: body }
3227
}
3328

3429
if (body.open) {
3530
jsText = jsText.replace(
3631
/\,\{fileApps\:(.+)\}\}\,deps\:/,
3732
`,{fileApps:new Proxy(${jsConst[1]},{get:(o,k)=>o[k]!==undefined?o[k]:o["*"]})}},deps:`
38-
);
33+
)
3934
} else {
40-
jsText = jsText.replace(
41-
/\,\{fileApps\:(.+)\}\}\,deps\:/,
42-
`,{fileApps:${jsConst[1]}}},deps:`
43-
);
35+
jsText = jsText.replace(/\,\{fileApps\:(.+)\}\}\,deps\:/, `,{fileApps:${jsConst[1]}}},deps:`)
4436
}
4537

46-
fs.writeFileSync(jsPath, jsText);
38+
fs.writeFileSync(jsPath, jsText)
4739

48-
return {
49-
code: 200,
50-
msg: "操作成功",
51-
data: {
52-
js: jsConst[1],
53-
},
54-
};
40+
return { code: 200, msg: '操作成功', data: {} }
5541
} catch (error) {
56-
if (error.code === "EACCES" || error.code === "EPERM") {
57-
return { code: 403, msg: "权限不足,无法操作", data: query };
42+
if (error.code === 'EACCES' || error.code === 'EPERM') {
43+
return { code: 403, msg: '权限不足,无法操作', data: query }
5844
}
5945

60-
return { code: 500, msg: `操作失败: ${error.message}`, data: query };
46+
return { code: 500, msg: `操作失败: ${error.message}`, data: query }
6147
}
62-
};
48+
}

backend/src/router/dir.js

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,47 @@
1-
const fs = require("fs");
2-
const path = require("path");
1+
const fs = require('fs')
2+
const path = require('path')
33

44
module.exports = async function read({ query }) {
55
if (!query.path) {
6-
return { code: 400, msg: "缺少文件路径参数", data: query };
6+
return { code: 400, msg: '缺少文件路径参数', data: query }
77
}
88

9-
const dirPath = query.path[0] === "/" ? query.path : `/${query.path}`;
9+
const dirPath = query.path[0] === '/' ? query.path : `/${query.path}`
1010

1111
try {
1212
if (!fs.existsSync(dirPath)) {
13-
return { code: 404, msg: "目录不存在", data: query };
13+
return { code: 404, msg: '目录不存在', data: query }
1414
}
1515

16-
const stat = fs.statSync(dirPath);
16+
const stat = fs.statSync(dirPath)
1717
if (!stat.isDirectory()) {
18-
return { code: 400, msg: "路径不是目录", data: query };
18+
return { code: 400, msg: '路径不是目录', data: query }
1919
}
2020

21-
const items = fs.readdirSync(dirPath);
21+
const items = fs.readdirSync(dirPath)
2222

23-
const result = {
24-
files: [],
25-
dirs: [],
26-
};
23+
const result = { files: [], dirs: [] }
2724

2825
items.forEach((item) => {
29-
const fullPath = path.join(dirPath, item);
30-
const itemStats = fs.statSync(fullPath);
26+
const fullPath = path.join(dirPath, item)
27+
const itemStats = fs.statSync(fullPath)
3128

3229
if (itemStats.isDirectory()) {
33-
result.dirs.push({
34-
name: item,
35-
});
30+
result.dirs.push({ name: item })
3631
} else {
37-
result.files.push({
38-
name: item,
39-
size: itemStats.size,
40-
updateDate: itemStats.mtime,
41-
});
32+
result.files.push({ name: item, size: itemStats.size, updateDate: itemStats.mtime })
4233
}
43-
});
34+
})
4435

45-
result.files.sort((i, j) =>
46-
(i.name || "").toLocaleLowerCase() > (j.name || "").toLocaleLowerCase()
47-
? 1
48-
: -1
49-
);
50-
result.dirs.sort((i, j) =>
51-
(i.name || "").toLocaleLowerCase() > (j.name || "").toLocaleLowerCase()
52-
? 1
53-
: -1
54-
);
36+
result.files.sort((i, j) => ((i.name || '').toLocaleLowerCase() > (j.name || '').toLocaleLowerCase() ? 1 : -1))
37+
result.dirs.sort((i, j) => ((i.name || '').toLocaleLowerCase() > (j.name || '').toLocaleLowerCase() ? 1 : -1))
5538

56-
return {
57-
code: 200,
58-
msg: "操作成功",
59-
data: result,
60-
};
39+
return { code: 200, msg: '操作成功', data: result }
6140
} catch (error) {
62-
if (error.code === "EACCES" || error.code === "EPERM") {
63-
return { code: 403, msg: "权限不足,无法读取目录", data: query };
41+
if (error.code === 'EACCES' || error.code === 'EPERM') {
42+
return { code: 403, msg: '权限不足,无法读取目录', data: query }
6443
}
6544

66-
return { code: 500, msg: `读取目录失败: ${error.message}`, data: query };
45+
return { code: 500, msg: `读取目录失败: ${error.message}`, data: query }
6746
}
68-
};
47+
}

0 commit comments

Comments
 (0)