XNSim/XNSimHtml/utils/file-utils.js

156 lines
3.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const path = require('path');
const fs = require('fs').promises;
const Database = require('better-sqlite3');
// 数据库连接管理
let dbConnection = null;
// 获取数据库连接
function getDBConnection(readonly = false) {
try {
if (dbConnection) {
return dbConnection;
}
const xnCorePath = getXNCorePath();
if (!xnCorePath) {
throw new Error('XNCore环境变量未设置无法获取数据库路径');
}
const dbPath = path.join(xnCorePath, 'database', 'XNSim.db');
if (!dbPath) {
throw new Error('无法找到数据库文件');
}
// 打开数据库连接,始终以可读可写模式打开
dbConnection = new Database(dbPath);
return dbConnection;
} catch (error) {
console.error('数据库连接失败:', error);
throw error;
}
}
// 关闭数据库连接
function closeDBConnection() {
if (dbConnection) {
dbConnection.close();
dbConnection = null;
}
}
// 获取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,
getDBConnection,
closeDBConnection
};