diff --git a/kernel/api/block.go b/kernel/api/block.go index bd4c08822..07e8a2653 100644 --- a/kernel/api/block.go +++ b/kernel/api/block.go @@ -371,10 +371,14 @@ func getDocInfo(c *gin.Context) { } id := arg["id"].(string) - info := model.GetDocInfo(id) + info, err := model.GetDocInfo(id) if nil == info { ret.Code = -1 - ret.Msg = fmt.Sprintf(model.Conf.Language(15), id) + if err != nil && !errors.Is(err, model.ErrTreeNotFound) { + ret.Msg = err.Error() + } else { + ret.Msg = fmt.Sprintf(model.Conf.Language(15), id) + } return } if model.IsReadOnlyRoleContext(c) { @@ -676,17 +680,29 @@ func getBlockInfo(c *gin.Context) { // 仅在此处使用带重建索引的加载函数,其他地方不要使用 tree, err := model.LoadTreeByBlockIDWithReindex(id) - if errors.Is(err, model.ErrIndexing) { - ret.Code = 3 - ret.Msg = model.Conf.Language(56) - return - } else if errors.Is(err, treenode.ErrSpecTooNew) { + if err != nil { + if errors.Is(err, model.ErrIndexing) { + ret.Code = 3 + ret.Msg = model.Conf.Language(56) + return + } + if errors.Is(err, treenode.ErrSpecTooNew) { + ret.Code = -1 + ret.Msg = model.Conf.Language(275) + return + } + if errors.Is(err, model.ErrBoxUnindexed) { + ret.Code = -1 + ret.Msg = "" // 加载的时候已经推送过提示了,这里不需要再提示 + return + } + if errors.Is(err, model.ErrTreeNotFound) { + ret.Code = -1 + ret.Msg = fmt.Sprintf(model.Conf.Language(15), id) + return + } ret.Code = -1 - ret.Msg = model.Conf.Language(275) - return - } else if errors.Is(err, model.ErrBoxUnindexed) { - ret.Code = -1 - ret.Msg = "" // 加载的时候已经推送过提示了,这里不需要再提示 + ret.Msg = err.Error() return } diff --git a/kernel/filesys/tree.go b/kernel/filesys/tree.go index 680a1667c..793f585b6 100644 --- a/kernel/filesys/tree.go +++ b/kernel/filesys/tree.go @@ -323,7 +323,7 @@ func prepareWriteTree(tree *parse.Tree) (data []byte, filePath string, err error tree.Root.SetIALAttr("type", "doc") renderer := render.NewJSONRenderer(tree, luteEngine.RenderOptions, luteEngine.ParseOptions) data = renderer.Render() - data = removeUnescapedUnicodeNull(data) + data, _ = removeUnescapedUnicodeNull(data) if !util.UseSingleLineSave { buf := bytes.Buffer{} buf.Grow(1024 * 1024 * 2) @@ -342,14 +342,14 @@ func prepareWriteTree(tree *parse.Tree) (data []byte, filePath string, err error // removeUnescapedUnicodeNull 只移除未被转义的 `\u0000` 字面序列。 // 判断方法:在匹配到 `\u0000` 时向前数连续的 `\` 个数,若为偶数则视为未转义并移除。 -func removeUnescapedUnicodeNull(data []byte) []byte { +func removeUnescapedUnicodeNull(data []byte) (ret []byte, needFix bool) { patLen := 6 // len(`\u0000`) n := len(data) if n < patLen { - return data + return data, false } if !bytes.Contains(data, []byte(`\u0000`)) { - return data + return data, false } dst := make([]byte, 0, n) @@ -386,7 +386,7 @@ func removeUnescapedUnicodeNull(data []byte) []byte { dst = append(dst, data[i]) i++ } - return dst + return dst, len(dst) != n } func afterWriteTree(tree *parse.Tree) { @@ -396,10 +396,14 @@ func afterWriteTree(tree *parse.Tree) { // fixTreeJSONData 订正树 JSON 数据。 func fixTreeJSONData(boxID, p string, jsonData []byte, luteEngine *lute.Lute) (data []byte, needFix bool, err error) { - jsonData = removeUnescapedUnicodeNull(jsonData) - ret, needFix, err := dataparser.ParseJSON(jsonData, luteEngine.ParseOptions) + jsonData, needFix = removeUnescapedUnicodeNull(jsonData) + ret, parseNeedFix, err := dataparser.ParseJSON(jsonData, luteEngine.ParseOptions) + if parseNeedFix { + needFix = true + } if err != nil { logging.LogErrorf("parse json [%s] to tree failed: %s", boxID+p, err) + err = fmt.Errorf("parse json [%s] to tree failed: %w", boxID+p, err) return } @@ -459,6 +463,7 @@ func parseJSON2Tree(boxID, p string, jsonData []byte, luteEngine *lute.Lute) (re ret, _, err = dataparser.ParseJSON(jsonData, luteEngine.ParseOptions) if err != nil { logging.LogErrorf("parse json [%s] to tree failed: %s", boxID+p, err) + err = fmt.Errorf("parse json [%s] to tree failed: %w", boxID+p, err) return } diff --git a/kernel/model/blockinfo.go b/kernel/model/blockinfo.go index 09f3d573c..83c47f4b8 100644 --- a/kernel/model/blockinfo.go +++ b/kernel/model/blockinfo.go @@ -53,7 +53,7 @@ type AttrView struct { Name string `json:"name"` } -func GetDocInfo(blockID string) (ret *BlockInfo) { +func GetDocInfo(blockID string) (ret *BlockInfo, err error) { FlushTxQueue() tree, err := LoadTreeByBlockID(blockID) diff --git a/kernel/model/file.go b/kernel/model/file.go index f3aabdd27..25913c355 100644 --- a/kernel/model/file.go +++ b/kernel/model/file.go @@ -1212,12 +1212,12 @@ func GetHPathByPath(boxID, p string) (hPath string, err error) { return } - luteEngine := util.NewLute() - tree, err := filesys.LoadTree(boxID, p, luteEngine) - if err != nil { + bt := treenode.GetBlockTreeByBoxPath(boxID, p) + if nil == bt { + err = ErrBlockNotFound return } - hPath = tree.HPath + hPath = bt.HPath return } @@ -1229,14 +1229,13 @@ func GetHPathsByPaths(paths []string) (hPaths []string, err error) { continue } - bt := treenode.GetBlockTreeByPath(p) + bt := treenode.GetBlockTreeByBoxPath(box.ID, p) if nil == bt { logging.LogWarnf("block tree not found by path [%s]", p) continue } - hpath := html.UnescapeString(bt.HPath) - hPaths = append(hPaths, box.Name+hpath) + hPaths = append(hPaths, box.Name+bt.HPath) } return } diff --git a/kernel/treenode/blocktree.go b/kernel/treenode/blocktree.go index 4b3131451..081465013 100644 --- a/kernel/treenode/blocktree.go +++ b/kernel/treenode/blocktree.go @@ -153,10 +153,10 @@ func GetBlockTreesByType(typ string) (ret []*BlockTree) { return } -func GetBlockTreeByPath(path string) (ret *BlockTree) { +func GetBlockTreeByBoxPath(boxID, path string) (ret *BlockTree) { ret = &BlockTree{} - sqlStmt := "SELECT * FROM blocktrees WHERE path = ?" - err := queryRow(sqlStmt, path).Scan(&ret.ID, &ret.RootID, &ret.ParentID, &ret.BoxID, &ret.Path, &ret.HPath, &ret.Updated, &ret.Type) + sqlStmt := "SELECT * FROM blocktrees WHERE box_id = ? AND path = ?" + err := queryRow(sqlStmt, boxID, path).Scan(&ret.ID, &ret.RootID, &ret.ParentID, &ret.BoxID, &ret.Path, &ret.HPath, &ret.Updated, &ret.Type) if err != nil { ret = nil if errors.Is(err, sql.ErrNoRows) {