widget.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 bazaar
  17. import (
  18. "errors"
  19. "os"
  20. "sort"
  21. "strings"
  22. "sync"
  23. "github.com/dustin/go-humanize"
  24. ants "github.com/panjf2000/ants/v2"
  25. "github.com/siyuan-note/httpclient"
  26. "github.com/siyuan-note/logging"
  27. "github.com/siyuan-note/siyuan/kernel/util"
  28. )
  29. type Widget struct {
  30. Author string `json:"author"`
  31. URL string `json:"url"`
  32. Version string `json:"version"`
  33. Name string `json:"name"`
  34. RepoURL string `json:"repoURL"`
  35. RepoHash string `json:"repoHash"`
  36. PreviewURL string `json:"previewURL"`
  37. PreviewURLThumb string `json:"previewURLThumb"`
  38. README string `json:"readme"`
  39. Installed bool `json:"installed"`
  40. Outdated bool `json:"outdated"`
  41. Current bool `json:"current"`
  42. Updated string `json:"updated"`
  43. Stars int `json:"stars"`
  44. OpenIssues int `json:"openIssues"`
  45. Size int64 `json:"size"`
  46. HSize string `json:"hSize"`
  47. HUpdated string `json:"hUpdated"`
  48. Downloads int `json:"downloads"`
  49. }
  50. func Widgets() (widgets []*Widget) {
  51. widgets = []*Widget{}
  52. result, err := util.GetRhyResult(false)
  53. if nil != err {
  54. return
  55. }
  56. bazaarIndex := getBazaarIndex()
  57. bazaarHash := result["bazaar"].(string)
  58. result = map[string]interface{}{}
  59. request := httpclient.NewBrowserRequest()
  60. u := util.BazaarOSSServer + "/bazaar@" + bazaarHash + "/stage/widgets.json"
  61. resp, err := request.SetResult(&result).Get(u)
  62. if nil != err {
  63. logging.LogErrorf("get community stage index [%s] failed: %s", u, err)
  64. return
  65. }
  66. if 200 != resp.StatusCode {
  67. logging.LogErrorf("get community stage index [%s] failed: %d", u, resp.StatusCode)
  68. return
  69. }
  70. repos := result["repos"].([]interface{})
  71. waitGroup := &sync.WaitGroup{}
  72. lock := &sync.Mutex{}
  73. p, _ := ants.NewPoolWithFunc(8, func(arg interface{}) {
  74. defer waitGroup.Done()
  75. repo := arg.(map[string]interface{})
  76. repoURL := repo["url"].(string)
  77. widget := &Widget{}
  78. innerU := util.BazaarOSSServer + "/package/" + repoURL + "/widget.json"
  79. innerResp, innerErr := httpclient.NewBrowserRequest().SetResult(widget).Get(innerU)
  80. if nil != innerErr {
  81. logging.LogErrorf("get bazaar package [%s] failed: %s", repoURL, innerErr)
  82. return
  83. }
  84. if 200 != innerResp.StatusCode {
  85. logging.LogErrorf("get bazaar package [%s] failed: %d", innerU, innerResp.StatusCode)
  86. return
  87. }
  88. repoURLHash := strings.Split(repoURL, "@")
  89. widget.RepoURL = "https://github.com/" + repoURLHash[0]
  90. widget.RepoHash = repoURLHash[1]
  91. widget.PreviewURL = util.BazaarOSSServer + "/package/" + repoURL + "/preview.png?imageslim"
  92. widget.PreviewURLThumb = util.BazaarOSSServer + "/package/" + repoURL + "/preview.png?imageView2/2/w/436/h/232"
  93. widget.Updated = repo["updated"].(string)
  94. widget.Stars = int(repo["stars"].(float64))
  95. widget.OpenIssues = int(repo["openIssues"].(float64))
  96. widget.Size = int64(repo["size"].(float64))
  97. widget.HSize = humanize.Bytes(uint64(widget.Size))
  98. widget.HUpdated = formatUpdated(widget.Updated)
  99. pkg := bazaarIndex[strings.Split(repoURL, "@")[0]]
  100. if nil != pkg {
  101. widget.Downloads = pkg.Downloads
  102. }
  103. lock.Lock()
  104. widgets = append(widgets, widget)
  105. lock.Unlock()
  106. })
  107. for _, repo := range repos {
  108. waitGroup.Add(1)
  109. p.Invoke(repo)
  110. }
  111. waitGroup.Wait()
  112. p.Release()
  113. sort.Slice(widgets, func(i, j int) bool { return widgets[i].Updated > widgets[j].Updated })
  114. return
  115. }
  116. func InstallWidget(repoURL, repoHash, installPath string, systemID string) error {
  117. repoURLHash := repoURL + "@" + repoHash
  118. data, err := downloadPackage(repoURLHash, true, systemID)
  119. if nil != err {
  120. return err
  121. }
  122. return installPackage(data, installPath)
  123. }
  124. func UninstallWidget(installPath string) error {
  125. if err := os.RemoveAll(installPath); nil != err {
  126. logging.LogErrorf("remove widget [%s] failed: %s", installPath, err)
  127. return errors.New("remove community widget failed")
  128. }
  129. //logging.Logger.Infof("uninstalled widget [%s]", installPath)
  130. return nil
  131. }