widget.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. package bazaar
  17. import (
  18. "os"
  19. "path/filepath"
  20. "sort"
  21. "strings"
  22. "sync"
  23. "github.com/88250/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. *Package
  31. }
  32. func Widgets() (widgets []*Widget) {
  33. widgets = []*Widget{}
  34. isOnline := isBazzarOnline()
  35. if !isOnline {
  36. return
  37. }
  38. stageIndex, err := getStageIndex("widgets")
  39. if nil != err {
  40. return
  41. }
  42. bazaarIndex := getBazaarIndex()
  43. waitGroup := &sync.WaitGroup{}
  44. lock := &sync.Mutex{}
  45. p, _ := ants.NewPoolWithFunc(8, func(arg interface{}) {
  46. defer waitGroup.Done()
  47. repo := arg.(*StageRepo)
  48. repoURL := repo.URL
  49. if pkg, found := packageCache.Get(repoURL); found {
  50. lock.Lock()
  51. widgets = append(widgets, pkg.(*Widget))
  52. lock.Unlock()
  53. return
  54. }
  55. widget := &Widget{}
  56. innerU := util.BazaarOSSServer + "/package/" + repoURL + "/widget.json"
  57. innerResp, innerErr := httpclient.NewBrowserRequest().SetSuccessResult(widget).Get(innerU)
  58. if nil != innerErr {
  59. logging.LogErrorf("get bazaar package [%s] failed: %s", repoURL, innerErr)
  60. return
  61. }
  62. if 200 != innerResp.StatusCode {
  63. logging.LogErrorf("get bazaar package [%s] failed: %d", innerU, innerResp.StatusCode)
  64. return
  65. }
  66. if disallowDisplayBazaarPackage(widget.Package) {
  67. return
  68. }
  69. widget.URL = strings.TrimSuffix(widget.URL, "/")
  70. repoURLHash := strings.Split(repoURL, "@")
  71. widget.RepoURL = "https://github.com/" + repoURLHash[0]
  72. widget.RepoHash = repoURLHash[1]
  73. widget.PreviewURL = util.BazaarOSSServer + "/package/" + repoURL + "/preview.png?imageslim"
  74. widget.PreviewURLThumb = util.BazaarOSSServer + "/package/" + repoURL + "/preview.png?imageView2/2/w/436/h/232"
  75. widget.IconURL = util.BazaarOSSServer + "/package/" + repoURL + "/icon.png"
  76. widget.Funding = repo.Package.Funding
  77. widget.PreferredFunding = getPreferredFunding(widget.Funding)
  78. widget.PreferredName = GetPreferredName(widget.Package)
  79. widget.PreferredDesc = getPreferredDesc(widget.Description)
  80. widget.Updated = repo.Updated
  81. widget.Stars = repo.Stars
  82. widget.OpenIssues = repo.OpenIssues
  83. widget.Size = repo.Size
  84. widget.HSize = humanize.BytesCustomCeil(uint64(widget.Size), 2)
  85. widget.InstallSize = repo.InstallSize
  86. widget.HInstallSize = humanize.BytesCustomCeil(uint64(widget.InstallSize), 2)
  87. packageInstallSizeCache.SetDefault(widget.RepoURL, widget.InstallSize)
  88. widget.HUpdated = formatUpdated(widget.Updated)
  89. pkg := bazaarIndex[strings.Split(repoURL, "@")[0]]
  90. if nil != pkg {
  91. widget.Downloads = pkg.Downloads
  92. }
  93. lock.Lock()
  94. widgets = append(widgets, widget)
  95. lock.Unlock()
  96. packageCache.SetDefault(repoURL, widget)
  97. })
  98. for _, repo := range stageIndex.Repos {
  99. waitGroup.Add(1)
  100. p.Invoke(repo)
  101. }
  102. waitGroup.Wait()
  103. p.Release()
  104. sort.Slice(widgets, func(i, j int) bool { return widgets[i].Updated > widgets[j].Updated })
  105. return
  106. }
  107. func InstalledWidgets() (ret []*Widget) {
  108. ret = []*Widget{}
  109. widgetsPath := filepath.Join(util.DataDir, "widgets")
  110. if !util.IsPathRegularDirOrSymlinkDir(widgetsPath) {
  111. return
  112. }
  113. widgetDirs, err := os.ReadDir(widgetsPath)
  114. if nil != err {
  115. logging.LogWarnf("read widgets folder failed: %s", err)
  116. return
  117. }
  118. bazaarWidgets := Widgets()
  119. for _, widgetDir := range widgetDirs {
  120. if !util.IsDirRegularOrSymlink(widgetDir) {
  121. continue
  122. }
  123. dirName := widgetDir.Name()
  124. widget, parseErr := WidgetJSON(dirName)
  125. if nil != parseErr || nil == widget {
  126. continue
  127. }
  128. installPath := filepath.Join(util.DataDir, "widgets", dirName)
  129. widget.Installed = true
  130. widget.RepoURL = widget.URL
  131. widget.PreviewURL = "/widgets/" + dirName + "/preview.png"
  132. widget.PreviewURLThumb = "/widgets/" + dirName + "/preview.png"
  133. widget.IconURL = "/widgets/" + dirName + "/icon.png"
  134. widget.PreferredFunding = getPreferredFunding(widget.Funding)
  135. widget.PreferredName = GetPreferredName(widget.Package)
  136. widget.PreferredDesc = getPreferredDesc(widget.Description)
  137. info, statErr := os.Stat(filepath.Join(installPath, "README.md"))
  138. if nil != statErr {
  139. logging.LogWarnf("stat install theme README.md failed: %s", statErr)
  140. continue
  141. }
  142. widget.HInstallDate = info.ModTime().Format("2006-01-02")
  143. if installSize, ok := packageInstallSizeCache.Get(widget.RepoURL); ok {
  144. widget.InstallSize = installSize.(int64)
  145. } else {
  146. is, _ := util.SizeOfDirectory(installPath)
  147. widget.InstallSize = is
  148. packageInstallSizeCache.SetDefault(widget.RepoURL, is)
  149. }
  150. widget.HInstallSize = humanize.BytesCustomCeil(uint64(widget.InstallSize), 2)
  151. readmeFilename := getPreferredReadme(widget.Readme)
  152. readme, readErr := os.ReadFile(filepath.Join(installPath, readmeFilename))
  153. if nil != readErr {
  154. logging.LogWarnf("read installed README.md failed: %s", readErr)
  155. continue
  156. }
  157. widget.PreferredReadme, _ = renderREADME(widget.URL, readme)
  158. widget.Outdated = isOutdatedWidget(widget, bazaarWidgets)
  159. ret = append(ret, widget)
  160. }
  161. return
  162. }
  163. func InstallWidget(repoURL, repoHash, installPath string, systemID string) error {
  164. repoURLHash := repoURL + "@" + repoHash
  165. data, err := downloadPackage(repoURLHash, true, systemID)
  166. if nil != err {
  167. return err
  168. }
  169. return installPackage(data, installPath, repoURLHash)
  170. }
  171. func UninstallWidget(installPath string) error {
  172. return uninstallPackage(installPath)
  173. }