plugin.go 5.3 KB

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