From b49f477f5944cf789201b8ee327884cb3e56fa6e Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Sat, 24 Sep 2022 09:34:32 +0800 Subject: [PATCH 1/2] =?UTF-8?q?:art:=20=E5=AF=BC=E5=87=BA=E5=AF=BC?= =?UTF-8?q?=E5=85=A5=20.sy.zip=20=E4=BF=9D=E6=8C=81=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E6=8E=92=E5=BA=8F=20https://github.com/siyuan-note/si?= =?UTF-8?q?yuan/issues/5939?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/model/export.go | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/kernel/model/export.go b/kernel/model/export.go index 90e621bbf..afd68972a 100644 --- a/kernel/model/export.go +++ b/kernel/model/export.go @@ -896,6 +896,49 @@ func exportSYZip(boxID, rootDirPath, baseFolderName string, docPaths []string) ( } } + // 导出自定义排序 + sortPath := filepath.Join(util.DataDir, box.ID, ".siyuan", "sort.json") + fullSortIDs := map[string]int{} + sortIDs := map[string]int{} + var sortData []byte + var sortErr error + if gulu.File.IsExist(sortPath) { + sortData, sortErr = filelock.NoLockFileRead(sortPath) + if nil != sortErr { + logging.LogErrorf("read sort conf failed: %s", sortErr) + } + + if sortErr = gulu.JSON.UnmarshalJSON(sortData, &fullSortIDs); nil != sortErr { + logging.LogErrorf("unmarshal sort conf failed: %s", sortErr) + } + + if 0 < len(fullSortIDs) { + for _, tree := range trees { + if v, ok := fullSortIDs[tree.ID]; ok { + sortIDs[tree.ID] = v + } + } + } + + if 0 < len(sortIDs) { + sortData, sortErr = gulu.JSON.MarshalJSON(sortIDs) + if nil != sortErr { + logging.LogErrorf("marshal sort conf failed: %s", sortErr) + } + if 0 < len(sortData) { + confDir := filepath.Join(exportFolder, ".siyuan") + if mkdirErr := os.MkdirAll(confDir, 0755); nil != mkdirErr { + logging.LogErrorf("create export conf folder [%s] failed: %s", confDir, mkdirErr) + } else { + sortPath = filepath.Join(confDir, "sort.json") + if writeErr := os.WriteFile(sortPath, sortData, 0644); nil != writeErr { + logging.LogErrorf("write sort conf failed: %s", writeErr) + } + } + } + } + } + zipPath = exportFolder + ".sy.zip" zip, err := gulu.Zip.Create(zipPath) if nil != err { From 11aa06038ba9d3f13d25017362585eaa20bac9dc Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Sat, 24 Sep 2022 09:56:27 +0800 Subject: [PATCH 2/2] =?UTF-8?q?:art:=20=E5=AF=BC=E5=87=BA=E5=AF=BC?= =?UTF-8?q?=E5=85=A5=20.sy.zip=20=E4=BF=9D=E6=8C=81=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E6=8E=92=E5=BA=8F=20Fix=20https://github.com/siyuan-n?= =?UTF-8?q?ote/siyuan/issues/5939?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/model/import.go | 45 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/kernel/model/import.go b/kernel/model/import.go index 2513fad74..6fd686e44 100644 --- a/kernel/model/import.go +++ b/kernel/model/import.go @@ -174,6 +174,51 @@ func ImportSY(zipPath, boxID, toPath string) (err error) { } } + // 合并 sort.json + fullSortIDs := map[string]int{} + sortIDs := map[string]int{} + var sortData []byte + var sortErr error + sortPath := filepath.Join(unzipRootPath, ".siyuan", "sort.json") + if gulu.File.IsExist(sortPath) { + sortData, sortErr = filelock.NoLockFileRead(sortPath) + if nil != sortErr { + logging.LogErrorf("read import sort conf failed: %s", sortErr) + } + + if sortErr = gulu.JSON.UnmarshalJSON(sortData, &sortIDs); nil != sortErr { + logging.LogErrorf("unmarshal sort conf failed: %s", sortErr) + } + + sortPath = filepath.Join(util.DataDir, boxID, ".siyuan", "sort.json") + if gulu.File.IsExist(sortPath) { + sortData, sortErr = filelock.NoLockFileRead(sortPath) + if nil != sortErr { + logging.LogErrorf("read box sort conf failed: %s", sortErr) + } + + if sortErr = gulu.JSON.UnmarshalJSON(sortData, &fullSortIDs); nil != sortErr { + logging.LogErrorf("unmarshal box sort conf failed: %s", sortErr) + } + } + + for oldID, sort := range sortIDs { + if newID := blockIDs[oldID]; "" != newID { + fullSortIDs[newID] = sort + } + } + + sortData, sortErr = gulu.JSON.MarshalJSON(fullSortIDs) + if nil != sortErr { + logging.LogErrorf("marshal box sort conf failed: %s", sortErr) + } else { + sortErr = filelock.NoLockFileWrite(sortPath, sortData) + if nil != sortErr { + logging.LogErrorf("write box sort conf failed: %s", sortErr) + } + } + } + // 重命名文件路径 renamePaths := map[string]string{} filepath.Walk(unzipRootPath, func(path string, info fs.FileInfo, err error) error {