UNPKG

ai-knowledge

Version:

ai-knowledge

57 lines (49 loc) 1.21 kB
const fs = require('fs'); const path = require('path'); /** * 获取目录中的所有文件(递归) */ function getAllFiles(dir, fileList = []) { const files = fs.readdirSync(dir); files.forEach(file => { const filePath = path.join(dir, file); if (fs.statSync(filePath).isDirectory()) { getAllFiles(filePath, fileList); } else { fileList.push(filePath); } }); return fileList; } /** * 确保目录存在 */ function ensureDir(dir) { if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }); } } /** * 获取文件扩展名 */ function getFileExtension(filePath) { return path.extname(filePath).toLowerCase(); } /** * 检查文件是否是支持的格式 */ function isSupportedFile(filePath) { const supportedExtensions = [ '.txt', '.html', '.htm', '.pdf', '.docx', '.doc', '.pptx', '.ppt', '.xlsx', '.xls', '.epub', '.md' ]; return supportedExtensions.includes(getFileExtension(filePath)); } module.exports = { getAllFiles, ensureDir, getFileExtension, isSupportedFile };