widget.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.Bytes(uint64(widget.Size))
  81. widget.HUpdated = formatUpdated(widget.Updated)
  82. pkg := bazaarIndex[strings.Split(repoURL, "@")[0]]
  83. if nil != pkg {
  84. widget.Downloads = pkg.Downloads
  85. }
  86. lock.Lock()
  87. widgets = append(widgets, widget)
  88. lock.Unlock()
  89. packageCache.SetDefault(repoURL, widget)
  90. })
  91. for _, repo := range stageIndex.Repos {
  92. waitGroup.Add(1)
  93. p.Invoke(repo)
  94. }
  95. waitGroup.Wait()
  96. p.Release()
  97. sort.Slice(widgets, func(i, j int) bool { return widgets[i].Updated > widgets[j].Updated })
  98. return
  99. }
  100. func InstalledWidgets() (ret []*Widget) {
  101. ret = []*Widget{}
  102. widgetsPath := filepath.Join(util.DataDir, "widgets")
  103. if !util.IsPathRegularDirOrSymlinkDir(widgetsPath) {
  104. return
  105. }
  106. widgetDirs, err := os.ReadDir(widgetsPath)
  107. if nil != err {
  108. logging.LogWarnf("read widgets folder failed: %s", err)
  109. return
  110. }
  111. bazaarWidgets := Widgets()
  112. for _, widgetDir := range widgetDirs {
  113. if !util.IsDirRegularOrSymlink(widgetDir) {
  114. continue
  115. }
  116. dirName := widgetDir.Name()
  117. widget, parseErr := WidgetJSON(dirName)
  118. if nil != parseErr || nil == widget {
  119. continue
  120. }
  121. installPath := filepath.Join(util.DataDir, "widgets", dirName)
  122. widget.Installed = true
  123. widget.RepoURL = widget.URL
  124. widget.PreviewURL = "/widgets/" + dirName + "/preview.png"
  125. widget.PreviewURLThumb = "/widgets/" + dirName + "/preview.png"
  126. widget.IconURL = "/widgets/" + dirName + "/icon.png"
  127. widget.PreferredFunding = getPreferredFunding(widget.Funding)
  128. widget.PreferredName = GetPreferredName(widget.Package)
  129. widget.PreferredDesc = getPreferredDesc(widget.Description)
  130. info, statErr := os.Stat(filepath.Join(installPath, "README.md"))
  131. if nil != statErr {
  132. logging.LogWarnf("stat install theme README.md failed: %s", statErr)
  133. continue
  134. }
  135. widget.HInstallDate = info.ModTime().Format("2006-01-02")
  136. installSize, _ := util.SizeOfDirectory(installPath)
  137. widget.InstallSize = installSize
  138. widget.HInstallSize = humanize.Bytes(uint64(installSize))
  139. readmeFilename := getPreferredReadme(widget.Readme)
  140. readme, readErr := os.ReadFile(filepath.Join(installPath, readmeFilename))
  141. if nil != readErr {
  142. logging.LogWarnf("read installed README.md failed: %s", readErr)
  143. continue
  144. }
  145. widget.PreferredReadme, _ = renderREADME(widget.URL, readme)
  146. widget.Outdated = isOutdatedWidget(widget, bazaarWidgets)
  147. ret = append(ret, widget)
  148. }
  149. return
  150. }
  151. func InstallWidget(repoURL, repoHash, installPath string, systemID string) error {
  152. repoURLHash := repoURL + "@" + repoHash
  153. data, err := downloadPackage(repoURLHash, true, systemID)
  154. if nil != err {
  155. return err
  156. }
  157. return installPackage(data, installPath, repoURLHash)
  158. }
  159. func UninstallWidget(installPath string) error {
  160. return uninstallPackage(installPath)
  161. }