116 lines
2.7 KiB
JavaScript
116 lines
2.7 KiB
JavaScript
|
const path = require('path');
|
|||
|
const fs = require('fs').promises;
|
|||
|
|
|||
|
// 获取XNCore路径
|
|||
|
function getXNCorePath() {
|
|||
|
const xnCorePath = process.env.XNCore || '';
|
|||
|
if (!xnCorePath) {
|
|||
|
console.error('XNCore环境变量未设置');
|
|||
|
}
|
|||
|
return xnCorePath;
|
|||
|
}
|
|||
|
|
|||
|
// 场景文件目录路径
|
|||
|
function getScenarioPath() {
|
|||
|
const xnCorePath = getXNCorePath();
|
|||
|
if (!xnCorePath) return '';
|
|||
|
return path.join(xnCorePath, 'Scenario');
|
|||
|
}
|
|||
|
|
|||
|
// 项目模型文件目录路径
|
|||
|
function getProjectModelPath() {
|
|||
|
const xnCorePath = getXNCorePath();
|
|||
|
if (!xnCorePath) return '';
|
|||
|
return path.join(xnCorePath, 'Project', 'Model');
|
|||
|
}
|
|||
|
|
|||
|
// 模型文件目录路径
|
|||
|
function getModelPath() {
|
|||
|
const xnCorePath = getXNCorePath();
|
|||
|
if (!xnCorePath) return '';
|
|||
|
return path.join(xnCorePath, 'Models');
|
|||
|
}
|
|||
|
|
|||
|
// 服务文件目录路径
|
|||
|
function getServicePath() {
|
|||
|
const xnCorePath = getXNCorePath();
|
|||
|
if (!xnCorePath) return '';
|
|||
|
return path.join(xnCorePath, 'Services');
|
|||
|
}
|
|||
|
|
|||
|
// 日志目录路径处理
|
|||
|
function getActualLogPath(requestedPath) {
|
|||
|
const xnCorePath = getXNCorePath();
|
|||
|
|
|||
|
// 如果请求的路径是/log开头,将它放在XNCore下
|
|||
|
if (requestedPath.startsWith('/log')) {
|
|||
|
return path.join(xnCorePath, requestedPath);
|
|||
|
}
|
|||
|
// 如果已经是绝对路径并且在XNCore下
|
|||
|
else if (requestedPath.startsWith(xnCorePath)) {
|
|||
|
return requestedPath;
|
|||
|
}
|
|||
|
// 默认不允许访问其他路径
|
|||
|
else {
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// 检查文件路径安全性
|
|||
|
function isPathSafe(filePath, allowedDir) {
|
|||
|
// 安全检查:确保allowedDir不为空
|
|||
|
if (!allowedDir) {
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
// 确保路径是绝对路径
|
|||
|
const absolutePath = path.resolve(filePath);
|
|||
|
|
|||
|
// 检查路径是否在允许的目录内
|
|||
|
return absolutePath.startsWith(allowedDir);
|
|||
|
}
|
|||
|
|
|||
|
// 确保目录存在,如不存在则创建
|
|||
|
async function ensureDirectoryExists(dirPath) {
|
|||
|
try {
|
|||
|
await fs.access(dirPath);
|
|||
|
} catch (error) {
|
|||
|
if (error.code === 'ENOENT') {
|
|||
|
try {
|
|||
|
await fs.mkdir(dirPath, { recursive: true });
|
|||
|
return true;
|
|||
|
} catch (mkdirError) {
|
|||
|
console.error('创建目录失败:', mkdirError);
|
|||
|
throw mkdirError;
|
|||
|
}
|
|||
|
} else {
|
|||
|
throw error;
|
|||
|
}
|
|||
|
}
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
// XML 验证
|
|||
|
async function validateXml(content) {
|
|||
|
if (!content || !content.trim()) return true;
|
|||
|
|
|||
|
try {
|
|||
|
const parser = new (require('xml2js')).Parser();
|
|||
|
await parser.parseStringPromise(content);
|
|||
|
return true;
|
|||
|
} catch (xmlError) {
|
|||
|
return { error: 'XML格式无效', details: xmlError.message };
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
module.exports = {
|
|||
|
getXNCorePath,
|
|||
|
getScenarioPath,
|
|||
|
getModelPath,
|
|||
|
getProjectModelPath,
|
|||
|
getServicePath,
|
|||
|
getActualLogPath,
|
|||
|
isPathSafe,
|
|||
|
ensureDirectoryExists,
|
|||
|
validateXml
|
|||
|
};
|