widget.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. "path/filepath"
  21. "sort"
  22. "strings"
  23. "sync"
  24. "github.com/dustin/go-humanize"
  25. ants "github.com/panjf2000/ants/v2"
  26. "github.com/siyuan-note/httpclient"
  27. "github.com/siyuan-note/logging"
  28. "github.com/siyuan-note/siyuan/kernel/util"
  29. )
  30. type Widget struct {
  31. Package
  32. }
  33. func Widgets() (widgets []*Widget) {
  34. widgets = []*Widget{}
  35. pkgIndex, err := getPkgIndex("widgets")
  36. if nil != err {
  37. return
  38. }
  39. bazaarIndex := getBazaarIndex()
  40. repos := pkgIndex["repos"].([]interface{})
  41. waitGroup := &sync.WaitGroup{}
  42. lock := &sync.Mutex{}
  43. p, _ := ants.NewPoolWithFunc(8, func(arg interface{}) {
  44. defer waitGroup.Done()
  45. repo := arg.(map[string]interface{})
  46. repoURL := repo["url"].(string)
  47. widget := &Widget{}
  48. innerU := util.BazaarOSSServer + "/package/" + repoURL + "/widget.json"
  49. innerResp, innerErr := httpclient.NewBrowserRequest().SetResult(widget).Get(innerU)
  50. if nil != innerErr {
  51. logging.LogErrorf("get bazaar package [%s] failed: %s", repoURL, innerErr)
  52. return
  53. }
  54. if 200 != innerResp.StatusCode {
  55. logging.LogErrorf("get bazaar package [%s] failed: %d", innerU, innerResp.StatusCode)
  56. return
  57. }
  58. repoURLHash := strings.Split(repoURL, "@")
  59. widget.RepoURL = "https://github.com/" + repoURLHash[0]
  60. widget.RepoHash = repoURLHash[1]
  61. widget.PreviewURL = util.BazaarOSSServer + "/package/" + repoURL + "/preview.png?imageslim"
  62. widget.PreviewURLThumb = util.BazaarOSSServer + "/package/" + repoURL + "/preview.png?imageView2/2/w/436/h/232"
  63. widget.Updated = repo["updated"].(string)
  64. widget.Stars = int(repo["stars"].(float64))
  65. widget.OpenIssues = int(repo["openIssues"].(float64))
  66. widget.Size = int64(repo["size"].(float64))
  67. widget.HSize = humanize.Bytes(uint64(widget.Size))
  68. widget.HUpdated = formatUpdated(widget.Updated)
  69. pkg := bazaarIndex[strings.Split(repoURL, "@")[0]]
  70. if nil != pkg {
  71. widget.Downloads = pkg.Downloads
  72. }
  73. lock.Lock()
  74. widgets = append(widgets, widget)
  75. lock.Unlock()
  76. })
  77. for _, repo := range repos {
  78. waitGroup.Add(1)
  79. p.Invoke(repo)
  80. }
  81. waitGroup.Wait()
  82. p.Release()
  83. sort.Slice(widgets, func(i, j int) bool { return widgets[i].Updated > widgets[j].Updated })
  84. return
  85. }
  86. func InstalledWidgets() (ret []*Widget) {
  87. ret = []*Widget{}
  88. dir, err := os.Open(filepath.Join(util.DataDir, "widgets"))
  89. if nil != err {
  90. logging.LogWarnf("open widgets folder [%s] failed: %s", util.ThemesPath, err)
  91. return
  92. }
  93. widgetDirs, err := dir.Readdir(-1)
  94. if nil != err {
  95. logging.LogWarnf("read widgets folder failed: %s", err)
  96. return
  97. }
  98. dir.Close()
  99. bazaarWidgets := Widgets()
  100. for _, widgetDir := range widgetDirs {
  101. if !widgetDir.IsDir() {
  102. continue
  103. }
  104. dirName := widgetDir.Name()
  105. widgetConf, parseErr := WidgetJSON(dirName)
  106. if nil != parseErr || nil == widgetConf {
  107. continue
  108. }
  109. widget := &Widget{}
  110. widget.Installed = true
  111. widget.Name = widgetConf["name"].(string)
  112. widget.Author = widgetConf["author"].(string)
  113. widget.URL = widgetConf["url"].(string)
  114. widget.Version = widgetConf["version"].(string)
  115. widget.RepoURL = widget.URL
  116. widget.PreviewURL = "/widgets/" + dirName + "/preview.png"
  117. widget.PreviewURLThumb = "/widgets/" + dirName + "/preview.png"
  118. widget.Updated = widgetDir.ModTime().Format("2006-01-02 15:04:05")
  119. widget.Size = widgetDir.Size()
  120. widget.HSize = humanize.Bytes(uint64(widget.Size))
  121. widget.HUpdated = formatUpdated(widget.Updated)
  122. readme, readErr := os.ReadFile(filepath.Join(util.DataDir, "widgets", dirName, "README.md"))
  123. if nil != readErr {
  124. logging.LogWarnf("read install widget README.md failed: %s", readErr)
  125. continue
  126. }
  127. widget.README, _ = renderREADME(widget.URL, readme)
  128. widget.Outdated = isOutdatedWidget(widget, bazaarWidgets)
  129. ret = append(ret, widget)
  130. }
  131. return
  132. }
  133. func InstallWidget(repoURL, repoHash, installPath string, systemID string) error {
  134. repoURLHash := repoURL + "@" + repoHash
  135. data, err := downloadPackage(repoURLHash, true, systemID)
  136. if nil != err {
  137. return err
  138. }
  139. return installPackage(data, installPath)
  140. }
  141. func UninstallWidget(installPath string) error {
  142. if err := os.RemoveAll(installPath); nil != err {
  143. logging.LogErrorf("remove widget [%s] failed: %s", installPath, err)
  144. return errors.New("remove community widget failed")
  145. }
  146. //logging.Logger.Infof("uninstalled widget [%s]", installPath)
  147. return nil
  148. }