feat: 同步知识库与工作台相关改动
This commit is contained in:
@@ -87,6 +87,14 @@ func parseBind(c *gin.Context) (string, *uint) {
|
||||
return bt, bid
|
||||
}
|
||||
|
||||
func parseKnowledgeSpaceKey(c *gin.Context) string {
|
||||
key := sanitizeSpaceKey(c.PostForm("knowledge_space_key"))
|
||||
if key == "" {
|
||||
return "general"
|
||||
}
|
||||
return ensureKnowledgeSpaceKeyOrDefault(key)
|
||||
}
|
||||
|
||||
func sourceOf(c *gin.Context) string {
|
||||
u := middleware.CurrentUser(c)
|
||||
if u != nil && u.Role == "admin" {
|
||||
@@ -122,6 +130,7 @@ func Upload(c *gin.Context) {
|
||||
}
|
||||
|
||||
bindType, bindID := parseBind(c)
|
||||
knowledgeSpaceKey := parseKnowledgeSpaceKey(c)
|
||||
source := sourceOf(c)
|
||||
status := "pending"
|
||||
targetDir := pendingDir()
|
||||
@@ -149,16 +158,17 @@ func Upload(c *gin.Context) {
|
||||
out.Close()
|
||||
|
||||
m := model.MediaFile{
|
||||
Filename: header.Filename,
|
||||
StoredName: storedName,
|
||||
StoredPath: storedName,
|
||||
FileExt: ext,
|
||||
FileSize: header.Size,
|
||||
Status: status,
|
||||
Source: source,
|
||||
SubmitterID: u.ID,
|
||||
BindType: bindType,
|
||||
BindID: bindID,
|
||||
Filename: header.Filename,
|
||||
StoredName: storedName,
|
||||
StoredPath: storedName,
|
||||
FileExt: ext,
|
||||
FileSize: header.Size,
|
||||
Status: status,
|
||||
Source: source,
|
||||
SubmitterID: u.ID,
|
||||
BindType: bindType,
|
||||
BindID: bindID,
|
||||
KnowledgeSpaceKey: knowledgeSpaceKey,
|
||||
}
|
||||
if err := store.DB.Create(&m).Error; err != nil {
|
||||
web.Fail(c, web.NewBadRequest("创建素材记录失败"))
|
||||
@@ -173,14 +183,15 @@ func Upload(c *gin.Context) {
|
||||
// ============ 分片上传 ============
|
||||
|
||||
type uploadSession struct {
|
||||
Filename string
|
||||
FileSize int64
|
||||
Ext string
|
||||
BindType string
|
||||
BindID *uint
|
||||
ChunkSize int64
|
||||
ChunkCount int
|
||||
Chunks map[int]bool
|
||||
Filename string
|
||||
FileSize int64
|
||||
Ext string
|
||||
BindType string
|
||||
BindID *uint
|
||||
ChunkSize int64
|
||||
ChunkCount int
|
||||
Chunks map[int]bool
|
||||
KnowledgeSpaceKey string
|
||||
}
|
||||
|
||||
var uploadSessions = struct {
|
||||
@@ -191,10 +202,11 @@ var uploadSessions = struct {
|
||||
// UploadInit POST /api/media/upload-init —— 初始化分片上传
|
||||
func UploadInit(c *gin.Context) {
|
||||
var req struct {
|
||||
Filename string `json:"filename"`
|
||||
FileSize int64 `json:"file_size"`
|
||||
BindType string `json:"bind_type"`
|
||||
BindID *uint `json:"bind_id"`
|
||||
Filename string `json:"filename"`
|
||||
FileSize int64 `json:"file_size"`
|
||||
BindType string `json:"bind_type"`
|
||||
BindID *uint `json:"bind_id"`
|
||||
KnowledgeSpaceKey string `json:"knowledge_space_key"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil || req.Filename == "" || req.FileSize <= 0 {
|
||||
web.Fail(c, web.NewBadRequest("filename/file_size 必填"))
|
||||
@@ -220,6 +232,7 @@ func UploadInit(c *gin.Context) {
|
||||
BindType: req.BindType, BindID: req.BindID,
|
||||
ChunkSize: defaultChunkSize, ChunkCount: chunkCount, Chunks: map[int]bool{},
|
||||
}
|
||||
sess.KnowledgeSpaceKey = ensureKnowledgeSpaceKeyOrDefault(sanitizeSpaceKey(req.KnowledgeSpaceKey))
|
||||
uploadSessions.Lock()
|
||||
uploadSessions.m[id] = sess
|
||||
uploadSessions.Unlock()
|
||||
@@ -346,6 +359,7 @@ func UploadComplete(c *gin.Context) {
|
||||
Filename: sess.Filename, StoredName: storedName, StoredPath: storedName,
|
||||
FileExt: sess.Ext, FileSize: sess.FileSize, Status: status,
|
||||
Source: source, SubmitterID: u.ID, BindType: sess.BindType, BindID: sess.BindID,
|
||||
KnowledgeSpaceKey: sess.KnowledgeSpaceKey,
|
||||
}
|
||||
if err := store.DB.Create(&m).Error; err != nil {
|
||||
web.Fail(c, web.NewBadRequest("创建素材记录失败"))
|
||||
@@ -394,7 +408,7 @@ func MediaStatus(c *gin.Context) {
|
||||
}
|
||||
var chunkCount int64
|
||||
store.DB.Model(&model.KnowledgeChunk{}).Where("media_file_id = ?", m.ID).Count(&chunkCount)
|
||||
web.OK(c, gin.H{"status": m.Status, "extracted": m.Extracted, "chunk_count": chunkCount})
|
||||
web.OK(c, gin.H{"status": m.Status, "extracted": m.Extracted, "chunk_count": chunkCount, "knowledge_space_key": m.KnowledgeSpaceKey})
|
||||
}
|
||||
|
||||
// ============ 审批 ============
|
||||
@@ -405,6 +419,9 @@ func AuditList(c *gin.Context) {
|
||||
if s := c.Query("status"); s != "" {
|
||||
q = q.Where("status = ?", s)
|
||||
}
|
||||
if key := sanitizeSpaceKey(c.Query("knowledge_space_key")); key != "" {
|
||||
q = q.Where("knowledge_space_key = ?", key)
|
||||
}
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
size, _ := strconv.Atoi(c.DefaultQuery("size", "20"))
|
||||
if page < 1 {
|
||||
@@ -531,14 +548,16 @@ func runExtractPipeline(mediaID uint) {
|
||||
continue
|
||||
}
|
||||
store.DB.Create(&model.KnowledgeChunk{
|
||||
MediaFileID: &m.ID,
|
||||
SourceType: m.FileExt,
|
||||
SourceID: strconv.FormatUint(uint64(m.ID), 10),
|
||||
ChunkIndex: i,
|
||||
Content: chunk,
|
||||
MediaFileID: &m.ID,
|
||||
SourceType: m.FileExt,
|
||||
SourceID: strconv.FormatUint(uint64(m.ID), 10),
|
||||
KnowledgeSpaceKey: resolveMediaKnowledgeSpaceKey(m),
|
||||
ChunkIndex: i,
|
||||
Content: chunk,
|
||||
})
|
||||
}
|
||||
store.DB.Model(&m).Update("extracted", true)
|
||||
triggerKnowledgeIndexRebuild()
|
||||
log.Printf("[提取完成] media_id=%d chunks=%d", mediaID, len(chunks))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user