135 lines
3.2 KiB
JavaScript
135 lines
3.2 KiB
JavaScript
const path = require('path');
|
||
const fs = require('fs'); // 添加同步方法支持
|
||
const fsPromises = require('fs').promises; // 重命名为fsPromises以区分
|
||
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 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 getProjectServicePath() {
|
||
const xnCorePath = getXNCorePath();
|
||
if (!xnCorePath) return '';
|
||
return path.join(xnCorePath, 'Project', 'Service');
|
||
}
|
||
|
||
// 服务文件目录路径
|
||
function getServicePath() {
|
||
const xnCorePath = getXNCorePath();
|
||
if (!xnCorePath) return '';
|
||
return path.join(xnCorePath, 'Services');
|
||
}
|
||
|
||
// 日志目录路径处理
|
||
async function getActualLogPath() {
|
||
const currentDir = process.cwd();
|
||
const logPath = path.join(currentDir, 'log');
|
||
|
||
// 确保日志目录存在
|
||
await ensureDirectoryExists(logPath);
|
||
|
||
return logPath;
|
||
}
|
||
|
||
// 检查文件路径安全性
|
||
function isPathSafe(filePath, allowedDir) {
|
||
// 安全检查:确保allowedDir不为空
|
||
if (!allowedDir) {
|
||
return false;
|
||
}
|
||
|
||
// 确保路径是绝对路径
|
||
const absolutePath = path.resolve(filePath);
|
||
|
||
// 检查路径是否在允许的目录内
|
||
return absolutePath.startsWith(allowedDir);
|
||
}
|
||
|
||
// 确保目录存在,如不存在则创建
|
||
async function ensureDirectoryExists(dirPath) {
|
||
try {
|
||
await fsPromises.access(dirPath);
|
||
} catch (error) {
|
||
if (error.code === 'ENOENT') {
|
||
try {
|
||
await fsPromises.mkdir(dirPath, { recursive: true });
|
||
return true;
|
||
} catch (mkdirError) {
|
||
console.error('创建目录失败:', mkdirError);
|
||
throw mkdirError;
|
||
}
|
||
} else {
|
||
throw error;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
module.exports = {
|
||
getXNCorePath,
|
||
getModelPath,
|
||
getProjectModelPath,
|
||
getServicePath,
|
||
getActualLogPath,
|
||
isPathSafe,
|
||
ensureDirectoryExists,
|
||
getDBConnection,
|
||
closeDBConnection
|
||
};
|