widget.go 5.9 KB

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