bazaar.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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/model"
  22. "github.com/siyuan-note/siyuan/kernel/util"
  23. )
  24. func getBazaarPackageREAME(c *gin.Context) {
  25. ret := gulu.Ret.NewResult()
  26. defer c.JSON(http.StatusOK, ret)
  27. arg, ok := util.JsonArg(c, ret)
  28. if !ok {
  29. return
  30. }
  31. repoURL := arg["repoURL"].(string)
  32. repoHash := arg["repoHash"].(string)
  33. packageType := arg["packageType"].(string)
  34. ret.Data = map[string]interface{}{
  35. "html": model.GetPackageREADME(repoURL, repoHash, packageType),
  36. }
  37. }
  38. func getBazaarPlugin(c *gin.Context) {
  39. ret := gulu.Ret.NewResult()
  40. defer c.JSON(http.StatusOK, ret)
  41. ret.Data = map[string]interface{}{
  42. "packages": model.BazaarPlugins(),
  43. }
  44. }
  45. func getInstalledPlugin(c *gin.Context) {
  46. ret := gulu.Ret.NewResult()
  47. defer c.JSON(http.StatusOK, ret)
  48. ret.Data = map[string]interface{}{
  49. "packages": model.InstalledPlugins(),
  50. }
  51. }
  52. func installBazaarPlugin(c *gin.Context) {
  53. ret := gulu.Ret.NewResult()
  54. defer c.JSON(http.StatusOK, ret)
  55. arg, ok := util.JsonArg(c, ret)
  56. if !ok {
  57. return
  58. }
  59. repoURL := arg["repoURL"].(string)
  60. repoHash := arg["repoHash"].(string)
  61. packageName := arg["packageName"].(string)
  62. err := model.InstallBazaarPlugin(repoURL, repoHash, packageName)
  63. if nil != err {
  64. ret.Code = 1
  65. ret.Msg = err.Error()
  66. return
  67. }
  68. util.PushMsg(model.Conf.Language(69), 3000)
  69. ret.Data = map[string]interface{}{
  70. "packages": model.BazaarPlugins(),
  71. }
  72. }
  73. func uninstallBazaarPlugin(c *gin.Context) {
  74. ret := gulu.Ret.NewResult()
  75. defer c.JSON(http.StatusOK, ret)
  76. arg, ok := util.JsonArg(c, ret)
  77. if !ok {
  78. return
  79. }
  80. packageName := arg["packageName"].(string)
  81. err := model.UninstallBazaarPlugin(packageName)
  82. if nil != err {
  83. ret.Code = -1
  84. ret.Msg = err.Error()
  85. return
  86. }
  87. ret.Data = map[string]interface{}{
  88. "packages": model.BazaarPlugins(),
  89. }
  90. }
  91. func getBazaarWidget(c *gin.Context) {
  92. ret := gulu.Ret.NewResult()
  93. defer c.JSON(http.StatusOK, ret)
  94. ret.Data = map[string]interface{}{
  95. "packages": model.BazaarWidgets(),
  96. }
  97. }
  98. func getInstalledWidget(c *gin.Context) {
  99. ret := gulu.Ret.NewResult()
  100. defer c.JSON(http.StatusOK, ret)
  101. ret.Data = map[string]interface{}{
  102. "packages": model.InstalledWidgets(),
  103. }
  104. }
  105. func installBazaarWidget(c *gin.Context) {
  106. ret := gulu.Ret.NewResult()
  107. defer c.JSON(http.StatusOK, ret)
  108. arg, ok := util.JsonArg(c, ret)
  109. if !ok {
  110. return
  111. }
  112. repoURL := arg["repoURL"].(string)
  113. repoHash := arg["repoHash"].(string)
  114. packageName := arg["packageName"].(string)
  115. err := model.InstallBazaarWidget(repoURL, repoHash, packageName)
  116. if nil != err {
  117. ret.Code = 1
  118. ret.Msg = err.Error()
  119. return
  120. }
  121. util.PushMsg(model.Conf.Language(69), 3000)
  122. ret.Data = map[string]interface{}{
  123. "packages": model.BazaarWidgets(),
  124. }
  125. }
  126. func uninstallBazaarWidget(c *gin.Context) {
  127. ret := gulu.Ret.NewResult()
  128. defer c.JSON(http.StatusOK, ret)
  129. arg, ok := util.JsonArg(c, ret)
  130. if !ok {
  131. return
  132. }
  133. packageName := arg["packageName"].(string)
  134. err := model.UninstallBazaarWidget(packageName)
  135. if nil != err {
  136. ret.Code = -1
  137. ret.Msg = err.Error()
  138. return
  139. }
  140. ret.Data = map[string]interface{}{
  141. "packages": model.BazaarWidgets(),
  142. }
  143. }
  144. func getBazaarIcon(c *gin.Context) {
  145. ret := gulu.Ret.NewResult()
  146. defer c.JSON(http.StatusOK, ret)
  147. ret.Data = map[string]interface{}{
  148. "packages": model.BazaarIcons(),
  149. }
  150. }
  151. func getInstalledIcon(c *gin.Context) {
  152. ret := gulu.Ret.NewResult()
  153. defer c.JSON(http.StatusOK, ret)
  154. ret.Data = map[string]interface{}{
  155. "packages": model.InstalledIcons(),
  156. }
  157. }
  158. func installBazaarIcon(c *gin.Context) {
  159. ret := gulu.Ret.NewResult()
  160. defer c.JSON(http.StatusOK, ret)
  161. arg, ok := util.JsonArg(c, ret)
  162. if !ok {
  163. return
  164. }
  165. repoURL := arg["repoURL"].(string)
  166. repoHash := arg["repoHash"].(string)
  167. packageName := arg["packageName"].(string)
  168. err := model.InstallBazaarIcon(repoURL, repoHash, packageName)
  169. if nil != err {
  170. ret.Code = 1
  171. ret.Msg = err.Error()
  172. return
  173. }
  174. util.PushMsg(model.Conf.Language(69), 3000)
  175. ret.Data = map[string]interface{}{
  176. "packages": model.BazaarIcons(),
  177. "appearance": model.Conf.Appearance,
  178. }
  179. }
  180. func uninstallBazaarIcon(c *gin.Context) {
  181. ret := gulu.Ret.NewResult()
  182. defer c.JSON(http.StatusOK, ret)
  183. arg, ok := util.JsonArg(c, ret)
  184. if !ok {
  185. return
  186. }
  187. packageName := arg["packageName"].(string)
  188. err := model.UninstallBazaarIcon(packageName)
  189. if nil != err {
  190. ret.Code = -1
  191. ret.Msg = err.Error()
  192. return
  193. }
  194. ret.Data = map[string]interface{}{
  195. "packages": model.BazaarIcons(),
  196. "appearance": model.Conf.Appearance,
  197. }
  198. }
  199. func getBazaarTemplate(c *gin.Context) {
  200. ret := gulu.Ret.NewResult()
  201. defer c.JSON(http.StatusOK, ret)
  202. ret.Data = map[string]interface{}{
  203. "packages": model.BazaarTemplates(),
  204. }
  205. }
  206. func getInstalledTemplate(c *gin.Context) {
  207. ret := gulu.Ret.NewResult()
  208. defer c.JSON(http.StatusOK, ret)
  209. ret.Data = map[string]interface{}{
  210. "packages": model.InstalledTemplates(),
  211. }
  212. }
  213. func installBazaarTemplate(c *gin.Context) {
  214. ret := gulu.Ret.NewResult()
  215. defer c.JSON(http.StatusOK, ret)
  216. arg, ok := util.JsonArg(c, ret)
  217. if !ok {
  218. return
  219. }
  220. repoURL := arg["repoURL"].(string)
  221. repoHash := arg["repoHash"].(string)
  222. packageName := arg["packageName"].(string)
  223. err := model.InstallBazaarTemplate(repoURL, repoHash, packageName)
  224. if nil != err {
  225. ret.Code = 1
  226. ret.Msg = err.Error()
  227. return
  228. }
  229. ret.Data = map[string]interface{}{
  230. "packages": model.BazaarTemplates(),
  231. }
  232. util.PushMsg(model.Conf.Language(69), 3000)
  233. }
  234. func uninstallBazaarTemplate(c *gin.Context) {
  235. ret := gulu.Ret.NewResult()
  236. defer c.JSON(http.StatusOK, ret)
  237. arg, ok := util.JsonArg(c, ret)
  238. if !ok {
  239. return
  240. }
  241. packageName := arg["packageName"].(string)
  242. err := model.UninstallBazaarTemplate(packageName)
  243. if nil != err {
  244. ret.Code = -1
  245. ret.Msg = err.Error()
  246. return
  247. }
  248. ret.Data = map[string]interface{}{
  249. "packages": model.BazaarTemplates(),
  250. }
  251. }
  252. func getBazaarTheme(c *gin.Context) {
  253. ret := gulu.Ret.NewResult()
  254. defer c.JSON(http.StatusOK, ret)
  255. ret.Data = map[string]interface{}{
  256. "packages": model.BazaarThemes(),
  257. }
  258. }
  259. func getInstalledTheme(c *gin.Context) {
  260. ret := gulu.Ret.NewResult()
  261. defer c.JSON(http.StatusOK, ret)
  262. ret.Data = map[string]interface{}{
  263. "packages": model.InstalledThemes(),
  264. }
  265. }
  266. func installBazaarTheme(c *gin.Context) {
  267. ret := gulu.Ret.NewResult()
  268. defer c.JSON(http.StatusOK, ret)
  269. arg, ok := util.JsonArg(c, ret)
  270. if !ok {
  271. return
  272. }
  273. repoURL := arg["repoURL"].(string)
  274. repoHash := arg["repoHash"].(string)
  275. packageName := arg["packageName"].(string)
  276. mode := arg["mode"].(float64)
  277. update := false
  278. if nil != arg["update"] {
  279. update = arg["update"].(bool)
  280. }
  281. err := model.InstallBazaarTheme(repoURL, repoHash, packageName, int(mode), update)
  282. if nil != err {
  283. ret.Code = 1
  284. ret.Msg = err.Error()
  285. return
  286. }
  287. // 安装集市主题后不跟随系统切换外观模式
  288. model.Conf.Appearance.ModeOS = false
  289. model.Conf.Save()
  290. util.PushMsg(model.Conf.Language(69), 3000)
  291. ret.Data = map[string]interface{}{
  292. "packages": model.BazaarThemes(),
  293. "appearance": model.Conf.Appearance,
  294. }
  295. }
  296. func uninstallBazaarTheme(c *gin.Context) {
  297. ret := gulu.Ret.NewResult()
  298. defer c.JSON(http.StatusOK, ret)
  299. arg, ok := util.JsonArg(c, ret)
  300. if !ok {
  301. return
  302. }
  303. packageName := arg["packageName"].(string)
  304. err := model.UninstallBazaarTheme(packageName)
  305. if nil != err {
  306. ret.Code = -1
  307. ret.Msg = err.Error()
  308. return
  309. }
  310. ret.Data = map[string]interface{}{
  311. "packages": model.BazaarThemes(),
  312. "appearance": model.Conf.Appearance,
  313. }
  314. }