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