瀏覽代碼

:art: 云端同步数据仓库 https://github.com/siyuan-note/siyuan/issues/5142

Liang Ding 3 年之前
父節點
當前提交
5b1b38aabe
共有 3 個文件被更改,包括 57 次插入54 次删除
  1. 8 7
      kernel/conf/sync.go
  2. 44 41
      kernel/model/repository.go
  3. 5 6
      kernel/model/sync.go

+ 8 - 7
kernel/conf/sync.go

@@ -23,13 +23,14 @@ import (
 )
 
 type Sync struct {
-	CloudName  string `json:"cloudName"`  // 云端同步目录名称
-	Enabled    bool   `json:"enabled"`    // 是否开启同步
-	Mode       int    `json:"mode"`       // 同步模式,0:未设置(为兼容已有配置,initConf 函数中会转换为 1),1:自动,2:手动 https://github.com/siyuan-note/siyuan/issues/5089
-	Uploaded   int64  `json:"uploaded"`   // 最近上传时间
-	Downloaded int64  `json:"downloaded"` // 最近下载时间
-	Synced     int64  `json:"synced"`     // 最近同步时间
-	Stat       string `json:"stat"`       // 最近同步统计信息
+	CloudName   string `json:"cloudName"`   // 云端同步目录名称
+	Enabled     bool   `json:"enabled"`     // 是否开启同步
+	Mode        int    `json:"mode"`        // 同步模式,0:未设置(为兼容已有配置,initConf 函数中会转换为 1),1:自动,2:手动 https://github.com/siyuan-note/siyuan/issues/5089
+	Uploaded    int64  `json:"uploaded"`    // 最近上传时间
+	Downloaded  int64  `json:"downloaded"`  // 最近下载时间
+	Synced      int64  `json:"synced"`      // 最近同步时间
+	Stat        string `json:"stat"`        // 最近同步统计信息
+	UseDataRepo bool   `json:"useDataRepo"` // 是否使用数据仓库同步
 }
 
 func NewSync() *Sync {

+ 44 - 41
kernel/model/repository.go

@@ -249,57 +249,25 @@ const (
 	CtxPushMsgToStatusBarAndProgress
 )
 
-func indexRepoBeforeCloudSync() {
+func syncRepo(byHand bool) {
 	if 1 > len(Conf.Repo.Key) {
+		msg := Conf.Language(26)
+		util.PushStatusBar(msg)
+		util.PushErrMsg(msg, 0)
 		return
 	}
 
 	repo, err := newRepository()
 	if nil != err {
+		msg := fmt.Sprintf("sync repo failed: %s", err)
+		util.LogErrorf(msg)
+		util.PushStatusBar(msg)
+		util.PushErrMsg(msg, 0)
 		return
 	}
 
-	start := time.Now()
-	latest, _ := repo.Latest()
-	index, err := repo.Index("[Auto] Cloud sync", map[string]interface{}{
-		CtxPushMsg: CtxPushMsgToStatusBar,
-	})
-	if nil != err {
-		util.PushStatusBar("Create data snapshot for cloud sync failed")
-		util.LogErrorf("index data repo before cloud sync failed: %s", err)
-		return
-	}
-	elapsed := time.Since(start)
-	if nil != latest {
-		if latest.ID != index.ID {
-			// 对新创建的快照需要更新备注,加入耗时统计
-			index.Memo = fmt.Sprintf("[Auto] Cloud sync, completed in %.2fs", elapsed.Seconds())
-			err = repo.PutIndex(index)
-			if nil != err {
-				util.PushStatusBar("Save data snapshot for cloud sync failed")
-				util.LogErrorf("put index into data repo before cloud sync failed: %s", err)
-				return
-			}
-			util.PushStatusBar(fmt.Sprintf(Conf.Language(147)+" [%s]", elapsed.Seconds(), latest.ID[:7]))
-		} else {
-			util.PushStatusBar(Conf.Language(148) + " [" + latest.ID[:7] + "]")
-		}
-	} else {
-		util.PushStatusBar(fmt.Sprintf(Conf.Language(147)+" [%s]", elapsed.Seconds(), latest.ID[:7]))
-	}
-	if 7000 < elapsed.Milliseconds() {
-		util.LogWarnf("index data repo before cloud sync elapsed [%dms]", elapsed.Milliseconds())
-	}
-}
-
-func syncRepo(byHand bool) {
-	if 1 > len(Conf.Repo.Key) {
-		return
-	}
-
-	repo, err := newRepository()
+	err = indexRepoBeforeCloudSync(repo)
 	if nil != err {
-		util.LogErrorf("sync repo failed: %s", err)
 		return
 	}
 
@@ -368,6 +336,41 @@ func syncRepo(byHand bool) {
 	return
 }
 
+func indexRepoBeforeCloudSync(repo *dejavu.Repo) (err error) {
+	start := time.Now()
+	latest, _ := repo.Latest()
+	index, err := repo.Index("[Auto] Cloud sync", map[string]interface{}{
+		CtxPushMsg: CtxPushMsgToStatusBar,
+	})
+	if nil != err {
+		util.PushStatusBar(Conf.Language(140))
+		util.LogErrorf("index data repo before cloud sync failed: %s", err)
+		return
+	}
+	elapsed := time.Since(start)
+	if nil != latest {
+		if latest.ID != index.ID {
+			// 对新创建的快照需要更新备注,加入耗时统计
+			index.Memo = fmt.Sprintf("[Auto] Cloud sync, completed in %.2fs", elapsed.Seconds())
+			err = repo.PutIndex(index)
+			if nil != err {
+				util.PushStatusBar("Save data snapshot for cloud sync failed")
+				util.LogErrorf("put index into data repo before cloud sync failed: %s", err)
+				return
+			}
+			util.PushStatusBar(fmt.Sprintf(Conf.Language(147)+" [%s]", elapsed.Seconds(), latest.ID[:7]))
+		} else {
+			util.PushStatusBar(Conf.Language(148) + " [" + latest.ID[:7] + "]")
+		}
+	} else {
+		util.PushStatusBar(fmt.Sprintf(Conf.Language(147)+" [%s]", elapsed.Seconds(), latest.ID[:7]))
+	}
+	if 7000 < elapsed.Milliseconds() {
+		util.LogWarnf("index data repo before cloud sync elapsed [%dms]", elapsed.Milliseconds())
+	}
+	return
+}
+
 func newRepository() (ret *dejavu.Repo, err error) {
 	ignoreLines := getIgnoreLines()
 	ignoreLines = append(ignoreLines, "/.siyuan/conf.json") // 忽略旧版同步配置

+ 5 - 6
kernel/model/sync.go

@@ -131,12 +131,10 @@ func SyncData(boot, exit, byHand bool) {
 	syncLock.Lock()
 	defer syncLock.Unlock()
 
-	// 创建数据快照 https://github.com/siyuan-note/siyuan/issues/5161
-	indexRepoBeforeCloudSync()
-
-	//同步数据仓库 https://github.com/siyuan-note/siyuan/issues/5142
-	syncRepo(byHand)
-	return // TODO: 测试
+	if Conf.Sync.UseDataRepo {
+		syncRepo(byHand)
+		return
+	}
 
 	WaitForWritingFiles()
 	writingDataLock.Lock()
@@ -412,6 +410,7 @@ func SyncData(boot, exit, byHand bool) {
 	return
 }
 
+// TODO: 新版同步上线后移除
 // 清理 dir 下符合 ID 规则的空文件夹。
 // 因为是深度遍历,所以可能会清理不完全空文件夹,每次遍历仅能清理叶子节点。但是多次调用后,可以清理完全。
 func clearEmptyDirs(dir string) {