plugin.go 5.4 KB

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