GetFileSummary.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. const API = require("../../lib/API")
  2. const db = require("../../plugin/DataBase/db")
  3. const AccessControl = require("../../lib/AccessControl")
  4. const { BaseStdResponse } = require("../../BaseStdResponse")
  5. const path = require('path')
  6. class GetFileSummary extends API {
  7. constructor() {
  8. super()
  9. this.setPath('/AI/GetFileSummary')
  10. this.setMethod('GET')
  11. }
  12. async onRequest(req, res) {
  13. let { uuid, session, id, hash, filePath } = req.query
  14. if ([uuid, session, id, hash, filePath].some(value => value === '' || value === null || value === undefined))
  15. return res.json({
  16. ...BaseStdResponse.MISSING_PARAMETER
  17. })
  18. // 检查 session
  19. if (!await AccessControl.checkSession(uuid, session))
  20. return res.status(401).json({
  21. ...BaseStdResponse.ACCESS_DENIED
  22. })
  23. let sql = 'SELECT state, path FROM repos WHERE create_user = ? AND id = ?'
  24. let rows = await db.query(sql, [uuid, id])
  25. if (!rows || rows.length === 0)
  26. return res.json({
  27. ...BaseStdResponse.ERR,
  28. msg: '未找到仓库'
  29. })
  30. if (rows[0].state !== 1 || !rows[0].path)
  31. return res.json({
  32. ...BaseStdResponse.ERR,
  33. msg: '仓库未成功克隆!'
  34. })
  35. filePath = path.join(rows[0].path, filePath)
  36. sql = 'SELECT create_time, start_time, end_time, result, repo_hash FROM file_summary_tasks WHERE create_user = ? AND repo_id = ? AND repo_hash = ? AND filepath = ?'
  37. rows = await db.query(sql, [uuid, id, hash, filePath])
  38. if (!rows || rows.length === 0)
  39. return res.json({
  40. ...BaseStdResponse.OK,
  41. data: []
  42. })
  43. res.json({
  44. ...BaseStdResponse.OK,
  45. data: rows
  46. })
  47. }
  48. }
  49. module.exports.GetFileSummary = GetFileSummary