A privacy-first, self-hosted, fully open source personal knowledge management software, written in typescript and golang. (PERSONAL FORK)
at upstream/main 570 lines 12 kB view raw
1// SiYuan - Refactor your thinking 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 17package api 18 19import ( 20 "net/http" 21 22 "github.com/88250/gulu" 23 "github.com/gin-gonic/gin" 24 "github.com/siyuan-note/siyuan/kernel/model" 25 "github.com/siyuan-note/siyuan/kernel/util" 26) 27 28func batchUpdatePackage(c *gin.Context) { 29 ret := gulu.Ret.NewResult() 30 defer c.JSON(http.StatusOK, ret) 31 32 arg, ok := util.JsonArg(c, ret) 33 if !ok { 34 return 35 } 36 37 frontend := arg["frontend"].(string) 38 model.BatchUpdateBazaarPackages(frontend) 39} 40 41func getUpdatedPackage(c *gin.Context) { 42 ret := gulu.Ret.NewResult() 43 defer c.JSON(http.StatusOK, ret) 44 45 arg, ok := util.JsonArg(c, ret) 46 if !ok { 47 return 48 } 49 50 frontend := arg["frontend"].(string) 51 plugins, widgets, icons, themes, templates := model.UpdatedPackages(frontend) 52 ret.Data = map[string]interface{}{ 53 "plugins": plugins, 54 "widgets": widgets, 55 "icons": icons, 56 "themes": themes, 57 "templates": templates, 58 } 59} 60 61func getBazaarPackageREAME(c *gin.Context) { 62 ret := gulu.Ret.NewResult() 63 defer c.JSON(http.StatusOK, ret) 64 65 arg, ok := util.JsonArg(c, ret) 66 if !ok { 67 return 68 } 69 70 repoURL := arg["repoURL"].(string) 71 repoHash := arg["repoHash"].(string) 72 packageType := arg["packageType"].(string) 73 ret.Data = map[string]interface{}{ 74 "html": model.GetPackageREADME(repoURL, repoHash, packageType), 75 } 76} 77 78func getBazaarPlugin(c *gin.Context) { 79 ret := gulu.Ret.NewResult() 80 defer c.JSON(http.StatusOK, ret) 81 82 arg, ok := util.JsonArg(c, ret) 83 if !ok { 84 return 85 } 86 87 frontend := arg["frontend"].(string) 88 var keyword string 89 if keywordArg := arg["keyword"]; nil != keywordArg { 90 keyword = keywordArg.(string) 91 } 92 93 ret.Data = map[string]interface{}{ 94 "packages": model.BazaarPlugins(frontend, keyword), 95 } 96} 97 98func getInstalledPlugin(c *gin.Context) { 99 ret := gulu.Ret.NewResult() 100 defer c.JSON(http.StatusOK, ret) 101 102 arg, ok := util.JsonArg(c, ret) 103 if !ok { 104 return 105 } 106 107 frontend := arg["frontend"].(string) 108 var keyword string 109 if keywordArg := arg["keyword"]; nil != keywordArg { 110 keyword = keywordArg.(string) 111 } 112 113 ret.Data = map[string]interface{}{ 114 "packages": model.InstalledPlugins(frontend, keyword), 115 } 116} 117 118func installBazaarPlugin(c *gin.Context) { 119 ret := gulu.Ret.NewResult() 120 defer c.JSON(http.StatusOK, ret) 121 122 arg, ok := util.JsonArg(c, ret) 123 if !ok { 124 return 125 } 126 127 var keyword string 128 if keywordArg := arg["keyword"]; nil != keywordArg { 129 keyword = keywordArg.(string) 130 } 131 132 repoURL := arg["repoURL"].(string) 133 repoHash := arg["repoHash"].(string) 134 packageName := arg["packageName"].(string) 135 err := model.InstallBazaarPlugin(repoURL, repoHash, packageName) 136 if err != nil { 137 ret.Code = 1 138 ret.Msg = err.Error() 139 return 140 } 141 142 frontend := arg["frontend"].(string) 143 144 util.PushMsg(model.Conf.Language(69), 3000) 145 ret.Data = map[string]interface{}{ 146 "packages": model.BazaarPlugins(frontend, keyword), 147 } 148} 149 150func uninstallBazaarPlugin(c *gin.Context) { 151 ret := gulu.Ret.NewResult() 152 defer c.JSON(http.StatusOK, ret) 153 154 arg, ok := util.JsonArg(c, ret) 155 if !ok { 156 return 157 } 158 159 var keyword string 160 if keywordArg := arg["keyword"]; nil != keywordArg { 161 keyword = keywordArg.(string) 162 } 163 164 frontend := arg["frontend"].(string) 165 packageName := arg["packageName"].(string) 166 err := model.UninstallBazaarPlugin(packageName, frontend) 167 if err != nil { 168 ret.Code = -1 169 ret.Msg = err.Error() 170 return 171 } 172 173 ret.Data = map[string]interface{}{ 174 "packages": model.BazaarPlugins(frontend, keyword), 175 } 176} 177 178func getBazaarWidget(c *gin.Context) { 179 ret := gulu.Ret.NewResult() 180 defer c.JSON(http.StatusOK, ret) 181 182 arg, ok := util.JsonArg(c, ret) 183 if !ok { 184 return 185 } 186 187 var keyword string 188 if keywordArg := arg["keyword"]; nil != keywordArg { 189 keyword = keywordArg.(string) 190 } 191 192 ret.Data = map[string]interface{}{ 193 "packages": model.BazaarWidgets(keyword), 194 } 195} 196 197func getInstalledWidget(c *gin.Context) { 198 ret := gulu.Ret.NewResult() 199 defer c.JSON(http.StatusOK, ret) 200 201 arg, ok := util.JsonArg(c, ret) 202 if !ok { 203 return 204 } 205 206 var keyword string 207 if keywordArg := arg["keyword"]; nil != keywordArg { 208 keyword = keywordArg.(string) 209 } 210 211 ret.Data = map[string]interface{}{ 212 "packages": model.InstalledWidgets(keyword), 213 } 214} 215 216func installBazaarWidget(c *gin.Context) { 217 ret := gulu.Ret.NewResult() 218 defer c.JSON(http.StatusOK, ret) 219 220 arg, ok := util.JsonArg(c, ret) 221 if !ok { 222 return 223 } 224 225 var keyword string 226 if keywordArg := arg["keyword"]; nil != keywordArg { 227 keyword = keywordArg.(string) 228 } 229 230 repoURL := arg["repoURL"].(string) 231 repoHash := arg["repoHash"].(string) 232 packageName := arg["packageName"].(string) 233 err := model.InstallBazaarWidget(repoURL, repoHash, packageName) 234 if err != nil { 235 ret.Code = 1 236 ret.Msg = err.Error() 237 return 238 } 239 240 util.PushMsg(model.Conf.Language(69), 3000) 241 ret.Data = map[string]interface{}{ 242 "packages": model.BazaarWidgets(keyword), 243 } 244} 245 246func uninstallBazaarWidget(c *gin.Context) { 247 ret := gulu.Ret.NewResult() 248 defer c.JSON(http.StatusOK, ret) 249 250 arg, ok := util.JsonArg(c, ret) 251 if !ok { 252 return 253 } 254 255 var keyword string 256 if keywordArg := arg["keyword"]; nil != keywordArg { 257 keyword = keywordArg.(string) 258 } 259 260 packageName := arg["packageName"].(string) 261 err := model.UninstallBazaarWidget(packageName) 262 if err != nil { 263 ret.Code = -1 264 ret.Msg = err.Error() 265 return 266 } 267 268 ret.Data = map[string]interface{}{ 269 "packages": model.BazaarWidgets(keyword), 270 } 271} 272 273func getBazaarIcon(c *gin.Context) { 274 ret := gulu.Ret.NewResult() 275 defer c.JSON(http.StatusOK, ret) 276 277 arg, ok := util.JsonArg(c, ret) 278 if !ok { 279 return 280 } 281 282 var keyword string 283 if keywordArg := arg["keyword"]; nil != keywordArg { 284 keyword = keywordArg.(string) 285 } 286 287 ret.Data = map[string]interface{}{ 288 "packages": model.BazaarIcons(keyword), 289 } 290} 291 292func getInstalledIcon(c *gin.Context) { 293 ret := gulu.Ret.NewResult() 294 defer c.JSON(http.StatusOK, ret) 295 296 arg, ok := util.JsonArg(c, ret) 297 if !ok { 298 return 299 } 300 301 var keyword string 302 if keywordArg := arg["keyword"]; nil != keywordArg { 303 keyword = keywordArg.(string) 304 } 305 306 ret.Data = map[string]interface{}{ 307 "packages": model.InstalledIcons(keyword), 308 } 309} 310 311func installBazaarIcon(c *gin.Context) { 312 ret := gulu.Ret.NewResult() 313 defer c.JSON(http.StatusOK, ret) 314 315 arg, ok := util.JsonArg(c, ret) 316 if !ok { 317 return 318 } 319 320 var keyword string 321 if keywordArg := arg["keyword"]; nil != keywordArg { 322 keyword = keywordArg.(string) 323 } 324 325 repoURL := arg["repoURL"].(string) 326 repoHash := arg["repoHash"].(string) 327 packageName := arg["packageName"].(string) 328 err := model.InstallBazaarIcon(repoURL, repoHash, packageName) 329 if err != nil { 330 ret.Code = 1 331 ret.Msg = err.Error() 332 return 333 } 334 util.PushMsg(model.Conf.Language(69), 3000) 335 336 ret.Data = map[string]interface{}{ 337 "packages": model.BazaarIcons(keyword), 338 "appearance": model.Conf.Appearance, 339 } 340} 341 342func uninstallBazaarIcon(c *gin.Context) { 343 ret := gulu.Ret.NewResult() 344 defer c.JSON(http.StatusOK, ret) 345 346 arg, ok := util.JsonArg(c, ret) 347 if !ok { 348 return 349 } 350 351 var keyword string 352 if keywordArg := arg["keyword"]; nil != keywordArg { 353 keyword = keywordArg.(string) 354 } 355 356 packageName := arg["packageName"].(string) 357 err := model.UninstallBazaarIcon(packageName) 358 if err != nil { 359 ret.Code = -1 360 ret.Msg = err.Error() 361 return 362 } 363 364 ret.Data = map[string]interface{}{ 365 "packages": model.BazaarIcons(keyword), 366 "appearance": model.Conf.Appearance, 367 } 368} 369 370func getBazaarTemplate(c *gin.Context) { 371 ret := gulu.Ret.NewResult() 372 defer c.JSON(http.StatusOK, ret) 373 374 arg, ok := util.JsonArg(c, ret) 375 if !ok { 376 return 377 } 378 379 var keyword string 380 if keywordArg := arg["keyword"]; nil != keywordArg { 381 keyword = keywordArg.(string) 382 } 383 384 ret.Data = map[string]interface{}{ 385 "packages": model.BazaarTemplates(keyword), 386 } 387} 388 389func getInstalledTemplate(c *gin.Context) { 390 ret := gulu.Ret.NewResult() 391 defer c.JSON(http.StatusOK, ret) 392 393 arg, ok := util.JsonArg(c, ret) 394 if !ok { 395 return 396 } 397 398 var keyword string 399 if keywordArg := arg["keyword"]; nil != keywordArg { 400 keyword = keywordArg.(string) 401 } 402 403 ret.Data = map[string]interface{}{ 404 "packages": model.InstalledTemplates(keyword), 405 } 406} 407 408func installBazaarTemplate(c *gin.Context) { 409 ret := gulu.Ret.NewResult() 410 defer c.JSON(http.StatusOK, ret) 411 412 arg, ok := util.JsonArg(c, ret) 413 if !ok { 414 return 415 } 416 417 var keyword string 418 if keywordArg := arg["keyword"]; nil != keywordArg { 419 keyword = keywordArg.(string) 420 } 421 422 repoURL := arg["repoURL"].(string) 423 repoHash := arg["repoHash"].(string) 424 packageName := arg["packageName"].(string) 425 err := model.InstallBazaarTemplate(repoURL, repoHash, packageName) 426 if err != nil { 427 ret.Code = 1 428 ret.Msg = err.Error() 429 return 430 } 431 432 ret.Data = map[string]interface{}{ 433 "packages": model.BazaarTemplates(keyword), 434 } 435 436 util.PushMsg(model.Conf.Language(69), 3000) 437} 438 439func uninstallBazaarTemplate(c *gin.Context) { 440 ret := gulu.Ret.NewResult() 441 defer c.JSON(http.StatusOK, ret) 442 443 arg, ok := util.JsonArg(c, ret) 444 if !ok { 445 return 446 } 447 448 var keyword string 449 if keywordArg := arg["keyword"]; nil != keywordArg { 450 keyword = keywordArg.(string) 451 } 452 453 packageName := arg["packageName"].(string) 454 err := model.UninstallBazaarTemplate(packageName) 455 if err != nil { 456 ret.Code = -1 457 ret.Msg = err.Error() 458 return 459 } 460 461 ret.Data = map[string]interface{}{ 462 "packages": model.BazaarTemplates(keyword), 463 } 464} 465 466func getBazaarTheme(c *gin.Context) { 467 ret := gulu.Ret.NewResult() 468 defer c.JSON(http.StatusOK, ret) 469 470 arg, ok := util.JsonArg(c, ret) 471 if !ok { 472 return 473 } 474 475 var keyword string 476 if keywordArg := arg["keyword"]; nil != keywordArg { 477 keyword = keywordArg.(string) 478 } 479 480 ret.Data = map[string]interface{}{ 481 "packages": model.BazaarThemes(keyword), 482 } 483} 484 485func getInstalledTheme(c *gin.Context) { 486 ret := gulu.Ret.NewResult() 487 defer c.JSON(http.StatusOK, ret) 488 489 arg, ok := util.JsonArg(c, ret) 490 if !ok { 491 return 492 } 493 494 var keyword string 495 if keywordArg := arg["keyword"]; nil != keywordArg { 496 keyword = keywordArg.(string) 497 } 498 499 ret.Data = map[string]interface{}{ 500 "packages": model.InstalledThemes(keyword), 501 } 502} 503 504func installBazaarTheme(c *gin.Context) { 505 ret := gulu.Ret.NewResult() 506 defer c.JSON(http.StatusOK, ret) 507 508 arg, ok := util.JsonArg(c, ret) 509 if !ok { 510 return 511 } 512 513 var keyword string 514 if keywordArg := arg["keyword"]; nil != keywordArg { 515 keyword = keywordArg.(string) 516 } 517 518 repoURL := arg["repoURL"].(string) 519 repoHash := arg["repoHash"].(string) 520 packageName := arg["packageName"].(string) 521 mode := arg["mode"].(float64) 522 update := false 523 if nil != arg["update"] { 524 update = arg["update"].(bool) 525 } 526 err := model.InstallBazaarTheme(repoURL, repoHash, packageName, int(mode), update) 527 if err != nil { 528 ret.Code = 1 529 ret.Msg = err.Error() 530 return 531 } 532 533 // 安装集市主题后不跟随系统切换外观模式 534 model.Conf.Appearance.ModeOS = false 535 model.Conf.Save() 536 537 util.PushMsg(model.Conf.Language(69), 3000) 538 ret.Data = map[string]interface{}{ 539 "packages": model.BazaarThemes(keyword), 540 "appearance": model.Conf.Appearance, 541 } 542} 543 544func uninstallBazaarTheme(c *gin.Context) { 545 ret := gulu.Ret.NewResult() 546 defer c.JSON(http.StatusOK, ret) 547 548 arg, ok := util.JsonArg(c, ret) 549 if !ok { 550 return 551 } 552 553 var keyword string 554 if keywordArg := arg["keyword"]; nil != keywordArg { 555 keyword = keywordArg.(string) 556 } 557 558 packageName := arg["packageName"].(string) 559 err := model.UninstallBazaarTheme(packageName) 560 if err != nil { 561 ret.Code = -1 562 ret.Msg = err.Error() 563 return 564 } 565 566 ret.Data = map[string]interface{}{ 567 "packages": model.BazaarThemes(keyword), 568 "appearance": model.Conf.Appearance, 569 } 570}