sync.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // SiYuan - Build Your Eternal Digital Garden
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package api
  17. import (
  18. "net/http"
  19. "github.com/88250/gulu"
  20. "github.com/gin-gonic/gin"
  21. "github.com/siyuan-note/siyuan/kernel/model"
  22. "github.com/siyuan-note/siyuan/kernel/util"
  23. )
  24. func getSyncDirection(c *gin.Context) {
  25. ret := gulu.Ret.NewResult()
  26. defer c.JSON(http.StatusOK, ret)
  27. arg, ok := util.JsonArg(c, ret)
  28. if !ok {
  29. return
  30. }
  31. cloudDirName := arg["name"].(string)
  32. ret.Code, ret.Msg = model.GetSyncDirection(cloudDirName)
  33. }
  34. func getBootSync(c *gin.Context) {
  35. ret := gulu.Ret.NewResult()
  36. defer c.JSON(http.StatusOK, ret)
  37. if 1 == model.BootSyncSucc {
  38. ret.Code = 1
  39. ret.Msg = model.Conf.Language(17)
  40. return
  41. }
  42. }
  43. func performSync(c *gin.Context) {
  44. ret := gulu.Ret.NewResult()
  45. defer c.JSON(http.StatusOK, ret)
  46. model.SyncData(false, false, true)
  47. }
  48. func performBootSync(c *gin.Context) {
  49. ret := gulu.Ret.NewResult()
  50. defer c.JSON(http.StatusOK, ret)
  51. model.SyncData(true, false, true)
  52. ret.Code = model.BootSyncSucc
  53. }
  54. func listCloudSyncDir(c *gin.Context) {
  55. ret := gulu.Ret.NewResult()
  56. defer c.JSON(http.StatusOK, ret)
  57. syncDirs, hSize, err := model.ListCloudSyncDir()
  58. if nil != err {
  59. ret.Code = 1
  60. ret.Msg = err.Error()
  61. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  62. return
  63. }
  64. ret.Data = map[string]interface{}{
  65. "syncDirs": syncDirs,
  66. "hSize": hSize,
  67. "checkedSyncDir": model.Conf.Sync.CloudName,
  68. }
  69. }
  70. func removeCloudSyncDir(c *gin.Context) {
  71. ret := gulu.Ret.NewResult()
  72. defer c.JSON(http.StatusOK, ret)
  73. arg, ok := util.JsonArg(c, ret)
  74. if !ok {
  75. return
  76. }
  77. name := arg["name"].(string)
  78. err := model.RemoveCloudSyncDir(name)
  79. if nil != err {
  80. ret.Code = -1
  81. ret.Msg = err.Error()
  82. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  83. return
  84. }
  85. ret.Data = model.Conf.Sync.CloudName
  86. }
  87. func createCloudSyncDir(c *gin.Context) {
  88. ret := gulu.Ret.NewResult()
  89. defer c.JSON(http.StatusOK, ret)
  90. arg, ok := util.JsonArg(c, ret)
  91. if !ok {
  92. return
  93. }
  94. name := arg["name"].(string)
  95. err := model.CreateCloudSyncDir(name)
  96. if nil != err {
  97. ret.Code = -1
  98. ret.Msg = err.Error()
  99. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  100. return
  101. }
  102. }
  103. func setSyncUseDataRepo(c *gin.Context) {
  104. ret := gulu.Ret.NewResult()
  105. defer c.JSON(http.StatusOK, ret)
  106. arg, ok := util.JsonArg(c, ret)
  107. if !ok {
  108. return
  109. }
  110. enabled := arg["enabled"].(bool)
  111. err := model.SetSyncUseDataRepo(enabled)
  112. if nil != err {
  113. ret.Code = 1
  114. ret.Msg = err.Error()
  115. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  116. return
  117. }
  118. }
  119. func setSyncEnable(c *gin.Context) {
  120. ret := gulu.Ret.NewResult()
  121. defer c.JSON(http.StatusOK, ret)
  122. arg, ok := util.JsonArg(c, ret)
  123. if !ok {
  124. return
  125. }
  126. enabled := arg["enabled"].(bool)
  127. err := model.SetSyncEnable(enabled)
  128. if nil != err {
  129. ret.Code = 1
  130. ret.Msg = err.Error()
  131. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  132. return
  133. }
  134. }
  135. func setSyncMode(c *gin.Context) {
  136. ret := gulu.Ret.NewResult()
  137. defer c.JSON(http.StatusOK, ret)
  138. arg, ok := util.JsonArg(c, ret)
  139. if !ok {
  140. return
  141. }
  142. mode := int(arg["mode"].(float64))
  143. err := model.SetSyncMode(mode)
  144. if nil != err {
  145. ret.Code = 1
  146. ret.Msg = err.Error()
  147. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  148. return
  149. }
  150. }
  151. func setCloudSyncDir(c *gin.Context) {
  152. ret := gulu.Ret.NewResult()
  153. defer c.JSON(http.StatusOK, ret)
  154. arg, ok := util.JsonArg(c, ret)
  155. if !ok {
  156. return
  157. }
  158. name := arg["name"].(string)
  159. model.SetCloudSyncDir(name)
  160. }