From 14eb394af39a740a4a8bf5c82fb4840199bec11e Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Mon, 25 Sep 2023 23:22:34 +0800 Subject: [PATCH] :art: Update av export --- kernel/model/import.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/kernel/model/import.go b/kernel/model/import.go index b384f7eec..8e15979d2 100644 --- a/kernel/model/import.go +++ b/kernel/model/import.go @@ -123,6 +123,7 @@ 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 @@ -146,9 +147,19 @@ func ImportSY(zipPath, boxID, toPath string) (err error) { if "" != n.ID { newNodeID := ast.NewNodeID() blockIDs[n.ID] = newNodeID + oldNodeID := n.ID n.ID = newNodeID n.SetIALAttr("id", newNodeID) + // 重新指向数据库属性值 + ial := parse.IAL2Map(n.KramdownIAL) + for k, v := range ial { + if strings.HasPrefix(k, NodeAttrNamePrefixAvKey) { + v = strings.ReplaceAll(v, oldNodeID, newNodeID) + n.SetIALAttr(k, v) + avBlockIDs[oldNodeID] = newNodeID + } + } } return ast.WalkContinue }) @@ -190,11 +201,39 @@ func ImportSY(zipPath, boxID, toPath string) (err error) { // 将关联的数据库文件移动到 data/storage/av/ 下 storageAvDir := filepath.Join(unzipRootPath, "storage", "av") if gulu.File.IsExist(storageAvDir) { + // 将数据库文件中的块 ID 替换为新的块 ID + filepath.Walk(storageAvDir, func(path string, info fs.FileInfo, err error) error { + if !strings.HasSuffix(path, ".json") || !ast.IsNodeIDPattern(strings.TrimSuffix(info.Name(), ".json")) { + return nil + } + + data, readErr := os.ReadFile(path) + if nil != readErr { + logging.LogErrorf("read av file [%s] failed: %s", path, readErr) + return nil + } + var newData []byte + newData = data + for oldID, newID := range avBlockIDs { + newData = bytes.ReplaceAll(newData, []byte(oldID), []byte(newID)) + } + if !bytes.Equal(data, newData) { + if writeErr := os.WriteFile(path, newData, 0644); nil != writeErr { + logging.LogErrorf("write av file [%s] failed: %s", path, writeErr) + return nil + } + } + return nil + }) + targetStorageAvDir := filepath.Join(util.DataDir, "storage", "av") if copyErr := filelock.Copy(storageAvDir, targetStorageAvDir); nil != copyErr { logging.LogErrorf("copy storage av dir from [%s] to [%s] failed: %s", storageAvDir, targetStorageAvDir, copyErr) } + if removeErr := os.RemoveAll(storageAvDir); nil != removeErr { + logging.LogErrorf("remove temp storage av dir failed: %s", removeErr) + } } // 写回 .sy