file-handle 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +91 -0
- package/README.zh-CN.md +93 -0
- package/package.json +36 -0
- package/server/bin/cli.js +3 -0
- package/server/common/adapter.js +90 -0
- package/server/common/i18n.js +109 -0
- package/server/config.js +123 -0
- package/server/fh.app.dat +0 -0
- package/server/index.js +153 -0
- package/server/middlewares/ipValidation.js +33 -0
- package/server/middlewares/security.js +172 -0
- package/server/middlewares/upload.js +57 -0
- package/server/routes/api.js +174 -0
- package/server/routes/fh.js +82 -0
- package/server/routes/res.js +18 -0
- package/server/types/d.js +1 -0
- package/server/types/index.js +83 -0
- package/server/types/response.js +20 -0
- package/server/utils/Preview/Markdown/registrie.js +8 -0
- package/server/utils/Preview/Text/registrie.js +92 -0
- package/server/utils/Preview/types.js +1 -0
- package/server/utils/logger.js +55 -0
- package/server/utils/path.js +62 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { previewerConfig as markdownConfig } from '../utils/Preview/Markdown/registrie.js';
|
|
2
|
+
import { previewerConfig as textConfig } from '../utils/Preview/Text/registrie.js';
|
|
3
|
+
import mime from 'mime-types';
|
|
4
|
+
// 文件操作错误
|
|
5
|
+
export class FileOperationError extends Error {
|
|
6
|
+
constructor(message, code = 500) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.code = code;
|
|
9
|
+
this.name = 'FileOperationError';
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
// 文件类型判断工具函数
|
|
13
|
+
export function getMime(fileName) {
|
|
14
|
+
let ct = mime.lookup(fileName);
|
|
15
|
+
if (!ct) {
|
|
16
|
+
ct = textConfig.viewExtensions.some((ext) => ext === extname(fileName).slice(1)) ? 'text/plain' : false;
|
|
17
|
+
}
|
|
18
|
+
return ct;
|
|
19
|
+
}
|
|
20
|
+
export function isKnownExtensions(fileName) {
|
|
21
|
+
return !!getMime(fileName);
|
|
22
|
+
}
|
|
23
|
+
export function isPreviewAble(fileName) {
|
|
24
|
+
let extName = extname(fileName);
|
|
25
|
+
if (!extName) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
extName = extName.slice(1);
|
|
29
|
+
const aExtAble = viewAbleExtensions.filter(([s]) => s == extName.toLowerCase()).map((a) => a[1]);
|
|
30
|
+
return aExtAble.length ? aExtAble : null;
|
|
31
|
+
}
|
|
32
|
+
export function isEditorAble(fileName) {
|
|
33
|
+
let extName = extname(fileName);
|
|
34
|
+
if (!extName) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
extName = extName.slice(1);
|
|
38
|
+
const aExtAble = editAbleExtensions.filter(([s]) => s == extName.toLowerCase()).map(([name]) => name);
|
|
39
|
+
return aExtAble.length ? aExtAble : null;
|
|
40
|
+
}
|
|
41
|
+
// 有浏览器预览的文件类型
|
|
42
|
+
export const viewAbleExtensions = [];
|
|
43
|
+
[markdownConfig, textConfig].forEach((config) => {
|
|
44
|
+
config.viewExtensions && config.viewExtensions.forEach((ext) => {
|
|
45
|
+
viewAbleExtensions.push([ext, config.name]);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
// 有编辑器对应的文件类型
|
|
49
|
+
export const editAbleExtensions = [];
|
|
50
|
+
[markdownConfig, textConfig].forEach((config) => {
|
|
51
|
+
config.editExtensions && config.editExtensions.forEach((ext) => {
|
|
52
|
+
editAbleExtensions.push([ext, config.name]);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
function extname(path) {
|
|
56
|
+
// 去除路径末尾的斜杠
|
|
57
|
+
path = path.replace(/[\\\/]+$/, '');
|
|
58
|
+
const parts = path.split('/');
|
|
59
|
+
const filename = parts[parts.length - 1];
|
|
60
|
+
const dotIndex = filename.lastIndexOf('.');
|
|
61
|
+
// 如果没有找到点号,或者文件名是'.'或'..',返回空字符串
|
|
62
|
+
if (dotIndex === -1 || filename === '.' || filename === '..') {
|
|
63
|
+
return '';
|
|
64
|
+
}
|
|
65
|
+
// 如果点号在开头且后面没有其他点号,返回空字符串
|
|
66
|
+
if (dotIndex === 0 && filename.indexOf('.', 1) === -1) {
|
|
67
|
+
return '';
|
|
68
|
+
}
|
|
69
|
+
// 如果是以'..'开头的情况
|
|
70
|
+
if (filename.startsWith('..')) {
|
|
71
|
+
// 如果只有'..',返回空字符串
|
|
72
|
+
if (filename === '..') {
|
|
73
|
+
return '';
|
|
74
|
+
}
|
|
75
|
+
// 对于所有以'..'开头的情况,都返回最后一个点号后面的内容
|
|
76
|
+
const lastDotIndex = filename.lastIndexOf('.');
|
|
77
|
+
if (lastDotIndex !== -1) {
|
|
78
|
+
return filename.slice(lastDotIndex);
|
|
79
|
+
}
|
|
80
|
+
return '';
|
|
81
|
+
}
|
|
82
|
+
return dotIndex === -1 ? '' : filename.slice(dotIndex);
|
|
83
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// 统一响应构造函数
|
|
2
|
+
export class ApiResponseBuilder {
|
|
3
|
+
static success(data, msg = 'ok') {
|
|
4
|
+
return {
|
|
5
|
+
code: 1,
|
|
6
|
+
msg,
|
|
7
|
+
data
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
static error(msg, code = -1) {
|
|
11
|
+
return {
|
|
12
|
+
code,
|
|
13
|
+
msg,
|
|
14
|
+
data: null
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
static notFound(msg = '资源不存在') {
|
|
18
|
+
return this.error(msg, -404);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
export const previewerConfig = {
|
|
2
|
+
name: 'text',
|
|
3
|
+
page: '/res/src/utils/Preview/Text/index.html',
|
|
4
|
+
viewExtensions: [
|
|
5
|
+
'json',
|
|
6
|
+
'text',
|
|
7
|
+
'yaml',
|
|
8
|
+
'md',
|
|
9
|
+
'sh',
|
|
10
|
+
'bat',
|
|
11
|
+
'sql',
|
|
12
|
+
'rb',
|
|
13
|
+
'r',
|
|
14
|
+
'txt',
|
|
15
|
+
'xml',
|
|
16
|
+
'html',
|
|
17
|
+
'css',
|
|
18
|
+
'class',
|
|
19
|
+
'js',
|
|
20
|
+
'py',
|
|
21
|
+
'java',
|
|
22
|
+
'cs',
|
|
23
|
+
'php',
|
|
24
|
+
'ini',
|
|
25
|
+
'log',
|
|
26
|
+
'conf',
|
|
27
|
+
'ps1',
|
|
28
|
+
'vbs',
|
|
29
|
+
'groovy',
|
|
30
|
+
'tcl',
|
|
31
|
+
'makefile',
|
|
32
|
+
'csv',
|
|
33
|
+
'h',
|
|
34
|
+
'cpp',
|
|
35
|
+
'c',
|
|
36
|
+
'lua',
|
|
37
|
+
'pl',
|
|
38
|
+
'gitignore',
|
|
39
|
+
'htaccess',
|
|
40
|
+
'env',
|
|
41
|
+
'tex',
|
|
42
|
+
'vue',
|
|
43
|
+
'dart',
|
|
44
|
+
'swift',
|
|
45
|
+
'go',
|
|
46
|
+
'cmd',
|
|
47
|
+
'forEach',
|
|
48
|
+
'jsonl',
|
|
49
|
+
'mdx',
|
|
50
|
+
'tsv',
|
|
51
|
+
'properties',
|
|
52
|
+
'toml',
|
|
53
|
+
'jsx',
|
|
54
|
+
'coffee',
|
|
55
|
+
'hpp',
|
|
56
|
+
'gradle',
|
|
57
|
+
'cfg',
|
|
58
|
+
'yml',
|
|
59
|
+
'dockerfile',
|
|
60
|
+
'sln',
|
|
61
|
+
'csproj',
|
|
62
|
+
'vbproj',
|
|
63
|
+
'ts',
|
|
64
|
+
'tsx',
|
|
65
|
+
'scss',
|
|
66
|
+
'less',
|
|
67
|
+
'styl',
|
|
68
|
+
'jade',
|
|
69
|
+
'tpl',
|
|
70
|
+
'haml',
|
|
71
|
+
'erb',
|
|
72
|
+
'kt',
|
|
73
|
+
'rs',
|
|
74
|
+
'scala',
|
|
75
|
+
'clj',
|
|
76
|
+
'erl',
|
|
77
|
+
'hs',
|
|
78
|
+
'ml',
|
|
79
|
+
'nim',
|
|
80
|
+
'asm',
|
|
81
|
+
'mk',
|
|
82
|
+
'cmake',
|
|
83
|
+
'xsd',
|
|
84
|
+
'xmlschema',
|
|
85
|
+
'wsdl',
|
|
86
|
+
'rdf',
|
|
87
|
+
'owl',
|
|
88
|
+
'ttl',
|
|
89
|
+
'n3',
|
|
90
|
+
'sparql'
|
|
91
|
+
]
|
|
92
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import winston from 'winston';
|
|
2
|
+
import { loadConfig } from '../config.js';
|
|
3
|
+
const config = loadConfig();
|
|
4
|
+
// 创建日志格式
|
|
5
|
+
const logFormat = winston.format.combine(winston.format.timestamp(), winston.format.printf(({ level, message, timestamp }) => {
|
|
6
|
+
return `${timestamp} ${level.toUpperCase()}: ${message}`;
|
|
7
|
+
}));
|
|
8
|
+
// 创建日志记录器
|
|
9
|
+
const opt = {
|
|
10
|
+
level: process.env.NODE_ENV === 'production' ? 'info' : 'debug',
|
|
11
|
+
format: logFormat,
|
|
12
|
+
defaultMeta: { service: 'File-Handle' },
|
|
13
|
+
transports: [
|
|
14
|
+
// 控制台输出
|
|
15
|
+
new winston.transports.Console({
|
|
16
|
+
level: process.env.DEBUG ? 'debug' : 'info',
|
|
17
|
+
format: winston.format.combine(winston.format.colorize(), logFormat)
|
|
18
|
+
})
|
|
19
|
+
]
|
|
20
|
+
};
|
|
21
|
+
if (config.writeLog) {
|
|
22
|
+
opt.transports.push(
|
|
23
|
+
// 文件输出
|
|
24
|
+
// @ts-ignore
|
|
25
|
+
new winston.transports.File({
|
|
26
|
+
filename: 'error.log',
|
|
27
|
+
level: 'error',
|
|
28
|
+
dirname: 'logs'
|
|
29
|
+
}), new winston.transports.File({
|
|
30
|
+
filename: 'info.log',
|
|
31
|
+
level: 'info',
|
|
32
|
+
dirname: 'logs'
|
|
33
|
+
}));
|
|
34
|
+
}
|
|
35
|
+
const logger = winston.createLogger(opt);
|
|
36
|
+
// 导出日志记录器
|
|
37
|
+
export default logger;
|
|
38
|
+
// 导出便捷方法
|
|
39
|
+
export const debug = (message) => {
|
|
40
|
+
logger.debug(message);
|
|
41
|
+
};
|
|
42
|
+
export const info = (message) => {
|
|
43
|
+
logger.info(message);
|
|
44
|
+
};
|
|
45
|
+
export const warn = (message) => {
|
|
46
|
+
logger.warn(message);
|
|
47
|
+
};
|
|
48
|
+
export const error = (message) => {
|
|
49
|
+
if (error instanceof Error) {
|
|
50
|
+
logger.error(`${error.message}\n${error.stack}`);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
logger.error(message.toString());
|
|
54
|
+
}
|
|
55
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { error } from './logger.js';
|
|
3
|
+
// 获取规范化的绝对路径
|
|
4
|
+
export function normalizePath(filePath, rootDir) {
|
|
5
|
+
try {
|
|
6
|
+
// 将相对路径转换为绝对路径
|
|
7
|
+
// TODO: 去掉filePath前面的斜杠
|
|
8
|
+
filePath = filePath.replace(/^[\/|\\]+/, '');
|
|
9
|
+
const absolutePath = path.resolve(rootDir, filePath);
|
|
10
|
+
// 规范化路径(处理 .. 和 . 等)
|
|
11
|
+
const normalizedPath = path.normalize(absolutePath);
|
|
12
|
+
// 确保路径在根目录内
|
|
13
|
+
if (!normalizedPath.startsWith(rootDir)) {
|
|
14
|
+
throw new Error('Access denied: Path is outside of root directory');
|
|
15
|
+
}
|
|
16
|
+
return normalizedPath;
|
|
17
|
+
}
|
|
18
|
+
catch (err) {
|
|
19
|
+
error(err instanceof Error ? err : 'Path normalization error');
|
|
20
|
+
throw err;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
// 获取相对于根目录的路径
|
|
24
|
+
export function getRelativePath(absolutePath, rootDir) {
|
|
25
|
+
return path.relative(rootDir, absolutePath);
|
|
26
|
+
}
|
|
27
|
+
// 验证文件名是否安全
|
|
28
|
+
export function validateFileName(fileName) {
|
|
29
|
+
// 禁止的字符和模式
|
|
30
|
+
const invalidChars = /[<>:"/\\|?*\x00-\x1F]/;
|
|
31
|
+
const invalidNames = new Set([
|
|
32
|
+
'CON', 'PRN', 'AUX', 'NUL',
|
|
33
|
+
'COM1', 'COM2', 'COM3', 'COM4',
|
|
34
|
+
'LPT1', 'LPT2', 'LPT3', 'LPT4'
|
|
35
|
+
]);
|
|
36
|
+
// 检查文件名长度
|
|
37
|
+
if (fileName.length === 0 || fileName.length > 255) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
// 检查是否包含非法字符
|
|
41
|
+
if (invalidChars.test(fileName)) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
// 检查是否是保留名称
|
|
45
|
+
const baseName = path.parse(fileName).name.toUpperCase();
|
|
46
|
+
if (invalidNames.has(baseName)) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
// 解析文件扩展名
|
|
52
|
+
export function getFileExtension(filePath) {
|
|
53
|
+
return path.extname(filePath).toLowerCase();
|
|
54
|
+
}
|
|
55
|
+
// 创建安全的文件路径
|
|
56
|
+
export function createSafePath(rootDir, ...pathSegments) {
|
|
57
|
+
const safePath = path.join(rootDir, ...pathSegments);
|
|
58
|
+
if (!safePath.startsWith(rootDir)) {
|
|
59
|
+
throw new Error('Invalid path: Attempted path traversal');
|
|
60
|
+
}
|
|
61
|
+
return safePath;
|
|
62
|
+
}
|