const API = require("../../lib/API") const AccessControl = require("../../lib/AccessControl") const { BaseStdResponse } = require("../../BaseStdResponse") const db = require("../../plugin/DataBase/db") const axios = require('axios') const { core_url } = require('../../config.json') const path = require('path') const { isBinaryFileSync } = require('isbinaryfile') class SummaryFile extends API { constructor() { super() this.setMethod("GET") this.setPath("/AI/SummaryFile") } isBinaryFile(filePath) { try { return isBinaryFileSync(filePath) } catch (err) { return false } } async onRequest(req, res) { let { uuid, session, id, hash, filePath } = req.query if ([uuid, session, id, hash, filePath].some(value => value === '' || value === null || value === undefined)) return res.json({ ...BaseStdResponse.MISSING_PARAMETER }) // 检查 session if (!await AccessControl.checkSession(uuid, session)) return res.status(401).json({ ...BaseStdResponse.ACCESS_DENIED }) let sql = 'SELECT state, path, url FROM repos WHERE create_user = ? AND id = ?' let rows = await db.query(sql, [uuid, id]) if (!rows || rows.length === 0) return res.json({ ...BaseStdResponse.ERR, msg: '未找到仓库' }) if (rows[0].state !== 1 || !rows[0].path) return res.json({ ...BaseStdResponse.ERR, msg: '仓库未成功克隆!' }) try { filePath = path.join(rows[0].path, filePath) if(this.isBinaryFile(filePath)) return res.json({ ...BaseStdResponse.ERR, msg: '该文件类型不支持AI总结!' }) const time = new Date().getTime() sql = 'INSERT INTO file_summary_tasks (repo_id, create_time, create_user, repo_hash, filepath) VALUES (?, ?, ?, ?, ?)' let r = await db.query(sql, [id, time, uuid, hash, filePath, 'hand']) if (!r || r.affectedRows !== 1) return res.json({ ...BaseStdResponse.ERR, msg: '扫描任务添加失败!' }) res.json({ ...BaseStdResponse.OK }) let endpoint = core_url + '/ai/summaryFile' await axios.post(endpoint, { uuid, repo_url: rows[0].url, task_id: String(r.insertId), file_path: filePath }) } catch (error) { this.logger.error('添加AI分析任务失败!' + error.stack) return res.json({ ...BaseStdResponse.ERR, msg: '添加AI分析任务失败!' }) } } } module.exports.SummaryFile = SummaryFile