-
Notifications
You must be signed in to change notification settings - Fork 296
Expand file tree
/
Copy pathfilesController.js
More file actions
206 lines (169 loc) · 5.69 KB
/
filesController.js
File metadata and controls
206 lines (169 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* 文件管理控制器
* 提供文件列表、读取、写入、删除功能
*/
import fs from '../../utils/fsWrapper.js';
import path from 'path';
import mime from 'mime-types';
// 列出目录
export async function listDirectory(req, reply) {
try {
const dirPath = req.query.path || '.';
if (!isSafePath(dirPath)) {
return reply.code(403).send({
error: '访问被拒绝'
});
}
const fullPath = path.join(process.cwd(), dirPath);
const files = await fs.readdir(fullPath, { withFileTypes: true });
const result = files.map(f => {
const isDir = f.isDirectory();
return {
name: f.name,
path: dirPath === '.' ? f.name : `${dirPath}/${f.name}`,
isDirectory: isDir,
size: isDir ? undefined : 0 // fs.stat is expensive to do for all files, so omit size here unless needed
};
});
// 异步获取文件大小
for (let i = 0; i < result.length; i++) {
if (!result[i].isDirectory) {
try {
const stat = await fs.stat(path.join(fullPath, result[i].name));
result[i].size = stat.size;
} catch (e) {
// Ignore stat errors
}
}
}
return reply.send({ files: result });
} catch (e) {
reply.code(500).send({
error: e.message
});
}
}
// 读取文件
export async function readFile(req, reply) {
try {
const { path: filePath } = req.query;
if (!filePath || !isSafePath(filePath)) {
return reply.code(403).send({
error: '无效的文件路径'
});
}
const fullPath = path.join(process.cwd(), filePath);
if (!await fs.pathExists(fullPath)) {
return reply.code(404).send({
error: '文件不存在'
});
}
const ext = path.extname(filePath).toLowerCase();
const imageExts = ['.png', '.jpg', '.jpeg', '.gif', '.svg', '.webp', '.ico', '.bmp', '.tiff', '.tif'];
if (imageExts.includes(ext)) {
// 图片文件 - 返回 base64
const buffer = await fs.readFile(fullPath);
const base64 = buffer.toString('base64');
const mimeType = mime.lookup(fullPath) || 'image/png';
return reply.send({
type: 'image',
mimeType,
dataUrl: `data:${mimeType};base64,${base64}`
});
}
// 文本文件
let content = await fs.readFile(fullPath, 'utf-8');
// 如果是 JS 文件,尝试解码
if (ext === '.js') {
try {
const { decodeDsSource } = await import('../../utils/dsHelper.js');
content = await decodeDsSource(content);
} catch (e) {
// 保持原样
}
}
return reply.send({
type: 'text',
content
});
} catch (e) {
reply.code(500).send({
error: e.message
});
}
}
// 写入文件
export async function writeFile(req, reply) {
try {
const { path: filePath, content } = req.body;
if (!filePath || !isSafePath(filePath)) {
return reply.code(403).send({
error: '无效的文件路径'
});
}
const fullPath = path.join(process.cwd(), filePath);
// 确保目录存在
await fs.ensureDir(path.dirname(fullPath));
// 写入文件
await fs.writeFile(fullPath, content, 'utf-8');
return reply.send({
success: true,
message: '文件保存成功'
});
} catch (e) {
reply.code(500).send({
error: e.message
});
}
}
// 删除文件
export async function deleteFile(req, reply) {
try {
const { path: filePath } = req.query; // in fastify, DELETE params might be in query or we can use body depending on client
const fp = filePath || (req.body && req.body.path);
if (!fp || !isSafePath(fp)) {
return reply.code(403).send({
error: '无效的文件路径'
});
}
const fullPath = path.join(process.cwd(), fp);
if (!await fs.pathExists(fullPath)) {
return reply.code(404).send({
error: '文件不存在'
});
}
await fs.remove(fullPath);
return reply.send({
success: true,
message: '文件删除成功'
});
} catch (e) {
reply.code(500).send({
error: e.message
});
}
}
function isSafePath(filePath) {
if (!filePath || typeof filePath !== 'string') return false;
// Prevent absolute paths from user input directly
if (path.isAbsolute(filePath)) return false;
// Resolve full path and check if it is within CWD
const fullPath = path.resolve(process.cwd(), filePath);
const cwd = process.cwd();
// Ensure the resolved path is inside the current working directory
if (!fullPath.startsWith(cwd)) return false;
// Blacklist check for sensitive files/directories
const blacklist = [
'node_modules',
'database.db',
'.git',
'.env',
'package-lock.json',
'yarn.lock'
];
// Check if any part of the relative path matches the blacklist
// We check against the relative path to avoid matching parts of CWD
const relativePath = path.relative(cwd, fullPath);
if (blacklist.some(item => relativePath.includes(item))) return false;
return true;
}