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

This commit is contained in:
Liang Ding 2023-02-12 00:23:53 +08:00
parent 698f130ffa
commit 1015ef4037
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
2 changed files with 57 additions and 45 deletions

View file

@ -112,25 +112,35 @@ func resetDuplicateTrees() {
util.PushStatusBar(Conf.Language(58)) util.PushStatusBar(Conf.Language(58))
boxes := Conf.GetBoxes() boxes := Conf.GetBoxes()
luteEngine := lute.New() luteEngine := lute.New()
type TreePath struct {
box *Box
path string
absPath string
}
var paths []*TreePath
for _, box := range boxes { for _, box := range boxes {
boxPath := filepath.Join(util.DataDir, box.ID) boxPath := filepath.Join(util.DataDir, box.ID)
paths := map[string]string{}
filepath.Walk(boxPath, func(path string, info os.FileInfo, err error) error { 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/") { if !info.IsDir() && filepath.Ext(path) == ".sy" && !strings.Contains(filepath.ToSlash(path), "/assets/") {
p := path[len(boxPath):] p := path[len(boxPath):]
p = filepath.ToSlash(p) p = filepath.ToSlash(p)
paths[p] = path paths = append(paths, &TreePath{box, p, path})
} }
return nil return nil
}) })
}
names := map[string]bool{} names := map[string]bool{}
duplicatedPaths := map[string]string{} var duplicatedPaths []*TreePath
for p, absPath := range paths { for _, treePath := range paths {
p := treePath.path
absPath := treePath.absPath
name := path.Base(p) name := path.Base(p)
if !ast.IsNodeIDPattern(strings.TrimSuffix(name, ".sy")) { if !ast.IsNodeIDPattern(strings.TrimSuffix(name, ".sy")) {
logging.LogWarnf("invalid .sy file name [%s]", p) logging.LogWarnf("invalid .sy file name [%s]", p)
box.moveCorruptedData(absPath) treePath.box.moveCorruptedData(absPath)
continue continue
} }
@ -139,10 +149,13 @@ func resetDuplicateTrees() {
continue continue
} }
duplicatedPaths[p] = absPath duplicatedPaths = append(duplicatedPaths, treePath)
} }
for p, absPath := range duplicatedPaths { 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) logging.LogWarnf("exist more than one file with same id [%s], reset it", p)
tree, loadErr := filesys.LoadTree(box.ID, p, luteEngine) tree, loadErr := filesys.LoadTree(box.ID, p, luteEngine)
@ -152,7 +165,7 @@ func resetDuplicateTrees() {
continue continue
} }
resetTree(tree, "Duplicated") resetTree(tree, "")
createTreeTx(tree) createTreeTx(tree)
if gulu.File.IsDir(strings.TrimSuffix(absPath, ".sy")) { if gulu.File.IsDir(strings.TrimSuffix(absPath, ".sy")) {
// 重命名子文档文件夹 // 重命名子文档文件夹
@ -163,11 +176,6 @@ func resetDuplicateTrees() {
} }
os.RemoveAll(absPath) os.RemoveAll(absPath)
} }
if util.IsExiting {
break
}
}
} }
// fixBlockTreeByFileSys 通过文件系统订正块树。 // fixBlockTreeByFileSys 通过文件系统订正块树。

View file

@ -38,14 +38,18 @@ import (
func resetTree(tree *parse.Tree, titleSuffix string) { func resetTree(tree *parse.Tree, titleSuffix string) {
tree.ID = ast.NewNodeID() tree.ID = ast.NewNodeID()
tree.Root.ID = tree.ID tree.Root.ID = tree.ID
if "" != titleSuffix {
if t, parseErr := time.Parse("20060102150405", util.TimeFromID(tree.ID)); nil == parseErr { if t, parseErr := time.Parse("20060102150405", util.TimeFromID(tree.ID)); nil == parseErr {
titleSuffix += " " + t.Format("2006-01-02 15:04:05") titleSuffix += " " + t.Format("2006-01-02 15:04:05")
} else { } else {
titleSuffix = "Duplicated " + time.Now().Format("2006-01-02 15:04:05") 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("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") tree.Root.RemoveIALAttr("scroll")
p := path.Join(path.Dir(tree.Path), tree.ID) + ".sy" p := path.Join(path.Dir(tree.Path), tree.ID) + ".sy"
tree.Path = p tree.Path = p