plugin.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. "runtime"
  22. "sort"
  23. "strings"
  24. "sync"
  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. Enabled bool `json:"enabled"`
  34. }
  35. func Plugins(frontend string) (plugins []*Plugin) {
  36. plugins = []*Plugin{}
  37. stageIndex, err := getStageIndex("plugins")
  38. if nil != err {
  39. return
  40. }
  41. bazaarIndex := getBazaarIndex()
  42. waitGroup := &sync.WaitGroup{}
  43. lock := &sync.Mutex{}
  44. p, _ := ants.NewPoolWithFunc(8, func(arg interface{}) {
  45. defer waitGroup.Done()
  46. repo := arg.(*StageRepo)
  47. repoURL := repo.URL
  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. if disallowDisplayBazaarPackage(plugin.Package) {
  60. return
  61. }
  62. plugin.Incompatible = isIncompatiblePlugin(plugin, frontend)
  63. plugin.URL = strings.TrimSuffix(plugin.URL, "/")
  64. repoURLHash := strings.Split(repoURL, "@")
  65. plugin.RepoURL = "https://github.com/" + repoURLHash[0]
  66. plugin.RepoHash = repoURLHash[1]
  67. plugin.PreviewURL = util.BazaarOSSServer + "/package/" + repoURL + "/preview.png?imageslim"
  68. plugin.PreviewURLThumb = util.BazaarOSSServer + "/package/" + repoURL + "/preview.png?imageView2/2/w/436/h/232"
  69. plugin.IconURL = util.BazaarOSSServer + "/package/" + repoURL + "/icon.png"
  70. plugin.Funding = repo.Package.Funding
  71. plugin.PreferredFunding = getPreferredFunding(plugin.Funding)
  72. plugin.PreferredName = getPreferredName(plugin.Package)
  73. plugin.PreferredDesc = getPreferredDesc(plugin.Description)
  74. plugin.Updated = repo.Updated
  75. plugin.Stars = repo.Stars
  76. plugin.OpenIssues = repo.OpenIssues
  77. plugin.Size = repo.Size
  78. plugin.HSize = humanize.Bytes(uint64(plugin.Size))
  79. plugin.HUpdated = formatUpdated(plugin.Updated)
  80. pkg := bazaarIndex[strings.Split(repoURL, "@")[0]]
  81. if nil != pkg {
  82. plugin.Downloads = pkg.Downloads
  83. }
  84. lock.Lock()
  85. plugins = append(plugins, plugin)
  86. lock.Unlock()
  87. })
  88. for _, repo := range stageIndex.Repos {
  89. waitGroup.Add(1)
  90. p.Invoke(repo)
  91. }
  92. waitGroup.Wait()
  93. p.Release()
  94. sort.Slice(plugins, func(i, j int) bool { return plugins[i].Updated > plugins[j].Updated })
  95. return
  96. }
  97. func IsIncompatibleInstalledPlugin(name, frontend string) (found, incompatible bool) {
  98. pluginsPath := filepath.Join(util.DataDir, "plugins")
  99. if !util.IsPathRegularDirOrSymlinkDir(pluginsPath) {
  100. return
  101. }
  102. pluginDirs, err := os.ReadDir(pluginsPath)
  103. if nil != err {
  104. logging.LogWarnf("read plugins folder failed: %s", err)
  105. return
  106. }
  107. for _, pluginDir := range pluginDirs {
  108. if !util.IsDirRegularOrSymlink(pluginDir) {
  109. continue
  110. }
  111. dirName := pluginDir.Name()
  112. if name != dirName {
  113. continue
  114. }
  115. plugin, parseErr := PluginJSON(dirName)
  116. if nil != parseErr || nil == plugin {
  117. return
  118. }
  119. found = true
  120. incompatible = isIncompatiblePlugin(plugin, frontend)
  121. }
  122. return
  123. }
  124. func InstalledPlugins(frontend string, checkUpdate bool) (ret []*Plugin) {
  125. ret = []*Plugin{}
  126. pluginsPath := filepath.Join(util.DataDir, "plugins")
  127. if !util.IsPathRegularDirOrSymlinkDir(pluginsPath) {
  128. return
  129. }
  130. pluginDirs, err := os.ReadDir(pluginsPath)
  131. if nil != err {
  132. logging.LogWarnf("read plugins folder failed: %s", err)
  133. return
  134. }
  135. var bazaarPlugins []*Plugin
  136. if checkUpdate {
  137. bazaarPlugins = Plugins(frontend)
  138. }
  139. for _, pluginDir := range pluginDirs {
  140. if !util.IsDirRegularOrSymlink(pluginDir) {
  141. continue
  142. }
  143. dirName := pluginDir.Name()
  144. plugin, parseErr := PluginJSON(dirName)
  145. if nil != parseErr || nil == plugin {
  146. continue
  147. }
  148. installPath := filepath.Join(util.DataDir, "plugins", dirName)
  149. plugin.Installed = true
  150. plugin.RepoURL = plugin.URL
  151. plugin.PreviewURL = "/plugins/" + dirName + "/preview.png"
  152. plugin.PreviewURLThumb = "/plugins/" + dirName + "/preview.png"
  153. plugin.IconURL = "/plugins/" + dirName + "/icon.png"
  154. plugin.PreferredFunding = getPreferredFunding(plugin.Funding)
  155. plugin.PreferredName = getPreferredName(plugin.Package)
  156. plugin.PreferredDesc = getPreferredDesc(plugin.Description)
  157. info, statErr := os.Stat(filepath.Join(installPath, "README.md"))
  158. if nil != statErr {
  159. logging.LogWarnf("stat install theme README.md failed: %s", statErr)
  160. continue
  161. }
  162. plugin.HInstallDate = info.ModTime().Format("2006-01-02")
  163. installSize, _ := util.SizeOfDirectory(installPath)
  164. plugin.InstallSize = installSize
  165. plugin.HInstallSize = humanize.Bytes(uint64(installSize))
  166. readmeFilename := getPreferredReadme(plugin.Readme)
  167. readme, readErr := os.ReadFile(filepath.Join(installPath, readmeFilename))
  168. if nil != readErr {
  169. logging.LogWarnf("read installed README.md failed: %s", readErr)
  170. continue
  171. }
  172. plugin.PreferredReadme, _ = renderREADME(plugin.URL, readme)
  173. plugin.Outdated = isOutdatedPlugin(plugin, bazaarPlugins)
  174. plugin.Incompatible = isIncompatiblePlugin(plugin, frontend)
  175. ret = append(ret, plugin)
  176. }
  177. return
  178. }
  179. func InstallPlugin(repoURL, repoHash, installPath string, systemID string) error {
  180. repoURLHash := repoURL + "@" + repoHash
  181. data, err := downloadPackage(repoURLHash, true, systemID)
  182. if nil != err {
  183. return err
  184. }
  185. return installPackage(data, installPath)
  186. }
  187. func UninstallPlugin(installPath string) error {
  188. if err := os.RemoveAll(installPath); nil != err {
  189. logging.LogErrorf("remove plugin [%s] failed: %s", installPath, err)
  190. return errors.New("remove community plugin failed")
  191. }
  192. //logging.Logger.Infof("uninstalled plugin [%s]", installPath)
  193. return nil
  194. }
  195. func isIncompatiblePlugin(plugin *Plugin, currentFrontend string) bool {
  196. if 1 > len(plugin.Backends) {
  197. return false
  198. }
  199. backendOk := false
  200. for _, backend := range plugin.Backends {
  201. if backend == getCurrentBackend() || "all" == backend {
  202. backendOk = true
  203. break
  204. }
  205. }
  206. frontendOk := false
  207. for _, frontend := range plugin.Frontends {
  208. if frontend == currentFrontend || "all" == frontend {
  209. frontendOk = true
  210. break
  211. }
  212. }
  213. return !backendOk || !frontendOk
  214. }
  215. func getCurrentBackend() string {
  216. switch util.Container {
  217. case util.ContainerDocker:
  218. return "docker"
  219. case util.ContainerIOS:
  220. return "ios"
  221. case util.ContainerAndroid:
  222. return "android"
  223. default:
  224. return runtime.GOOS
  225. }
  226. }