bazaar.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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/bazaar"
  22. "github.com/siyuan-note/siyuan/kernel/model"
  23. "github.com/siyuan-note/siyuan/kernel/util"
  24. )
  25. func getBazaarPackageREAME(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. repoURL := arg["repoURL"].(string)
  33. repoHash := arg["repoHash"].(string)
  34. ret.Data = map[string]interface{}{
  35. "html": model.GetPackageREADME(repoURL, repoHash),
  36. }
  37. }
  38. func getBazaarWidget(c *gin.Context) {
  39. ret := gulu.Ret.NewResult()
  40. defer c.JSON(http.StatusOK, ret)
  41. ret.Data = map[string]interface{}{
  42. "packages": model.BazaarWidgets(),
  43. }
  44. }
  45. func installBazaarWidget(c *gin.Context) {
  46. ret := gulu.Ret.NewResult()
  47. defer c.JSON(http.StatusOK, ret)
  48. arg, ok := util.JsonArg(c, ret)
  49. if !ok {
  50. return
  51. }
  52. repoURL := arg["repoURL"].(string)
  53. repoHash := arg["repoHash"].(string)
  54. packageName := arg["packageName"].(string)
  55. err := model.InstallBazaarWidget(repoURL, repoHash, packageName)
  56. if nil != err {
  57. ret.Code = 1
  58. ret.Msg = err.Error()
  59. return
  60. }
  61. util.PushMsg(model.Conf.Language(69), 3000)
  62. ret.Data = map[string]interface{}{
  63. "packages": model.BazaarWidgets(),
  64. }
  65. }
  66. func uninstallBazaarWidget(c *gin.Context) {
  67. ret := gulu.Ret.NewResult()
  68. defer c.JSON(http.StatusOK, ret)
  69. arg, ok := util.JsonArg(c, ret)
  70. if !ok {
  71. return
  72. }
  73. packageName := arg["packageName"].(string)
  74. err := model.UninstallBazaarWidget(packageName)
  75. if nil != err {
  76. ret.Code = -1
  77. ret.Msg = err.Error()
  78. return
  79. }
  80. ret.Data = map[string]interface{}{
  81. "packages": model.BazaarWidgets(),
  82. }
  83. }
  84. func getBazaarIcon(c *gin.Context) {
  85. ret := gulu.Ret.NewResult()
  86. defer c.JSON(http.StatusOK, ret)
  87. ret.Data = map[string]interface{}{
  88. "packages": model.BazaarIcons(),
  89. }
  90. }
  91. func installBazaarIcon(c *gin.Context) {
  92. ret := gulu.Ret.NewResult()
  93. defer c.JSON(http.StatusOK, ret)
  94. arg, ok := util.JsonArg(c, ret)
  95. if !ok {
  96. return
  97. }
  98. repoURL := arg["repoURL"].(string)
  99. repoHash := arg["repoHash"].(string)
  100. packageName := arg["packageName"].(string)
  101. err := model.InstallBazaarIcon(repoURL, repoHash, packageName)
  102. if nil != err {
  103. ret.Code = 1
  104. ret.Msg = err.Error()
  105. return
  106. }
  107. util.PushMsg(model.Conf.Language(69), 3000)
  108. ret.Data = map[string]interface{}{
  109. "packages": model.BazaarIcons(),
  110. "appearance": model.Conf.Appearance,
  111. }
  112. }
  113. func uninstallBazaarIcon(c *gin.Context) {
  114. ret := gulu.Ret.NewResult()
  115. defer c.JSON(http.StatusOK, ret)
  116. arg, ok := util.JsonArg(c, ret)
  117. if !ok {
  118. return
  119. }
  120. packageName := arg["packageName"].(string)
  121. err := model.UninstallBazaarIcon(packageName)
  122. if nil != err {
  123. ret.Code = -1
  124. ret.Msg = err.Error()
  125. return
  126. }
  127. ret.Data = map[string]interface{}{
  128. "packages": model.BazaarIcons(),
  129. "appearance": model.Conf.Appearance,
  130. }
  131. }
  132. func getBazaarTemplate(c *gin.Context) {
  133. ret := gulu.Ret.NewResult()
  134. defer c.JSON(http.StatusOK, ret)
  135. ret.Data = map[string]interface{}{
  136. "packages": model.BazaarTemplates(),
  137. }
  138. }
  139. func getInstalledTemplate(c *gin.Context) {
  140. ret := gulu.Ret.NewResult()
  141. defer c.JSON(http.StatusOK, ret)
  142. ret.Data = map[string]interface{}{
  143. "packages": bazaar.InstalledTemplates(),
  144. }
  145. }
  146. func installBazaarTemplate(c *gin.Context) {
  147. ret := gulu.Ret.NewResult()
  148. defer c.JSON(http.StatusOK, ret)
  149. arg, ok := util.JsonArg(c, ret)
  150. if !ok {
  151. return
  152. }
  153. repoURL := arg["repoURL"].(string)
  154. repoHash := arg["repoHash"].(string)
  155. packageName := arg["packageName"].(string)
  156. err := model.InstallBazaarTemplate(repoURL, repoHash, packageName)
  157. if nil != err {
  158. ret.Code = 1
  159. ret.Msg = err.Error()
  160. return
  161. }
  162. ret.Data = map[string]interface{}{
  163. "packages": model.BazaarTemplates(),
  164. }
  165. util.PushMsg(model.Conf.Language(69), 3000)
  166. }
  167. func uninstallBazaarTemplate(c *gin.Context) {
  168. ret := gulu.Ret.NewResult()
  169. defer c.JSON(http.StatusOK, ret)
  170. arg, ok := util.JsonArg(c, ret)
  171. if !ok {
  172. return
  173. }
  174. packageName := arg["packageName"].(string)
  175. err := model.UninstallBazaarTemplate(packageName)
  176. if nil != err {
  177. ret.Code = -1
  178. ret.Msg = err.Error()
  179. return
  180. }
  181. ret.Data = map[string]interface{}{
  182. "packages": model.BazaarTemplates(),
  183. }
  184. }
  185. func getBazaarTheme(c *gin.Context) {
  186. ret := gulu.Ret.NewResult()
  187. defer c.JSON(http.StatusOK, ret)
  188. ret.Data = map[string]interface{}{
  189. "packages": model.BazaarThemes(),
  190. }
  191. }
  192. func getInstalledTheme(c *gin.Context) {
  193. ret := gulu.Ret.NewResult()
  194. defer c.JSON(http.StatusOK, ret)
  195. ret.Data = map[string]interface{}{
  196. "packages": bazaar.InstalledThemes(),
  197. }
  198. }
  199. func installBazaarTheme(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. repoURL := arg["repoURL"].(string)
  207. repoHash := arg["repoHash"].(string)
  208. packageName := arg["packageName"].(string)
  209. mode := arg["mode"].(float64)
  210. update := false
  211. if nil != arg["update"] {
  212. update = arg["update"].(bool)
  213. }
  214. err := model.InstallBazaarTheme(repoURL, repoHash, packageName, int(mode), update)
  215. if nil != err {
  216. ret.Code = 1
  217. ret.Msg = err.Error()
  218. return
  219. }
  220. util.PushMsg(model.Conf.Language(69), 3000)
  221. ret.Data = map[string]interface{}{
  222. "packages": model.BazaarThemes(),
  223. "appearance": model.Conf.Appearance,
  224. }
  225. }
  226. func uninstallBazaarTheme(c *gin.Context) {
  227. ret := gulu.Ret.NewResult()
  228. defer c.JSON(http.StatusOK, ret)
  229. arg, ok := util.JsonArg(c, ret)
  230. if !ok {
  231. return
  232. }
  233. packageName := arg["packageName"].(string)
  234. err := model.UninstallBazaarTheme(packageName)
  235. if nil != err {
  236. ret.Code = -1
  237. ret.Msg = err.Error()
  238. return
  239. }
  240. ret.Data = map[string]interface{}{
  241. "packages": model.BazaarThemes(),
  242. "appearance": model.Conf.Appearance,
  243. }
  244. }