🎨 Update av export

This commit is contained in:
Daniel 2023-09-25 23:22:34 +08:00
parent 707e9e03cc
commit 14eb394af3
No known key found for this signature in database
GPG key ID: 86211BA83DF03017

View file

@ -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