Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: 文档数据文件 ID 重复时自动重置 ID Fix https://github.com/siyuan-note/siyuan/issues/7340

Liang Ding1015ef403731
diff --git a/kernel/model/index_fix.go b/kernel/model/index_fix.go
index a01c4feeb4..d795a54d3f 100644
--- a/kernel/model/index_fix.go
+++ b/kernel/model/index_fix.go
@@ -112,61 +112,69 @@ func resetDuplicateTrees() {
util.PushStatusBar(Conf.Language(58))
boxes := Conf.GetBoxes()
luteEngine := lute.New()
+
+ type TreePath struct {
+ box *Box
+ path string
+ absPath string
+ }
+
+ var paths []*TreePath
for _, box := range boxes {
boxPath := filepath.Join(util.DataDir, box.ID)
- paths := map[string]string{}
filepath.Walk(boxPath, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() && filepath.Ext(path) == ".sy" && !strings.Contains(filepath.ToSlash(path), "/assets/") {
p := path[len(boxPath):]
p = filepath.ToSlash(p)
- paths[p] = path
+ paths = append(paths, &TreePath{box, p, path})
}
return nil
})
+ }
- names := map[string]bool{}
- duplicatedPaths := map[string]string{}
- for p, absPath := range paths {
- name := path.Base(p)
- if !ast.IsNodeIDPattern(strings.TrimSuffix(name, ".sy")) {
- logging.LogWarnf("invalid .sy file name [%s]", p)
- box.moveCorruptedData(absPath)
- continue
- }
-
- if !names[name] {
- names[name] = true
- continue
- }
+ names := map[string]bool{}
+ var duplicatedPaths []*TreePath
+ for _, treePath := range paths {
+ p := treePath.path
+ absPath := treePath.absPath
+ name := path.Base(p)
+ if !ast.IsNodeIDPattern(strings.TrimSuffix(name, ".sy")) {
+ logging.LogWarnf("invalid .sy file name [%s]", p)
+ treePath.box.moveCorruptedData(absPath)
+ continue
+ }
- duplicatedPaths[p] = absPath
+ if !names[name] {
+ names[name] = true
+ continue
}
- for p, absPath := range duplicatedPaths {
- logging.LogWarnf("exist more than one file with same id [%s], reset it", p)
+ duplicatedPaths = append(duplicatedPaths, treePath)
+ }
- tree, loadErr := filesys.LoadTree(box.ID, p, luteEngine)
- if nil != loadErr {
- logging.LogWarnf("load tree [%s] failed: %s", p, loadErr)
- box.moveCorruptedData(absPath)
- continue
- }
+ for _, duplicatedPath := range duplicatedPaths {
+ p := duplicatedPath.path
+ absPath := duplicatedPath.absPath
+ box := duplicatedPath.box
+ logging.LogWarnf("exist more than one file with same id [%s], reset it", p)
- resetTree(tree, "Duplicated")
- createTreeTx(tree)
- if gulu.File.IsDir(strings.TrimSuffix(absPath, ".sy")) {
- // 重命名子文档文件夹
- if renameErr := os.Rename(strings.TrimSuffix(absPath, ".sy"), filepath.Join(filepath.Dir(absPath), tree.ID)); nil != renameErr {
- logging.LogWarnf("rename [%s] failed: %s", absPath, renameErr)
- continue
- }
- }
- os.RemoveAll(absPath)
+ tree, loadErr := filesys.LoadTree(box.ID, p, luteEngine)
+ if nil != loadErr {
+ logging.LogWarnf("load tree [%s] failed: %s", p, loadErr)
+ box.moveCorruptedData(absPath)
+ continue
}
- if util.IsExiting {
- break
+ resetTree(tree, "")
+ createTreeTx(tree)
+ if gulu.File.IsDir(strings.TrimSuffix(absPath, ".sy")) {
+ // 重命名子文档文件夹
+ if renameErr := os.Rename(strings.TrimSuffix(absPath, ".sy"), filepath.Join(filepath.Dir(absPath), tree.ID)); nil != renameErr {
+ logging.LogWarnf("rename [%s] failed: %s", absPath, renameErr)
+ continue
+ }
}
+ os.RemoveAll(absPath)
}
}
diff --git a/kernel/model/tree.go b/kernel/model/tree.go
index 1c4a0ff6cc..c13ad975dc 100644
--- a/kernel/model/tree.go
+++ b/kernel/model/tree.go
@@ -38,14 +38,18 @@ import (
func resetTree(tree *parse.Tree, titleSuffix string) {
tree.ID = ast.NewNodeID()
tree.Root.ID = tree.ID
- if t, parseErr := time.Parse("20060102150405", util.TimeFromID(tree.ID)); nil == parseErr {
- titleSuffix += " " + t.Format("2006-01-02 15:04:05")
- } else {
- titleSuffix = "Duplicated " + time.Now().Format("2006-01-02 15:04:05")
+
+ if "" != titleSuffix {
+ if t, parseErr := time.Parse("20060102150405", util.TimeFromID(tree.ID)); nil == parseErr {
+ titleSuffix += " " + t.Format("2006-01-02 15:04:05")
+ } else {
+ titleSuffix = "Duplicated " + time.Now().Format("2006-01-02 15:04:05")
+ }
+ titleSuffix = "(" + titleSuffix + ")"
+ titleSuffix = " " + titleSuffix
}
- titleSuffix = "(" + titleSuffix + ")"
tree.Root.SetIALAttr("id", tree.ID)
- tree.Root.SetIALAttr("title", tree.Root.IALAttr("title")+" "+titleSuffix)
+ tree.Root.SetIALAttr("title", tree.Root.IALAttr("title")+titleSuffix)
tree.Root.RemoveIALAttr("scroll")
p := path.Join(path.Dir(tree.Path), tree.ID) + ".sy"
tree.Path = p