backup.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/dustin/go-humanize"
  21. "github.com/gin-gonic/gin"
  22. "github.com/siyuan-note/siyuan/kernel/model"
  23. "github.com/siyuan-note/siyuan/kernel/util"
  24. )
  25. func removeCloudBackup(c *gin.Context) {
  26. ret := gulu.Ret.NewResult()
  27. defer c.JSON(http.StatusOK, ret)
  28. err := model.RemoveCloudBackup()
  29. if nil != err {
  30. ret.Code = -1
  31. ret.Msg = err.Error()
  32. return
  33. }
  34. }
  35. func downloadCloudBackup(c *gin.Context) {
  36. ret := gulu.Ret.NewResult()
  37. defer c.JSON(http.StatusOK, ret)
  38. err := model.DownloadBackup()
  39. if nil != err {
  40. ret.Code = -1
  41. ret.Msg = err.Error()
  42. return
  43. }
  44. }
  45. func uploadLocalBackup(c *gin.Context) {
  46. ret := gulu.Ret.NewResult()
  47. defer c.JSON(http.StatusOK, ret)
  48. err := model.UploadBackup()
  49. if nil != err {
  50. ret.Code = -1
  51. ret.Msg = err.Error()
  52. return
  53. }
  54. }
  55. func recoverLocalBackup(c *gin.Context) {
  56. ret := gulu.Ret.NewResult()
  57. defer c.JSON(http.StatusOK, ret)
  58. err := model.RecoverLocalBackup()
  59. if nil != err {
  60. ret.Code = -1
  61. ret.Msg = err.Error()
  62. return
  63. }
  64. }
  65. func createLocalBackup(c *gin.Context) {
  66. ret := gulu.Ret.NewResult()
  67. defer c.JSON(http.StatusOK, ret)
  68. err := model.CreateLocalBackup()
  69. if nil != err {
  70. ret.Code = -1
  71. ret.Msg = err.Error()
  72. return
  73. }
  74. }
  75. func getLocalBackup(c *gin.Context) {
  76. ret := gulu.Ret.NewResult()
  77. defer c.JSON(http.StatusOK, ret)
  78. backup, err := model.GetLocalBackup()
  79. if nil != err {
  80. ret.Code = -1
  81. ret.Msg = err.Error()
  82. return
  83. }
  84. ret.Data = map[string]interface{}{
  85. "backup": backup,
  86. }
  87. }
  88. func getCloudSpace(c *gin.Context) {
  89. ret := gulu.Ret.NewResult()
  90. defer c.JSON(http.StatusOK, ret)
  91. sync, backup, size, assetSize, totalSize, err := model.GetCloudSpace()
  92. if nil != err {
  93. ret.Code = 1
  94. ret.Msg = err.Error()
  95. util.PushErrMsg(err.Error(), 3000)
  96. return
  97. }
  98. hTrafficUploadSize := humanize.Bytes(uint64(model.Conf.User.UserTrafficUpload))
  99. hTrafficDownloadSize := humanize.Bytes(uint64(model.Conf.User.UserTrafficDownload))
  100. ret.Data = map[string]interface{}{
  101. "sync": sync,
  102. "backup": backup,
  103. "hAssetSize": assetSize,
  104. "hSize": size,
  105. "hTotalSize": totalSize,
  106. "hTrafficUploadSize": hTrafficUploadSize,
  107. "hTrafficDownloadSize": hTrafficDownloadSize,
  108. }
  109. }