bazaar.go 8.4 KB

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