widget.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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().SetSuccessResult(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. widget.URL = strings.TrimSuffix(widget.URL, "/")
  59. repoURLHash := strings.Split(repoURL, "@")
  60. widget.RepoURL = "https://github.com/" + repoURLHash[0]
  61. widget.RepoHash = repoURLHash[1]
  62. widget.PreviewURL = util.BazaarOSSServer + "/package/" + repoURL + "/preview.png?imageslim"
  63. widget.PreviewURLThumb = util.BazaarOSSServer + "/package/" + repoURL + "/preview.png?imageView2/2/w/436/h/232"
  64. widget.Updated = repo["updated"].(string)
  65. widget.Stars = int(repo["stars"].(float64))
  66. widget.OpenIssues = int(repo["openIssues"].(float64))
  67. widget.Size = int64(repo["size"].(float64))
  68. widget.HSize = humanize.Bytes(uint64(widget.Size))
  69. widget.HUpdated = formatUpdated(widget.Updated)
  70. pkg := bazaarIndex[strings.Split(repoURL, "@")[0]]
  71. if nil != pkg {
  72. widget.Downloads = pkg.Downloads
  73. }
  74. lock.Lock()
  75. widgets = append(widgets, widget)
  76. lock.Unlock()
  77. })
  78. for _, repo := range repos {
  79. waitGroup.Add(1)
  80. p.Invoke(repo)
  81. }
  82. waitGroup.Wait()
  83. p.Release()
  84. sort.Slice(widgets, func(i, j int) bool { return widgets[i].Updated > widgets[j].Updated })
  85. return
  86. }
  87. func InstalledWidgets() (ret []*Widget) {
  88. ret = []*Widget{}
  89. widgetDirs, err := os.ReadDir(filepath.Join(util.DataDir, "widgets"))
  90. if nil != err {
  91. logging.LogWarnf("read widgets folder failed: %s", err)
  92. return
  93. }
  94. bazaarWidgets := Widgets()
  95. for _, widgetDir := range widgetDirs {
  96. if !widgetDir.IsDir() {
  97. continue
  98. }
  99. dirName := widgetDir.Name()
  100. widgetConf, parseErr := WidgetJSON(dirName)
  101. if nil != parseErr || nil == widgetConf {
  102. continue
  103. }
  104. installPath := filepath.Join(util.DataDir, "widgets", dirName)
  105. widget := &Widget{}
  106. widget.Installed = true
  107. widget.Name = widgetConf["name"].(string)
  108. widget.Author = widgetConf["author"].(string)
  109. widget.URL = widgetConf["url"].(string)
  110. widget.URL = strings.TrimSuffix(widget.URL, "/")
  111. widget.Version = widgetConf["version"].(string)
  112. widget.RepoURL = widget.URL
  113. widget.PreviewURL = "/widgets/" + dirName + "/preview.png"
  114. widget.PreviewURLThumb = "/widgets/" + dirName + "/preview.png"
  115. info, statErr := os.Stat(filepath.Join(installPath, "README.md"))
  116. if nil != statErr {
  117. logging.LogWarnf("stat install theme README.md failed: %s", statErr)
  118. continue
  119. }
  120. widget.HInstallDate = info.ModTime().Format("2006-01-02")
  121. installSize, _ := util.SizeOfDirectory(installPath)
  122. widget.InstallSize = installSize
  123. widget.HInstallSize = humanize.Bytes(uint64(installSize))
  124. readme, readErr := os.ReadFile(filepath.Join(installPath, "README.md"))
  125. if nil != readErr {
  126. logging.LogWarnf("read install widget README.md failed: %s", readErr)
  127. continue
  128. }
  129. widget.README, _ = renderREADME(widget.URL, readme)
  130. widget.Outdated = isOutdatedWidget(widget, bazaarWidgets)
  131. ret = append(ret, widget)
  132. }
  133. return
  134. }
  135. func InstallWidget(repoURL, repoHash, installPath string, systemID string) error {
  136. repoURLHash := repoURL + "@" + repoHash
  137. data, err := downloadPackage(repoURLHash, true, systemID)
  138. if nil != err {
  139. return err
  140. }
  141. return installPackage(data, installPath)
  142. }
  143. func UninstallWidget(installPath string) error {
  144. if err := os.RemoveAll(installPath); nil != err {
  145. logging.LogErrorf("remove widget [%s] failed: %s", installPath, err)
  146. return errors.New("remove community widget failed")
  147. }
  148. //logging.Logger.Infof("uninstalled widget [%s]", installPath)
  149. return nil
  150. }