feat(kb): 大文档正文存 MinIO(PG 只留元数据+预览+对象键)
超过阈值(8000 字)的正文落对象存储,彻底解决十几万字文件塞 PG 的问题。 - internal/blob:minio-go 封装 Store(Open/Put/Get/Delete + Ready 降级);连不上则降级内联。 - docker-compose:milvus-minio 暴露 9000 端口供网关用作文档对象存储(bucket sundynix-docs)。 - main/router/handler:注入 blob.Store(env MINIO_*,默认 localhost:9000 minioadmin)。 - runIngest:size>8000 且 MinIO 可用 → 正文 Put 到 owner/kb/name,PG content 置空仅存 object_key+preview+size;否则内联。SaveDoc 改为按全文显式传 preview(offload 后内联为空也有预览)。 - KbDoc:object_key 非空时从 MinIO 取回全文。 验证:入 12182 字笔记 → PG content_len=0、object_key=wt/default/超大文件测试、preview 非空、 size=12182;/kb/doc 取回完整 12182 字(来自 MinIO);6321 字的仍内联(object_key 空)。 列表只读元数据+预览。gateway build 通过。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
@@ -20,6 +21,9 @@ import (
|
||||
"github.com/sundynix/sundynix-shared/contract"
|
||||
)
|
||||
|
||||
// docInlineMax 是内联存 PG 的正文字数上限;超过则正文落 MinIO,PG 只留元数据+预览+对象键。
|
||||
const docInlineMax = 8000
|
||||
|
||||
// rawKB 规整知识库名(去空白,空则 default)—— 注册表里的展示名。
|
||||
func rawKB(kb string) string {
|
||||
kb = strings.TrimSpace(kb)
|
||||
@@ -139,7 +143,13 @@ func (h *Handler) KbDoc(c *gin.Context) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "文档不存在"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"name": d.Name, "content": d.Content, "size": d.Size})
|
||||
content := d.Content
|
||||
if d.ObjectKey != "" && h.blob.Ready() { // 大文档:从 MinIO 取回正文
|
||||
if obj, oerr := h.blob.Get(c.Request.Context(), d.ObjectKey); oerr == nil {
|
||||
content = obj
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"name": d.Name, "content": content, "size": d.Size})
|
||||
}
|
||||
|
||||
// KbLinks: GET /api/v1/kb/links?kb= —— 某库全部 [[双链]](from→to),供反链/笔记关系图。
|
||||
@@ -232,7 +242,18 @@ func (h *Handler) runIngest(job, owner, kbName, scoped, forceDoc, filename strin
|
||||
docName = noteName(text)
|
||||
}
|
||||
if text != "" {
|
||||
_ = h.db.SaveDoc(ctx, owner, kbName, docName, text, "", len([]rune(text)))
|
||||
size := len([]rune(text))
|
||||
inline, objectKey := text, ""
|
||||
// 大文档正文落对象存储,PG 只留元数据+预览+对象键(避免把十几万字塞进 PG)。
|
||||
if size > docInlineMax && h.blob.Ready() {
|
||||
key := owner + "/" + kbName + "/" + docName
|
||||
if err := h.blob.Put(ctx, key, text); err == nil {
|
||||
inline, objectKey = "", key
|
||||
} else {
|
||||
log.Printf("[gateway] 大文档转 MinIO 失败,回退内联: %v", err)
|
||||
}
|
||||
}
|
||||
_ = h.db.SaveDoc(ctx, owner, kbName, docName, inline, objectKey, size, head(text, 500))
|
||||
_ = h.db.ReplaceDocLinks(ctx, owner, kbName, docName, wikiLinks(text)) // 维护 [[双链]] 索引
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user