repo.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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/gin-gonic/gin"
  22. "github.com/siyuan-note/siyuan/kernel/model"
  23. "github.com/siyuan-note/siyuan/kernel/util"
  24. )
  25. func openRepoSnapshotDoc(c *gin.Context) {
  26. ret := gulu.Ret.NewResult()
  27. defer c.JSON(http.StatusOK, ret)
  28. arg, ok := util.JsonArg(c, ret)
  29. if !ok {
  30. return
  31. }
  32. id := arg["id"].(string)
  33. id, rootID, content, isLargeDoc, err := model.OpenRepoSnapshotDoc(id)
  34. if nil != err {
  35. ret.Code = -1
  36. ret.Msg = err.Error()
  37. return
  38. }
  39. ret.Data = map[string]interface{}{
  40. "id": id,
  41. "rootID": rootID,
  42. "content": content,
  43. "isLargeDoc": isLargeDoc,
  44. }
  45. }
  46. func diffRepoSnapshots(c *gin.Context) {
  47. ret := gulu.Ret.NewResult()
  48. defer c.JSON(http.StatusOK, ret)
  49. arg, ok := util.JsonArg(c, ret)
  50. if !ok {
  51. return
  52. }
  53. left := arg["left"].(string)
  54. right := arg["right"].(string)
  55. diff, err := model.DiffRepoSnapshots(left, right)
  56. if nil != err {
  57. ret.Code = -1
  58. ret.Msg = err.Error()
  59. return
  60. }
  61. ret.Data = map[string]interface{}{
  62. "diff": diff,
  63. }
  64. }
  65. func getCloudSpace(c *gin.Context) {
  66. ret := gulu.Ret.NewResult()
  67. defer c.JSON(http.StatusOK, ret)
  68. sync, backup, size, assetSize, totalSize, hTrafficUploadSize, hTrafficDownloadSize, err := model.GetCloudSpace()
  69. if nil != err {
  70. ret.Code = 1
  71. ret.Msg = err.Error()
  72. util.PushErrMsg(err.Error(), 3000)
  73. return
  74. }
  75. ret.Data = map[string]interface{}{
  76. "sync": sync,
  77. "backup": backup,
  78. "hAssetSize": assetSize,
  79. "hSize": size,
  80. "hTotalSize": totalSize,
  81. "hTrafficUploadSize": hTrafficUploadSize,
  82. "hTrafficDownloadSize": hTrafficDownloadSize,
  83. }
  84. }
  85. func checkoutRepo(c *gin.Context) {
  86. ret := gulu.Ret.NewResult()
  87. defer c.JSON(http.StatusOK, ret)
  88. arg, ok := util.JsonArg(c, ret)
  89. if !ok {
  90. return
  91. }
  92. id := arg["id"].(string)
  93. if err := model.CheckoutRepo(id); nil != err {
  94. ret.Code = -1
  95. ret.Msg = model.Conf.Language(141)
  96. return
  97. }
  98. }
  99. func downloadCloudSnapshot(c *gin.Context) {
  100. ret := gulu.Ret.NewResult()
  101. defer c.JSON(http.StatusOK, ret)
  102. arg, ok := util.JsonArg(c, ret)
  103. if !ok {
  104. return
  105. }
  106. id := arg["id"].(string)
  107. tag := arg["tag"].(string)
  108. if err := model.DownloadCloudSnapshot(tag, id); nil != err {
  109. ret.Code = -1
  110. ret.Msg = err.Error()
  111. return
  112. }
  113. }
  114. func uploadCloudSnapshot(c *gin.Context) {
  115. ret := gulu.Ret.NewResult()
  116. defer c.JSON(http.StatusOK, ret)
  117. arg, ok := util.JsonArg(c, ret)
  118. if !ok {
  119. return
  120. }
  121. id := arg["id"].(string)
  122. tag := arg["tag"].(string)
  123. if err := model.UploadCloudSnapshot(tag, id); nil != err {
  124. ret.Code = -1
  125. ret.Msg = err.Error()
  126. return
  127. }
  128. }
  129. func getRepoSnapshots(c *gin.Context) {
  130. ret := gulu.Ret.NewResult()
  131. defer c.JSON(http.StatusOK, ret)
  132. arg, ok := util.JsonArg(c, ret)
  133. if !ok {
  134. return
  135. }
  136. page := arg["page"].(float64)
  137. snapshots, pageCount, totalCount, err := model.GetRepoSnapshots(int(page))
  138. if nil != err {
  139. ret.Code = -1
  140. ret.Msg = err.Error()
  141. return
  142. }
  143. ret.Data = map[string]interface{}{
  144. "snapshots": snapshots,
  145. "pageCount": pageCount,
  146. "totalCount": totalCount,
  147. }
  148. }
  149. func getCloudRepoTagSnapshots(c *gin.Context) {
  150. ret := gulu.Ret.NewResult()
  151. defer c.JSON(http.StatusOK, ret)
  152. snapshots, err := model.GetCloudRepoTagSnapshots()
  153. if nil != err {
  154. ret.Code = -1
  155. ret.Msg = err.Error()
  156. return
  157. }
  158. ret.Data = map[string]interface{}{
  159. "snapshots": snapshots,
  160. }
  161. }
  162. func removeCloudRepoTagSnapshot(c *gin.Context) {
  163. ret := gulu.Ret.NewResult()
  164. defer c.JSON(http.StatusOK, ret)
  165. arg, ok := util.JsonArg(c, ret)
  166. if !ok {
  167. return
  168. }
  169. tag := arg["tag"].(string)
  170. err := model.RemoveCloudRepoTag(tag)
  171. if nil != err {
  172. ret.Code = -1
  173. ret.Msg = err.Error()
  174. return
  175. }
  176. }
  177. func getRepoTagSnapshots(c *gin.Context) {
  178. ret := gulu.Ret.NewResult()
  179. defer c.JSON(http.StatusOK, ret)
  180. snapshots, err := model.GetTagSnapshots()
  181. if nil != err {
  182. ret.Code = -1
  183. ret.Msg = err.Error()
  184. return
  185. }
  186. ret.Data = map[string]interface{}{
  187. "snapshots": snapshots,
  188. }
  189. }
  190. func removeRepoTagSnapshot(c *gin.Context) {
  191. ret := gulu.Ret.NewResult()
  192. defer c.JSON(http.StatusOK, ret)
  193. arg, ok := util.JsonArg(c, ret)
  194. if !ok {
  195. return
  196. }
  197. tag := arg["tag"].(string)
  198. err := model.RemoveTagSnapshot(tag)
  199. if nil != err {
  200. ret.Code = -1
  201. ret.Msg = err.Error()
  202. return
  203. }
  204. }
  205. func createSnapshot(c *gin.Context) {
  206. ret := gulu.Ret.NewResult()
  207. defer c.JSON(http.StatusOK, ret)
  208. arg, ok := util.JsonArg(c, ret)
  209. if !ok {
  210. return
  211. }
  212. memo := arg["memo"].(string)
  213. if err := model.IndexRepo(memo); nil != err {
  214. ret.Code = -1
  215. ret.Msg = fmt.Sprintf(model.Conf.Language(140), err)
  216. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  217. return
  218. }
  219. }
  220. func tagSnapshot(c *gin.Context) {
  221. ret := gulu.Ret.NewResult()
  222. defer c.JSON(http.StatusOK, ret)
  223. arg, ok := util.JsonArg(c, ret)
  224. if !ok {
  225. return
  226. }
  227. id := arg["id"].(string)
  228. name := arg["name"].(string)
  229. if err := model.TagSnapshot(id, name); nil != err {
  230. ret.Code = -1
  231. ret.Msg = fmt.Sprintf(model.Conf.Language(140), err)
  232. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  233. return
  234. }
  235. }
  236. func importRepoKey(c *gin.Context) {
  237. ret := gulu.Ret.NewResult()
  238. defer c.JSON(http.StatusOK, ret)
  239. arg, ok := util.JsonArg(c, ret)
  240. if !ok {
  241. return
  242. }
  243. base64Key := arg["key"].(string)
  244. if err := model.ImportRepoKey(base64Key); nil != err {
  245. ret.Code = -1
  246. ret.Msg = fmt.Sprintf(model.Conf.Language(137), err)
  247. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  248. return
  249. }
  250. }
  251. func initRepoKeyFromPassphrase(c *gin.Context) {
  252. ret := gulu.Ret.NewResult()
  253. defer c.JSON(http.StatusOK, ret)
  254. arg, ok := util.JsonArg(c, ret)
  255. if !ok {
  256. return
  257. }
  258. pass := arg["pass"].(string)
  259. if err := model.InitRepoKeyFromPassphrase(pass); nil != err {
  260. ret.Code = -1
  261. ret.Msg = fmt.Sprintf(model.Conf.Language(137), err)
  262. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  263. return
  264. }
  265. ret.Data = map[string]interface{}{
  266. "key": model.Conf.Repo.Key,
  267. }
  268. }
  269. func initRepoKey(c *gin.Context) {
  270. ret := gulu.Ret.NewResult()
  271. defer c.JSON(http.StatusOK, ret)
  272. if err := model.InitRepoKey(); nil != err {
  273. ret.Code = -1
  274. ret.Msg = fmt.Sprintf(model.Conf.Language(137), err)
  275. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  276. return
  277. }
  278. ret.Data = map[string]interface{}{
  279. "key": model.Conf.Repo.Key,
  280. }
  281. }
  282. func resetRepo(c *gin.Context) {
  283. ret := gulu.Ret.NewResult()
  284. defer c.JSON(http.StatusOK, ret)
  285. if err := model.ResetRepo(); nil != err {
  286. ret.Code = -1
  287. ret.Msg = fmt.Sprintf(model.Conf.Language(146), err.Error())
  288. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  289. return
  290. }
  291. }