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
|
//node_modules\.pnpm\egg-multipart@3.3.0\node_modules\egg-multipart\lib\utils.js
// 内置文件名白名单
exports.whitelist = [
// images
'.jpg', '.jpeg', // image/jpeg
'.png', // image/png, image/x-png
'.gif', // image/gif
'.bmp', // image/bmp
'.wbmp', // image/vnd.wap.wbmp
'.webp',
'.tif',
'.psd',
// text
'.svg',
'.js', '.jsx',
'.json',
'.css', '.less',
'.html', '.htm',
'.xml',
// tar
'.zip',
'.gz', '.tgz', '.gzip',
// video
'.mp3',
'.mp4',
'.avi',
];
exports.normalizeOptions = options => {
//...
// normalize whitelist
if (Array.isArray(options.whitelist)) options.whitelist = options.whitelist.map(extname => extname.toLowerCase());
// normalize fileExtensions
if (Array.isArray(options.fileExtensions)) {
options.fileExtensions = options.fileExtensions.map(extname => {
return (extname.startsWith('.') || extname === '') ? extname.toLowerCase() : `.${extname.toLowerCase()}`;
});
}
//...
function checkExt(fileName) {
if (typeof options.whitelist === 'function') return options.whitelist(fileName);
const extname = path.extname(fileName).toLowerCase();
if (Array.isArray(options.whitelist)) return options.whitelist.includes(extname);
// 或条件,存在白名单中或者在extname配置中
return exports.whitelist.includes(extname) || options.fileExtensions.includes(extname);
}
options.checkFile = (fieldName, fileStream, fileName) => {
// just ignore, if no file
if (!fileStream || !fileName) return null;
try {
// 文件名是否允许上传的判断,不可以就抛出异常
if (!checkExt(fileName)) {
const err = new Error('Invalid filename: ' + fileName);
err.status = 400;
return err;
}
} catch (err) {
err.status = 400;
return err;
}
};
}
|