2025-04-28 12:25:20 +08:00
|
|
|
|
const path = require('path');
|
2025-05-28 16:20:01 +08:00
|
|
|
|
const fs = require('fs'); // 添加同步方法支持
|
|
|
|
|
const fsPromises = require('fs').promises; // 重命名为fsPromises以区分
|
2025-05-15 16:59:12 +08:00
|
|
|
|
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('无法找到数据库文件');
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-16 09:21:11 +08:00
|
|
|
|
// 打开数据库连接,始终以可读可写模式打开
|
|
|
|
|
dbConnection = new Database(dbPath);
|
2025-05-15 16:59:12 +08:00
|
|
|
|
return dbConnection;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('数据库连接失败:', error);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 关闭数据库连接
|
|
|
|
|
function closeDBConnection() {
|
|
|
|
|
if (dbConnection) {
|
|
|
|
|
dbConnection.close();
|
|
|
|
|
dbConnection = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-28 12:25:20 +08:00
|
|
|
|
|
|
|
|
|
// 获取XNCore路径
|
|
|
|
|
function getXNCorePath() {
|
|
|
|
|
const xnCorePath = process.env.XNCore || '';
|
|
|
|
|
if (!xnCorePath) {
|
|
|
|
|
console.error('XNCore环境变量未设置');
|
|
|
|
|
}
|
|
|
|
|
return xnCorePath;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-11 16:39:04 +08:00
|
|
|
|
// 模型项目文件目录路径
|
2025-04-28 12:25:20 +08:00
|
|
|
|
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');
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-11 16:39:04 +08:00
|
|
|
|
//服务项目文件目录路径
|
|
|
|
|
function getProjectServicePath() {
|
|
|
|
|
const xnCorePath = getXNCorePath();
|
|
|
|
|
if (!xnCorePath) return '';
|
|
|
|
|
return path.join(xnCorePath, 'Project', 'Service');
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-28 12:25:20 +08:00
|
|
|
|
// 服务文件目录路径
|
|
|
|
|
function getServicePath() {
|
|
|
|
|
const xnCorePath = getXNCorePath();
|
|
|
|
|
if (!xnCorePath) return '';
|
|
|
|
|
return path.join(xnCorePath, 'Services');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 日志目录路径处理
|
2025-06-11 16:39:04 +08:00
|
|
|
|
async function getActualLogPath() {
|
2025-05-28 16:20:01 +08:00
|
|
|
|
const currentDir = process.cwd();
|
2025-06-11 16:39:04 +08:00
|
|
|
|
const logPath = path.join(currentDir, 'log');
|
2025-04-28 12:25:20 +08:00
|
|
|
|
|
2025-06-11 16:39:04 +08:00
|
|
|
|
// 确保日志目录存在
|
|
|
|
|
await ensureDirectoryExists(logPath);
|
|
|
|
|
|
|
|
|
|
return logPath;
|
2025-04-28 12:25:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查文件路径安全性
|
|
|
|
|
function isPathSafe(filePath, allowedDir) {
|
|
|
|
|
// 安全检查:确保allowedDir不为空
|
|
|
|
|
if (!allowedDir) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 确保路径是绝对路径
|
|
|
|
|
const absolutePath = path.resolve(filePath);
|
|
|
|
|
|
|
|
|
|
// 检查路径是否在允许的目录内
|
|
|
|
|
return absolutePath.startsWith(allowedDir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 确保目录存在,如不存在则创建
|
|
|
|
|
async function ensureDirectoryExists(dirPath) {
|
|
|
|
|
try {
|
2025-05-28 16:20:01 +08:00
|
|
|
|
await fsPromises.access(dirPath);
|
2025-04-28 12:25:20 +08:00
|
|
|
|
} catch (error) {
|
|
|
|
|
if (error.code === 'ENOENT') {
|
|
|
|
|
try {
|
2025-05-28 16:20:01 +08:00
|
|
|
|
await fsPromises.mkdir(dirPath, { recursive: true });
|
2025-04-28 12:25:20 +08:00
|
|
|
|
return true;
|
|
|
|
|
} catch (mkdirError) {
|
|
|
|
|
console.error('创建目录失败:', mkdirError);
|
|
|
|
|
throw mkdirError;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
getXNCorePath,
|
|
|
|
|
getModelPath,
|
|
|
|
|
getProjectModelPath,
|
|
|
|
|
getServicePath,
|
|
|
|
|
getActualLogPath,
|
|
|
|
|
isPathSafe,
|
|
|
|
|
ensureDirectoryExists,
|
2025-05-15 16:59:12 +08:00
|
|
|
|
getDBConnection,
|
|
|
|
|
closeDBConnection
|
2025-04-28 12:25:20 +08:00
|
|
|
|
};
|