plugin.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // SiYuan - Refactor your thinking
  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. "os"
  19. "path/filepath"
  20. "runtime"
  21. "sort"
  22. "strings"
  23. "sync"
  24. "github.com/88250/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(frontend string) (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. if pkg, found := packageCache.Get(repoURL); found {
  48. lock.Lock()
  49. plugins = append(plugins, pkg.(*Plugin))
  50. lock.Unlock()
  51. return
  52. }
  53. plugin := &Plugin{}
  54. innerU := util.BazaarOSSServer + "/package/" + repoURL + "/plugin.json"
  55. innerResp, innerErr := httpclient.NewBrowserRequest().SetSuccessResult(plugin).Get(innerU)
  56. if nil != innerErr {
  57. logging.LogErrorf("get bazaar package [%s] failed: %s", repoURL, innerErr)
  58. return
  59. }
  60. if 200 != innerResp.StatusCode {
  61. logging.LogErrorf("get bazaar package [%s] failed: %d", innerU, innerResp.StatusCode)
  62. return
  63. }
  64. if disallowDisplayBazaarPackage(plugin.Package) {
  65. return
  66. }
  67. plugin.Incompatible = isIncompatiblePlugin(plugin, frontend)
  68. plugin.URL = strings.TrimSuffix(plugin.URL, "/")
  69. repoURLHash := strings.Split(repoURL, "@")
  70. plugin.RepoURL = "https://github.com/" + repoURLHash[0]
  71. plugin.RepoHash = repoURLHash[1]
  72. plugin.PreviewURL = util.BazaarOSSServer + "/package/" + repoURL + "/preview.png?imageslim"
  73. plugin.PreviewURLThumb = util.BazaarOSSServer + "/package/" + repoURL + "/preview.png?imageView2/2/w/436/h/232"
  74. plugin.IconURL = util.BazaarOSSServer + "/package/" + repoURL + "/icon.png"
  75. plugin.Funding = repo.Package.Funding
  76. plugin.PreferredFunding = getPreferredFunding(plugin.Funding)
  77. plugin.PreferredName = GetPreferredName(plugin.Package)
  78. plugin.PreferredDesc = getPreferredDesc(plugin.Description)
  79. plugin.Updated = repo.Updated
  80. plugin.Stars = repo.Stars
  81. plugin.OpenIssues = repo.OpenIssues
  82. plugin.Size = repo.Size
  83. plugin.HSize = humanize.BytesCustomCeil(uint64(plugin.Size), 2)
  84. plugin.InstallSize = repo.InstallSize
  85. plugin.HInstallSize = humanize.BytesCustomCeil(uint64(plugin.InstallSize), 2)
  86. packageInstallSizeCache.SetDefault(plugin.RepoURL, plugin.InstallSize)
  87. plugin.HUpdated = formatUpdated(plugin.Updated)
  88. pkg := bazaarIndex[strings.Split(repoURL, "@")[0]]
  89. if nil != pkg {
  90. plugin.Downloads = pkg.Downloads
  91. }
  92. lock.Lock()
  93. plugins = append(plugins, plugin)
  94. lock.Unlock()
  95. packageCache.SetDefault(repoURL, plugin)
  96. })
  97. for _, repo := range stageIndex.Repos {
  98. waitGroup.Add(1)
  99. p.Invoke(repo)
  100. }
  101. waitGroup.Wait()
  102. p.Release()
  103. sort.Slice(plugins, func(i, j int) bool { return plugins[i].Updated > plugins[j].Updated })
  104. return
  105. }
  106. func ParseInstalledPlugin(name, frontend string) (found bool, displayName string, incompatible bool) {
  107. pluginsPath := filepath.Join(util.DataDir, "plugins")
  108. if !util.IsPathRegularDirOrSymlinkDir(pluginsPath) {
  109. return
  110. }
  111. pluginDirs, err := os.ReadDir(pluginsPath)
  112. if nil != err {
  113. logging.LogWarnf("read plugins folder failed: %s", err)
  114. return
  115. }
  116. for _, pluginDir := range pluginDirs {
  117. if !util.IsDirRegularOrSymlink(pluginDir) {
  118. continue
  119. }
  120. dirName := pluginDir.Name()
  121. if name != dirName {
  122. continue
  123. }
  124. plugin, parseErr := PluginJSON(dirName)
  125. if nil != parseErr || nil == plugin {
  126. return
  127. }
  128. found = true
  129. displayName = GetPreferredName(plugin.Package)
  130. incompatible = isIncompatiblePlugin(plugin, frontend)
  131. }
  132. return
  133. }
  134. func InstalledPlugins(frontend string, checkUpdate bool) (ret []*Plugin) {
  135. ret = []*Plugin{}
  136. pluginsPath := filepath.Join(util.DataDir, "plugins")
  137. if !util.IsPathRegularDirOrSymlinkDir(pluginsPath) {
  138. return
  139. }
  140. pluginDirs, err := os.ReadDir(pluginsPath)
  141. if nil != err {
  142. logging.LogWarnf("read plugins folder failed: %s", err)
  143. return
  144. }
  145. var bazaarPlugins []*Plugin
  146. if checkUpdate {
  147. bazaarPlugins = Plugins(frontend)
  148. }
  149. for _, pluginDir := range pluginDirs {
  150. if !util.IsDirRegularOrSymlink(pluginDir) {
  151. continue
  152. }
  153. dirName := pluginDir.Name()
  154. plugin, parseErr := PluginJSON(dirName)
  155. if nil != parseErr || nil == plugin {
  156. continue
  157. }
  158. installPath := filepath.Join(util.DataDir, "plugins", dirName)
  159. plugin.Installed = true
  160. plugin.RepoURL = plugin.URL
  161. plugin.PreviewURL = "/plugins/" + dirName + "/preview.png"
  162. plugin.PreviewURLThumb = "/plugins/" + dirName + "/preview.png"
  163. plugin.IconURL = "/plugins/" + dirName + "/icon.png"
  164. plugin.PreferredFunding = getPreferredFunding(plugin.Funding)
  165. plugin.PreferredName = GetPreferredName(plugin.Package)
  166. plugin.PreferredDesc = getPreferredDesc(plugin.Description)
  167. info, statErr := os.Stat(filepath.Join(installPath, "README.md"))
  168. if nil != statErr {
  169. logging.LogWarnf("stat install theme README.md failed: %s", statErr)
  170. continue
  171. }
  172. plugin.HInstallDate = info.ModTime().Format("2006-01-02")
  173. if installSize, ok := packageInstallSizeCache.Get(plugin.RepoURL); ok {
  174. plugin.InstallSize = installSize.(int64)
  175. } else {
  176. is, _ := util.SizeOfDirectory(installPath)
  177. plugin.InstallSize = is
  178. packageInstallSizeCache.SetDefault(plugin.RepoURL, is)
  179. }
  180. plugin.HInstallSize = humanize.BytesCustomCeil(uint64(plugin.InstallSize), 2)
  181. readmeFilename := getPreferredReadme(plugin.Readme)
  182. readme, readErr := os.ReadFile(filepath.Join(installPath, readmeFilename))
  183. if nil != readErr {
  184. logging.LogWarnf("read installed README.md failed: %s", readErr)
  185. continue
  186. }
  187. plugin.PreferredReadme, _ = renderREADME(plugin.URL, readme)
  188. plugin.Outdated = isOutdatedPlugin(plugin, bazaarPlugins)
  189. plugin.Incompatible = isIncompatiblePlugin(plugin, frontend)
  190. ret = append(ret, plugin)
  191. }
  192. return
  193. }
  194. func InstallPlugin(repoURL, repoHash, installPath string, systemID string) error {
  195. repoURLHash := repoURL + "@" + repoHash
  196. data, err := downloadPackage(repoURLHash, true, systemID)
  197. if nil != err {
  198. return err
  199. }
  200. return installPackage(data, installPath, repoURLHash)
  201. }
  202. func UninstallPlugin(installPath string) error {
  203. return uninstallPackage(installPath)
  204. }
  205. func isIncompatiblePlugin(plugin *Plugin, currentFrontend string) bool {
  206. if 1 > len(plugin.Backends) {
  207. return false
  208. }
  209. backendOk := false
  210. for _, backend := range plugin.Backends {
  211. if backend == getCurrentBackend() || "all" == backend {
  212. backendOk = true
  213. break
  214. }
  215. }
  216. frontendOk := false
  217. for _, frontend := range plugin.Frontends {
  218. if frontend == currentFrontend || "all" == frontend {
  219. frontendOk = true
  220. break
  221. }
  222. }
  223. return !backendOk || !frontendOk
  224. }
  225. func getCurrentBackend() string {
  226. switch util.Container {
  227. case util.ContainerDocker:
  228. return "docker"
  229. case util.ContainerIOS:
  230. return "ios"
  231. case util.ContainerAndroid:
  232. return "android"
  233. default:
  234. return runtime.GOOS
  235. }
  236. }