mirror of
https://github.com/siyuan-note/siyuan.git
synced 2026-01-22 08:16:10 +01:00
✨ 数据仓库支持云端备份 https://github.com/siyuan-note/siyuan/issues/5336
This commit is contained in:
parent
ebdb5b2a36
commit
e41d411c2d
10 changed files with 108 additions and 38 deletions
|
|
@ -66,7 +66,7 @@ func getRepoIndexLogs(c *gin.Context) {
|
|||
}
|
||||
}
|
||||
|
||||
func indexRepo(c *gin.Context) {
|
||||
func createSnapshot(c *gin.Context) {
|
||||
ret := gulu.Ret.NewResult()
|
||||
defer c.JSON(http.StatusOK, ret)
|
||||
|
||||
|
|
@ -75,8 +75,8 @@ func indexRepo(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
message := arg["message"].(string)
|
||||
if err := model.IndexRepo(message); nil != err {
|
||||
name := arg["name"].(string)
|
||||
if err := model.CreateSnapshot(name); nil != err {
|
||||
ret.Code = -1
|
||||
ret.Msg = fmt.Sprintf(model.Conf.Language(140), err)
|
||||
ret.Data = map[string]interface{}{"closeTimeout": 5000}
|
||||
|
|
|
|||
|
|
@ -254,7 +254,7 @@ func ServeAPI(ginServer *gin.Engine) {
|
|||
ginServer.Handle("POST", "/api/repo/initRepoKey", model.CheckAuth, initRepoKey)
|
||||
ginServer.Handle("POST", "/api/repo/resetRepo", model.CheckAuth, resetRepo)
|
||||
ginServer.Handle("POST", "/api/repo/importRepoKey", model.CheckAuth, importRepoKey)
|
||||
ginServer.Handle("POST", "/api/repo/indexRepo", model.CheckAuth, indexRepo)
|
||||
ginServer.Handle("POST", "/api/repo/createSnapshot", model.CheckAuth, createSnapshot)
|
||||
ginServer.Handle("POST", "/api/repo/checkoutRepo", model.CheckAuth, checkoutRepo)
|
||||
ginServer.Handle("POST", "/api/repo/getRepoIndexLogs", model.CheckAuth, getRepoIndexLogs)
|
||||
|
||||
|
|
|
|||
|
|
@ -85,7 +85,7 @@ func ImportRepoKey(base64Key string) (err error) {
|
|||
time.Sleep(1 * time.Second)
|
||||
util.PushUpdateMsg(msgId, Conf.Language(138), 3000)
|
||||
time.Sleep(1 * time.Second)
|
||||
if initErr := IndexRepo("Init data repo"); nil != initErr {
|
||||
if initErr := indexRepo("[Auto] Init data repo"); nil != initErr {
|
||||
util.PushUpdateMsg(msgId, fmt.Sprintf(Conf.Language(140), initErr), 7000)
|
||||
}
|
||||
return
|
||||
|
|
@ -145,7 +145,7 @@ func InitRepoKey() (err error) {
|
|||
time.Sleep(1 * time.Second)
|
||||
util.PushUpdateMsg(msgId, Conf.Language(138), 3000)
|
||||
time.Sleep(1 * time.Second)
|
||||
if initErr := IndexRepo("Init data repo"); nil != initErr {
|
||||
if initErr := indexRepo("[Auto] Init data repo"); nil != initErr {
|
||||
util.PushUpdateMsg(msgId, fmt.Sprintf(Conf.Language(140), initErr), 7000)
|
||||
}
|
||||
return
|
||||
|
|
@ -194,7 +194,59 @@ func CheckoutRepo(id string) (err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func IndexRepo(memo string) (err error) {
|
||||
func CreateSnapshot(name string) (err error) {
|
||||
if 1 > len(Conf.Repo.Key) {
|
||||
err = errors.New(Conf.Language(26))
|
||||
return
|
||||
}
|
||||
|
||||
name = gulu.Str.RemoveInvisible(name)
|
||||
if "" == name {
|
||||
err = errors.New(Conf.Language(142))
|
||||
return
|
||||
}
|
||||
|
||||
if !gulu.File.IsValidFilename(name) {
|
||||
err = errors.New(Conf.Language(151))
|
||||
return
|
||||
}
|
||||
|
||||
repo, err := newRepository()
|
||||
if nil != err {
|
||||
return
|
||||
}
|
||||
|
||||
util.PushEndlessProgress(Conf.Language(143))
|
||||
writingDataLock.Lock()
|
||||
defer writingDataLock.Unlock()
|
||||
|
||||
start := time.Now()
|
||||
latest, _ := repo.Latest()
|
||||
WaitForWritingFiles()
|
||||
filelock.ReleaseAllFileLocks()
|
||||
index, err := repo.Index("[Snapshot] "+name, map[string]interface{}{
|
||||
CtxPushMsg: CtxPushMsgToStatusBarAndProgress,
|
||||
})
|
||||
if nil != err {
|
||||
util.PushStatusBar("Create data snapshot failed")
|
||||
return
|
||||
}
|
||||
elapsed := time.Since(start)
|
||||
|
||||
if nil == latest || latest.ID != index.ID {
|
||||
msg := fmt.Sprintf(Conf.Language(147), elapsed.Seconds())
|
||||
util.PushStatusBar(msg)
|
||||
util.PushMsg(msg, 5000)
|
||||
} else {
|
||||
msg := fmt.Sprintf(Conf.Language(148), elapsed.Seconds())
|
||||
util.PushStatusBar(msg)
|
||||
util.PushMsg(msg, 5000)
|
||||
}
|
||||
util.PushClearProgress()
|
||||
return
|
||||
}
|
||||
|
||||
func indexRepo(memo string) (err error) {
|
||||
if 1 > len(Conf.Repo.Key) {
|
||||
err = errors.New(Conf.Language(26))
|
||||
return
|
||||
|
|
@ -223,7 +275,7 @@ func IndexRepo(memo string) (err error) {
|
|||
CtxPushMsg: CtxPushMsgToStatusBarAndProgress,
|
||||
})
|
||||
if nil != err {
|
||||
util.PushStatusBar("Create data snapshot failed")
|
||||
util.PushStatusBar("Index data repo failed: " + err.Error())
|
||||
return
|
||||
}
|
||||
elapsed := time.Since(start)
|
||||
|
|
@ -233,7 +285,7 @@ func IndexRepo(memo string) (err error) {
|
|||
util.PushStatusBar(msg)
|
||||
util.PushMsg(msg, 5000)
|
||||
} else {
|
||||
msg := Conf.Language(148)
|
||||
msg := fmt.Sprintf(Conf.Language(148), elapsed.Seconds())
|
||||
util.PushStatusBar(msg)
|
||||
util.PushMsg(msg, 5000)
|
||||
}
|
||||
|
|
@ -354,23 +406,20 @@ func indexRepoBeforeCloudSync(repo *dejavu.Repo) (err error) {
|
|||
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), elapsed.Seconds()))
|
||||
} else {
|
||||
util.PushStatusBar(Conf.Language(148))
|
||||
|
||||
if nil == latest || latest.ID != index.ID {
|
||||
// 对新创建的快照需要更新备注,加入耗时统计
|
||||
index.Memo = fmt.Sprintf("[Auto] Cloud sync, completed in %.2fs", elapsed.Seconds())
|
||||
if err = repo.PutIndex(index); 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
|
||||
}
|
||||
} else {
|
||||
util.PushStatusBar(fmt.Sprintf(Conf.Language(147), elapsed.Seconds()))
|
||||
} else {
|
||||
util.PushStatusBar(fmt.Sprintf(Conf.Language(148), elapsed.Seconds()))
|
||||
}
|
||||
|
||||
if 7000 < elapsed.Milliseconds() {
|
||||
util.LogWarnf("index data repo before cloud sync elapsed [%dms]", elapsed.Milliseconds())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/88250/gulu"
|
||||
"github.com/88250/lute/html"
|
||||
"github.com/88250/melody"
|
||||
)
|
||||
|
||||
|
|
@ -125,28 +126,33 @@ func ReloadUI() {
|
|||
}
|
||||
|
||||
func PushTxErr(msg string, code int, data interface{}) {
|
||||
msg = html.EscapeHTMLStr(msg)
|
||||
BroadcastByType("main", "txerr", code, msg, data)
|
||||
}
|
||||
|
||||
func PushUpdateMsg(msgId string, msg string, timeout int) {
|
||||
msg = html.EscapeHTMLStr(msg)
|
||||
BroadcastByType("main", "msg", 0, msg, map[string]interface{}{"id": msgId, "closeTimeout": timeout})
|
||||
return
|
||||
}
|
||||
|
||||
func PushMsg(msg string, timeout int) (msgId string) {
|
||||
msgId = gulu.Rand.String(7)
|
||||
msg = html.EscapeHTMLStr(msg)
|
||||
BroadcastByType("main", "msg", 0, msg, map[string]interface{}{"id": msgId, "closeTimeout": timeout})
|
||||
return
|
||||
}
|
||||
|
||||
func PushErrMsg(msg string, timeout int) (msgId string) {
|
||||
msgId = gulu.Rand.String(7)
|
||||
msg = html.EscapeHTMLStr(msg)
|
||||
BroadcastByType("main", "msg", -1, msg, map[string]interface{}{"id": msgId, "closeTimeout": timeout})
|
||||
return
|
||||
}
|
||||
|
||||
func PushStatusBar(msg string) {
|
||||
msg += " (" + time.Now().Format("2006-01-02 15:04:05") + ")"
|
||||
msg = html.EscapeHTMLStr(msg)
|
||||
BroadcastByType("main", "statusbar", 0, msg, nil)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue