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; } // 获取上传文件目录路径 async function getUploadPath() { const currentDir = process.cwd(); const uploadPath = path.join(currentDir, 'upload'); // 确保上传目录存在 await ensureDirectoryExists(uploadPath); return uploadPath; } // 保存上传的文件 async function saveUploadedFile(file) { try { const uploadPath = await getUploadPath(); const filePath = path.join(uploadPath, file.originalname); // 如果文件已存在,添加时间戳 if (fs.existsSync(filePath)) { const timestamp = new Date().getTime(); const ext = path.extname(file.originalname); const name = path.basename(file.originalname, ext); const newFileName = `${name}_${timestamp}${ext}`; return path.join(uploadPath, newFileName); } return filePath; } catch (error) { console.error('处理上传文件路径失败:', error); throw error; } } // 检查文件类型是否允许 function isAllowedFileType(fileName, allowedTypes = []) { if (!allowedTypes || allowedTypes.length === 0) { return true; // 如果没有指定允许的类型,则允许所有类型 } const ext = path.extname(fileName).toLowerCase(); return allowedTypes.includes(ext); } module.exports = { getXNCorePath, getModelPath, getProjectModelPath, getServicePath, getActualLogPath, isPathSafe, ensureDirectoryExists, getDBConnection, closeDBConnection, getUploadPath, saveUploadedFile, isAllowedFileType };