Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: 支持多设备操作不同文档后云端同步合并 https://github.com/siyuan-note/siyuan/issues/5092

Liang Dingce04cfcc7aa7
diff --git a/kernel/model/osssync.go b/kernel/model/osssync.go
index 43a4caac17..fc89e2558e 100644
--- a/kernel/model/osssync.go
+++ b/kernel/model/osssync.go
@@ -178,7 +178,8 @@ func ossDownload(isBackup bool, localDirPath, cloudDirPath string, bootOrExit bo
if nil != err {
return
}
- tmpIndex := filepath.Join(util.TempDir, "sync", "index.json")
+ tmpSyncDir := filepath.Join(util.TempDir, "sync")
+ tmpIndex := filepath.Join(tmpSyncDir, "index.json")
if err = os.WriteFile(tmpIndex, data, 0644); nil != err {
return
}
@@ -186,60 +187,58 @@ func ossDownload(isBackup bool, localDirPath, cloudDirPath string, bootOrExit bo
if !isBackup && (0 < len(removeList) || 0 < len(upsertList)) {
// 上传合并本地变更
- //var removed, upserted bool
- //var removes []string
- //for remove, _ := range removeList {
- // removes = append(removes, remove)
- //}
- //removeErr := ossRemove0(cloudDirPath, removes)
- //if nil != removeErr {
- // util.LogErrorf("remove merge cloud file failed: %s", removeErr)
- //} else {
- // for remove, _ := range removeList {
- // delete(cloudFileList, remove)
- // }
- // removed = 0 < len(removeList)
- //}
- //
- //var tmpWroteFiles int
- //var tmpTransferSize uint64
- //for upsert, _ := range upsertList {
- // localUpsert := filepath.Join(localDirPath, upsert)
- // info, statErr := os.Stat(localUpsert)
- // if nil != statErr {
- // util.LogErrorf("stat file [%s] failed: %s", localUpsert, statErr)
- // upserted = false
- // break
- // }
- //
- // if uploadErr := ossUpload0(localDirPath, cloudDirPath, localUpsert, &tmpWroteFiles, &tmpTransferSize); nil != uploadErr {
- // util.LogErrorf("upload merge cloud file [%s] failed: %s", upsert, uploadErr)
- // upserted = false
- // break
- // }
- // cloudFileList[upsert] = &CloudIndex{
- // Size: info.Size(),
- // Updated: info.ModTime().Unix(),
- // }
- // upserted = true
- //}
- //
- //if removed || upserted {
- // data, marshalErr := gulu.JSON.MarshalJSON(cloudFileList)
- // if nil != marshalErr {
- // util.LogErrorf("marshal cloud file list failed: %s", marshalErr)
- // } else {
- // tmpMergeDir := filepath.Join(util.TempDir, "sync")
- // tmpIndex := filepath.Join(tmpMergeDir, "index.json")
- // if writeErr := os.WriteFile(tmpIndex, data, 0644); nil != writeErr {
- // util.LogErrorf("write cloud file list failed: %s", writeErr)
- // } else {
- // if uploadErr := ossUpload0(tmpMergeDir, cloudDirPath, tmpIndex, &tmpWroteFiles, &tmpTransferSize); nil != uploadErr {
- // util.LogErrorf("upload merge cloud file [%s] failed: %s", tmpIndex, uploadErr)
- // }
- // }
- // }
- //}
+ var removed, upserted bool
+ var removes []string
+ for remove, _ := range removeList {
+ removes = append(removes, remove)
+ }
+ err = ossRemove0(cloudDirPath, removes)
+ if nil != err {
+ util.LogErrorf("remove merge cloud file failed: %s", err)
+ return
+ }
+ for remove, _ := range removeList {
+ delete(cloudFileList, remove)
+ }
+ removed = 0 < len(removeList)
+
+ var tmpWroteFiles int
+ var tmpTransferSize uint64
+ for upsert, _ := range upsertList {
+ localUpsert := filepath.Join(localDirPath, upsert)
+ var info os.FileInfo
+ info, err = os.Stat(localUpsert)
+ if nil != err {
+ util.LogErrorf("stat file [%s] failed: %s", localUpsert, err)
+ return
+ }
+
+ if err = ossUpload0(localDirPath, cloudDirPath, localUpsert, &tmpWroteFiles, &tmpTransferSize); nil != err {
+ util.LogErrorf("upload merge cloud file [%s] failed: %s", upsert, err)
+ return
+ }
+ cloudFileList[upsert] = &CloudIndex{
+ Size: info.Size(),
+ Updated: info.ModTime().Unix(),
+ }
+ upserted = true
+ }
+
+ if removed || upserted {
+ data, err = gulu.JSON.MarshalJSON(cloudFileList)
+ if nil != err {
+ util.LogErrorf("marshal cloud file list failed: %s", err)
+ return
+ }
+ if err = os.WriteFile(tmpIndex, data, 0644); nil != err {
+ util.LogErrorf("write cloud file list failed: %s", err)
+ return
+ }
+ if err = ossUpload0(tmpSyncDir, cloudDirPath, tmpIndex, &tmpWroteFiles, &tmpTransferSize); nil != err {
+ util.LogErrorf("upload merge cloud file [%s] failed: %s", tmpIndex, err)
+ return
+ }
+ }
}
localRemoves, cloudFetches, err := localUpsertRemoveListOSS(localDirPath, cloudFileList)
@@ -448,33 +447,34 @@ func ossUpload(isBackup bool, localDirPath, cloudDirPath, cloudDevice string, bo
err = ossDownload0(util.TempDir+"/sync", "sync/"+Conf.Sync.CloudName, "/"+pathJSON, &tmpFetchedFiles, &tmpTransferSize, false)
if nil != err {
util.LogErrorf("download merge cloud file failed: %s", err)
+ return
}
metaPath := filepath.Join(util.TempDir, "/sync/"+pathJSON)
- mergeErr := syncDirUpsertWorkspaceData(metaPath, indexPath, downloadList)
- if nil != mergeErr {
- util.LogErrorf("download merge cloud file failed: %s", mergeErr)
- } else {
- // 增量索引
- for upsertFile, _ := range downloadList {
- if !strings.HasSuffix(upsertFile, ".sy") {
- continue
- }
-
- upsertFile = filepath.ToSlash(upsertFile)
- box := upsertFile[:strings.Index(upsertFile, "/")]
- p := strings.TrimPrefix(upsertFile, box)
- tree, err0 := LoadTree(box, p)
- if nil != err0 {
- continue
- }
- treenode.ReindexBlockTree(tree)
- sql.UpsertTreeQueue(tree)
+ err = syncDirUpsertWorkspaceData(metaPath, indexPath, downloadList)
+ if nil != err {
+ util.LogErrorf("download merge cloud file failed: %s", err)
+ return
+ }
+ // 增量索引
+ for upsertFile, _ := range downloadList {
+ if !strings.HasSuffix(upsertFile, ".sy") {
+ continue
+ }
+
+ upsertFile = filepath.ToSlash(upsertFile)
+ box := upsertFile[:strings.Index(upsertFile, "/")]
+ p := strings.TrimPrefix(upsertFile, box)
+ tree, err0 := LoadTree(box, p)
+ if nil != err0 {
+ continue
}
+ treenode.ReindexBlockTree(tree)
+ sql.UpsertTreeQueue(tree)
}
// 重新生成云端索引
- if _, idxErr := genCloudIndex(localDirPath, excludes); nil != idxErr {
+ if _, err = genCloudIndex(localDirPath, excludes); nil != err {
return
}
}