repo.go 2.9 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. "encoding/hex"
  19. "fmt"
  20. "net/http"
  21. "github.com/88250/gulu"
  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 checkoutRepo(c *gin.Context) {
  27. ret := gulu.Ret.NewResult()
  28. defer c.JSON(http.StatusOK, ret)
  29. arg, ok := util.JsonArg(c, ret)
  30. if !ok {
  31. return
  32. }
  33. id := arg["id"].(string)
  34. if err := model.CheckoutRepo(id); nil != err {
  35. ret.Code = -1
  36. ret.Msg = model.Conf.Language(141)
  37. return
  38. }
  39. }
  40. func getRepoIndexLogs(c *gin.Context) {
  41. ret := gulu.Ret.NewResult()
  42. defer c.JSON(http.StatusOK, ret)
  43. arg, ok := util.JsonArg(c, ret)
  44. if !ok {
  45. return
  46. }
  47. page := arg["page"].(float64)
  48. logs, pageCount, totalCount, err := model.GetRepoIndexLogs(int(page))
  49. if nil != err {
  50. ret.Code = -1
  51. ret.Msg = err.Error()
  52. return
  53. }
  54. ret.Data = map[string]interface{}{
  55. "logs": logs,
  56. "pageCount": pageCount,
  57. "totalCount": totalCount,
  58. }
  59. }
  60. func indexRepo(c *gin.Context) {
  61. ret := gulu.Ret.NewResult()
  62. defer c.JSON(http.StatusOK, ret)
  63. arg, ok := util.JsonArg(c, ret)
  64. if !ok {
  65. return
  66. }
  67. message := arg["message"].(string)
  68. if err := model.IndexRepo(message); nil != err {
  69. ret.Code = -1
  70. ret.Msg = fmt.Sprintf(model.Conf.Language(140), err)
  71. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  72. return
  73. }
  74. }
  75. func importRepoKey(c *gin.Context) {
  76. ret := gulu.Ret.NewResult()
  77. defer c.JSON(http.StatusOK, ret)
  78. arg, ok := util.JsonArg(c, ret)
  79. if !ok {
  80. return
  81. }
  82. hexKey := arg["key"].(string)
  83. if err := model.ImportRepoKey(hexKey); nil != err {
  84. ret.Code = -1
  85. ret.Msg = model.Conf.Language(137)
  86. return
  87. }
  88. }
  89. func initRepoKey(c *gin.Context) {
  90. ret := gulu.Ret.NewResult()
  91. defer c.JSON(http.StatusOK, ret)
  92. if err := model.InitRepoKey(); nil != err {
  93. ret.Code = -1
  94. ret.Msg = model.Conf.Language(137)
  95. return
  96. }
  97. ret.Data = map[string]interface{}{
  98. "key": hex.EncodeToString(model.Conf.Repo.Key),
  99. }
  100. }
  101. func resetRepo(c *gin.Context) {
  102. ret := gulu.Ret.NewResult()
  103. defer c.JSON(http.StatusOK, ret)
  104. if err := model.ResetRepo(); nil != err {
  105. ret.Code = -1
  106. ret.Msg = fmt.Sprintf(model.Conf.Language(146), err.Error())
  107. return
  108. }
  109. }