repo.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. "fmt"
  19. "net/http"
  20. "github.com/88250/gulu"
  21. "github.com/dustin/go-humanize"
  22. "github.com/gin-gonic/gin"
  23. "github.com/siyuan-note/siyuan/kernel/model"
  24. "github.com/siyuan-note/siyuan/kernel/util"
  25. )
  26. func getCloudSpace(c *gin.Context) {
  27. ret := gulu.Ret.NewResult()
  28. defer c.JSON(http.StatusOK, ret)
  29. sync, backup, size, assetSize, totalSize, err := model.GetCloudSpace()
  30. if nil != err {
  31. ret.Code = 1
  32. ret.Msg = err.Error()
  33. util.PushErrMsg(err.Error(), 3000)
  34. return
  35. }
  36. hTrafficUploadSize := humanize.Bytes(uint64(model.Conf.User.UserTrafficUpload))
  37. hTrafficDownloadSize := humanize.Bytes(uint64(model.Conf.User.UserTrafficDownload))
  38. ret.Data = map[string]interface{}{
  39. "sync": sync,
  40. "backup": backup,
  41. "hAssetSize": assetSize,
  42. "hSize": size,
  43. "hTotalSize": totalSize,
  44. "hTrafficUploadSize": hTrafficUploadSize,
  45. "hTrafficDownloadSize": hTrafficDownloadSize,
  46. }
  47. }
  48. func checkoutRepo(c *gin.Context) {
  49. ret := gulu.Ret.NewResult()
  50. defer c.JSON(http.StatusOK, ret)
  51. arg, ok := util.JsonArg(c, ret)
  52. if !ok {
  53. return
  54. }
  55. id := arg["id"].(string)
  56. if err := model.CheckoutRepo(id); nil != err {
  57. ret.Code = -1
  58. ret.Msg = model.Conf.Language(141)
  59. return
  60. }
  61. }
  62. func downloadCloudSnapshot(c *gin.Context) {
  63. ret := gulu.Ret.NewResult()
  64. defer c.JSON(http.StatusOK, ret)
  65. arg, ok := util.JsonArg(c, ret)
  66. if !ok {
  67. return
  68. }
  69. id := arg["id"].(string)
  70. tag := arg["tag"].(string)
  71. if err := model.DownloadCloudSnapshot(tag, id); nil != err {
  72. ret.Code = -1
  73. ret.Msg = err.Error()
  74. return
  75. }
  76. }
  77. func uploadCloudSnapshot(c *gin.Context) {
  78. ret := gulu.Ret.NewResult()
  79. defer c.JSON(http.StatusOK, ret)
  80. arg, ok := util.JsonArg(c, ret)
  81. if !ok {
  82. return
  83. }
  84. id := arg["id"].(string)
  85. tag := arg["tag"].(string)
  86. if err := model.UploadCloudSnapshot(tag, id); nil != err {
  87. ret.Code = -1
  88. ret.Msg = err.Error()
  89. return
  90. }
  91. }
  92. func getRepoSnapshots(c *gin.Context) {
  93. ret := gulu.Ret.NewResult()
  94. defer c.JSON(http.StatusOK, ret)
  95. arg, ok := util.JsonArg(c, ret)
  96. if !ok {
  97. return
  98. }
  99. page := arg["page"].(float64)
  100. snapshots, pageCount, totalCount, err := model.GetRepoSnapshots(int(page))
  101. if nil != err {
  102. ret.Code = -1
  103. ret.Msg = err.Error()
  104. return
  105. }
  106. ret.Data = map[string]interface{}{
  107. "snapshots": snapshots,
  108. "pageCount": pageCount,
  109. "totalCount": totalCount,
  110. }
  111. }
  112. func getCloudRepoTagSnapshots(c *gin.Context) {
  113. ret := gulu.Ret.NewResult()
  114. defer c.JSON(http.StatusOK, ret)
  115. snapshots, err := model.GetCloudRepoTagSnapshots()
  116. if nil != err {
  117. ret.Code = -1
  118. ret.Msg = err.Error()
  119. return
  120. }
  121. ret.Data = map[string]interface{}{
  122. "snapshots": snapshots,
  123. }
  124. }
  125. func removeCloudRepoTagSnapshot(c *gin.Context) {
  126. ret := gulu.Ret.NewResult()
  127. defer c.JSON(http.StatusOK, ret)
  128. arg, ok := util.JsonArg(c, ret)
  129. if !ok {
  130. return
  131. }
  132. tag := arg["tag"].(string)
  133. err := model.RemoveCloudRepoTag(tag)
  134. if nil != err {
  135. ret.Code = -1
  136. ret.Msg = err.Error()
  137. return
  138. }
  139. }
  140. func getRepoTagSnapshots(c *gin.Context) {
  141. ret := gulu.Ret.NewResult()
  142. defer c.JSON(http.StatusOK, ret)
  143. snapshots, err := model.GetTagSnapshots()
  144. if nil != err {
  145. ret.Code = -1
  146. ret.Msg = err.Error()
  147. return
  148. }
  149. ret.Data = map[string]interface{}{
  150. "snapshots": snapshots,
  151. }
  152. }
  153. func removeRepoTagSnapshot(c *gin.Context) {
  154. ret := gulu.Ret.NewResult()
  155. defer c.JSON(http.StatusOK, ret)
  156. arg, ok := util.JsonArg(c, ret)
  157. if !ok {
  158. return
  159. }
  160. tag := arg["tag"].(string)
  161. err := model.RemoveTagSnapshot(tag)
  162. if nil != err {
  163. ret.Code = -1
  164. ret.Msg = err.Error()
  165. return
  166. }
  167. }
  168. func createSnapshot(c *gin.Context) {
  169. ret := gulu.Ret.NewResult()
  170. defer c.JSON(http.StatusOK, ret)
  171. arg, ok := util.JsonArg(c, ret)
  172. if !ok {
  173. return
  174. }
  175. memo := arg["memo"].(string)
  176. if err := model.IndexRepo(memo); nil != err {
  177. ret.Code = -1
  178. ret.Msg = fmt.Sprintf(model.Conf.Language(140), err)
  179. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  180. return
  181. }
  182. }
  183. func tagSnapshot(c *gin.Context) {
  184. ret := gulu.Ret.NewResult()
  185. defer c.JSON(http.StatusOK, ret)
  186. arg, ok := util.JsonArg(c, ret)
  187. if !ok {
  188. return
  189. }
  190. id := arg["id"].(string)
  191. name := arg["name"].(string)
  192. if err := model.TagSnapshot(id, name); nil != err {
  193. ret.Code = -1
  194. ret.Msg = fmt.Sprintf(model.Conf.Language(140), err)
  195. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  196. return
  197. }
  198. }
  199. func importRepoKey(c *gin.Context) {
  200. ret := gulu.Ret.NewResult()
  201. defer c.JSON(http.StatusOK, ret)
  202. arg, ok := util.JsonArg(c, ret)
  203. if !ok {
  204. return
  205. }
  206. base64Key := arg["key"].(string)
  207. if err := model.ImportRepoKey(base64Key); nil != err {
  208. ret.Code = -1
  209. ret.Msg = fmt.Sprintf(model.Conf.Language(137), err)
  210. return
  211. }
  212. }
  213. func initRepoKey(c *gin.Context) {
  214. ret := gulu.Ret.NewResult()
  215. defer c.JSON(http.StatusOK, ret)
  216. if err := model.InitRepoKey(); nil != err {
  217. ret.Code = -1
  218. ret.Msg = fmt.Sprintf(model.Conf.Language(137), err)
  219. return
  220. }
  221. ret.Data = map[string]interface{}{
  222. "key": model.Conf.Repo.Key,
  223. }
  224. }
  225. func resetRepo(c *gin.Context) {
  226. ret := gulu.Ret.NewResult()
  227. defer c.JSON(http.StatusOK, ret)
  228. if err := model.ResetRepo(); nil != err {
  229. ret.Code = -1
  230. ret.Msg = fmt.Sprintf(model.Conf.Language(146), err.Error())
  231. return
  232. }
  233. }