widget.go 5.7 KB

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