plugin.go 5.8 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 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{Package{Funding: &Funding{}, Description: &Description{}}}
  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.Funding = parseFunding(repo["package"].(map[string]interface{}))
  67. plugin.PreferredFunding = getPreferredFunding(plugin.Funding)
  68. plugin.PreferredDesc = getPreferredDesc(plugin.Description)
  69. plugin.Updated = repo["updated"].(string)
  70. plugin.Stars = int(repo["stars"].(float64))
  71. plugin.OpenIssues = int(repo["openIssues"].(float64))
  72. plugin.Size = int64(repo["size"].(float64))
  73. plugin.HSize = humanize.Bytes(uint64(plugin.Size))
  74. plugin.HUpdated = formatUpdated(plugin.Updated)
  75. pkg := bazaarIndex[strings.Split(repoURL, "@")[0]]
  76. if nil != pkg {
  77. plugin.Downloads = pkg.Downloads
  78. }
  79. lock.Lock()
  80. plugins = append(plugins, plugin)
  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(plugins, func(i, j int) bool { return plugins[i].Updated > plugins[j].Updated })
  90. return
  91. }
  92. func InstalledPlugins() (ret []*Plugin) {
  93. ret = []*Plugin{}
  94. pluginsPath := filepath.Join(util.DataDir, "plugins")
  95. if !gulu.File.IsDir(pluginsPath) {
  96. return
  97. }
  98. pluginDirs, err := os.ReadDir(pluginsPath)
  99. if nil != err {
  100. logging.LogWarnf("read plugins folder failed: %s", err)
  101. return
  102. }
  103. bazaarPlugins := Plugins()
  104. for _, pluginDir := range pluginDirs {
  105. if !pluginDir.IsDir() {
  106. continue
  107. }
  108. dirName := pluginDir.Name()
  109. pluginConf, parseErr := PluginJSON(dirName)
  110. if nil != parseErr || nil == pluginConf {
  111. continue
  112. }
  113. installPath := filepath.Join(util.DataDir, "plugins", dirName)
  114. plugin := &Plugin{}
  115. plugin.Installed = true
  116. plugin.Name = pluginConf["name"].(string)
  117. plugin.Author = pluginConf["author"].(string)
  118. plugin.URL = pluginConf["url"].(string)
  119. plugin.URL = strings.TrimSuffix(plugin.URL, "/")
  120. plugin.Version = pluginConf["version"].(string)
  121. plugin.RepoURL = plugin.URL
  122. plugin.PreviewURL = "/plugins/" + dirName + "/preview.png"
  123. plugin.PreviewURLThumb = "/plugins/" + dirName + "/preview.png"
  124. plugin.IconURL = "/plugins/" + dirName + "/icon.png"
  125. plugin.Funding = parseFunding(pluginConf)
  126. plugin.PreferredFunding = getPreferredFunding(plugin.Funding)
  127. plugin.PreferredDesc = getPreferredDesc(plugin.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. plugin.HInstallDate = info.ModTime().Format("2006-01-02")
  134. installSize, _ := util.SizeOfDirectory(installPath)
  135. plugin.InstallSize = installSize
  136. plugin.HInstallSize = humanize.Bytes(uint64(installSize))
  137. readme, readErr := os.ReadFile(filepath.Join(installPath, "README.md"))
  138. if nil != readErr {
  139. logging.LogWarnf("read install plugin README.md failed: %s", readErr)
  140. continue
  141. }
  142. plugin.README, _ = renderREADME(plugin.URL, readme)
  143. plugin.Outdated = isOutdatedPlugin(plugin, bazaarPlugins)
  144. ret = append(ret, plugin)
  145. }
  146. return
  147. }
  148. func InstallPlugin(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 UninstallPlugin(installPath string) error {
  157. if err := os.RemoveAll(installPath); nil != err {
  158. logging.LogErrorf("remove plugin [%s] failed: %s", installPath, err)
  159. return errors.New("remove community plugin failed")
  160. }
  161. //logging.Logger.Infof("uninstalled plugin [%s]", installPath)
  162. return nil
  163. }