Browse Source

✨ feat: 增加对非文本类型文件的检测判断

Pchen. 2 months ago
parent
commit
51febf2e15
3 changed files with 36 additions and 3 deletions
  1. 17 1
      apis/AI/SummaryFile.js
  2. 18 2
      apis/Repos/GetFileDetail.js
  3. 1 0
      package.json

+ 17 - 1
apis/AI/SummaryFile.js

@@ -5,6 +5,7 @@ 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() {
@@ -14,6 +15,14 @@ class SummaryFile extends API {
         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
 
@@ -43,8 +52,15 @@ class SummaryFile extends API {
             })
 
         try {
-            const time = new Date().getTime()
             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])
 

+ 18 - 2
apis/Repos/GetFileDetail.js

@@ -2,7 +2,8 @@ const API = require("../../lib/API")
 const AccessControl = require("../../lib/AccessControl")
 const { BaseStdResponse } = require("../../BaseStdResponse")
 const db = require("../../plugin/DataBase/db")
-
+const path = require('path')
+const { isBinaryFileSync } = require('isbinaryfile')
 const simpleGit = require('simple-git')
 
 class GetFileDetail extends API {
@@ -13,6 +14,14 @@ class GetFileDetail extends API {
         this.setPath("/Repos/GetFileDetail")
     }
 
+    isBinaryFile(filePath) {
+        try {
+            return isBinaryFileSync(filePath)
+        } catch (err) {
+            return false
+        }
+    }
+
     async onRequest(req, res) {
         let { uuid, session, id, filePath } = req.query
 
@@ -58,9 +67,17 @@ class GetFileDetail extends API {
                 }
             })
 
+            if(this.isBinaryFile(path.join(r[0].path, filePath))) 
+                return res.json({
+                    ...BaseStdResponse.OK,
+                    type: 'binary',
+                    commit: log
+                })
+
             const content = await git.show([`HEAD:${filePath}`])
             res.json({
                 ...BaseStdResponse.OK,
+                type: 'text',
                 commit: log,
                 content: btoa(encodeURI(content))
             })
@@ -71,7 +88,6 @@ class GetFileDetail extends API {
                 msg: '获取文件详情失败!'
             })
         }
-
     }
 }
 

+ 1 - 0
package.json

@@ -14,6 +14,7 @@
     "chalk": "^4.1.2",
     "cors": "^2.8.5",
     "express": "^4.21.1",
+    "isbinaryfile": "^5.0.4",
     "multer": "^1.4.5-lts.1",
     "mysql2": "^3.11.0",
     "nodemailer": "^6.9.14",