Skip to content

Commit

Permalink
🐛 Improve importing .sy.zip #12825
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Oct 18, 2024
1 parent d4c049d commit 1b9bc4d
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions kernel/model/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {

luteEngine := util.NewLute()
blockIDs := map[string]string{}
avBlockIDs := map[string]string{}
trees := map[string]*parse.Tree{}

// 重新生成块 ID
Expand All @@ -170,19 +169,8 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {
// Keep original creation time when importing .sy.zip https://github.com/siyuan-note/siyuan/issues/9923
newNodeID := util.TimeFromID(n.ID) + "-" + util.RandString(7)
blockIDs[n.ID] = newNodeID
oldNodeID := n.ID
n.ID = newNodeID
n.SetIALAttr("id", newNodeID)

// 重新指向数据库属性值
for _, kv := range n.KramdownIAL {
if 2 > len(kv) {
continue
}
if strings.HasPrefix(kv[0], av.NodeAttrNameAvs) {
avBlockIDs[oldNodeID] = newNodeID
}
}
return ast.WalkContinue
})
tree.ID = tree.Root.ID
Expand Down Expand Up @@ -249,14 +237,14 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {
return nil
}

// 将数据库文件中的块 ID 替换为新的块 ID
// 将数据库文件中的 ID 替换为新的 ID
newData := data
for oldAvID, newAvID := range avIDs {
for oldID, newID := range avBlockIDs {
newData = bytes.ReplaceAll(newData, []byte(oldID), []byte(newID))
}
newData = bytes.ReplaceAll(newData, []byte(oldAvID), []byte(newAvID))
}
for oldID, newID := range blockIDs {
newData = bytes.ReplaceAll(newData, []byte(oldID), []byte(newID))
}
if !bytes.Equal(data, newData) {
if writeErr := os.WriteFile(oldPath, newData, 0644); nil != writeErr {
logging.LogErrorf("write av file [%s] failed: %s", oldPath, writeErr)
Expand Down

0 comments on commit 1b9bc4d

Please sign in to comment.