bazaar.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 installBazaarTemplate(c *gin.Context) {
  140. ret := gulu.Ret.NewResult()
  141. defer c.JSON(http.StatusOK, ret)
  142. arg, ok := util.JsonArg(c, ret)
  143. if !ok {
  144. return
  145. }
  146. repoURL := arg["repoURL"].(string)
  147. repoHash := arg["repoHash"].(string)
  148. packageName := arg["packageName"].(string)
  149. err := model.InstallBazaarTemplate(repoURL, repoHash, packageName)
  150. if nil != err {
  151. ret.Code = 1
  152. ret.Msg = err.Error()
  153. return
  154. }
  155. ret.Data = map[string]interface{}{
  156. "packages": model.BazaarTemplates(),
  157. }
  158. util.PushMsg(model.Conf.Language(69), 3000)
  159. }
  160. func uninstallBazaarTemplate(c *gin.Context) {
  161. ret := gulu.Ret.NewResult()
  162. defer c.JSON(http.StatusOK, ret)
  163. arg, ok := util.JsonArg(c, ret)
  164. if !ok {
  165. return
  166. }
  167. packageName := arg["packageName"].(string)
  168. err := model.UninstallBazaarTemplate(packageName)
  169. if nil != err {
  170. ret.Code = -1
  171. ret.Msg = err.Error()
  172. return
  173. }
  174. ret.Data = map[string]interface{}{
  175. "packages": model.BazaarTemplates(),
  176. }
  177. }
  178. func getBazaarTheme(c *gin.Context) {
  179. ret := gulu.Ret.NewResult()
  180. defer c.JSON(http.StatusOK, ret)
  181. ret.Data = map[string]interface{}{
  182. "packages": model.BazaarThemes(),
  183. }
  184. }
  185. func getInstalledTheme(c *gin.Context) {
  186. ret := gulu.Ret.NewResult()
  187. defer c.JSON(http.StatusOK, ret)
  188. ret.Data = map[string]interface{}{
  189. "packages": bazaar.InstalledThemes(),
  190. }
  191. }
  192. func installBazaarTheme(c *gin.Context) {
  193. ret := gulu.Ret.NewResult()
  194. defer c.JSON(http.StatusOK, ret)
  195. arg, ok := util.JsonArg(c, ret)
  196. if !ok {
  197. return
  198. }
  199. repoURL := arg["repoURL"].(string)
  200. repoHash := arg["repoHash"].(string)
  201. packageName := arg["packageName"].(string)
  202. mode := arg["mode"].(float64)
  203. update := false
  204. if nil != arg["update"] {
  205. update = arg["update"].(bool)
  206. }
  207. err := model.InstallBazaarTheme(repoURL, repoHash, packageName, int(mode), update)
  208. if nil != err {
  209. ret.Code = 1
  210. ret.Msg = err.Error()
  211. return
  212. }
  213. util.PushMsg(model.Conf.Language(69), 3000)
  214. ret.Data = map[string]interface{}{
  215. "packages": model.BazaarThemes(),
  216. "appearance": model.Conf.Appearance,
  217. }
  218. }
  219. func uninstallBazaarTheme(c *gin.Context) {
  220. ret := gulu.Ret.NewResult()
  221. defer c.JSON(http.StatusOK, ret)
  222. arg, ok := util.JsonArg(c, ret)
  223. if !ok {
  224. return
  225. }
  226. packageName := arg["packageName"].(string)
  227. err := model.UninstallBazaarTheme(packageName)
  228. if nil != err {
  229. ret.Code = -1
  230. ret.Msg = err.Error()
  231. return
  232. }
  233. ret.Data = map[string]interface{}{
  234. "packages": model.BazaarThemes(),
  235. "appearance": model.Conf.Appearance,
  236. }
  237. }