plugin.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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.HUpdated = formatUpdated(plugin.Updated)
  85. pkg := bazaarIndex[strings.Split(repoURL, "@")[0]]
  86. if nil != pkg {
  87. plugin.Downloads = pkg.Downloads
  88. }
  89. lock.Lock()
  90. plugins = append(plugins, plugin)
  91. lock.Unlock()
  92. packageCache.SetDefault(repoURL, plugin)
  93. })
  94. for _, repo := range stageIndex.Repos {
  95. waitGroup.Add(1)
  96. p.Invoke(repo)
  97. }
  98. waitGroup.Wait()
  99. p.Release()
  100. sort.Slice(plugins, func(i, j int) bool { return plugins[i].Updated > plugins[j].Updated })
  101. return
  102. }
  103. func ParseInstalledPlugin(name, frontend string) (found bool, displayName string, incompatible bool) {
  104. pluginsPath := filepath.Join(util.DataDir, "plugins")
  105. if !util.IsPathRegularDirOrSymlinkDir(pluginsPath) {
  106. return
  107. }
  108. pluginDirs, err := os.ReadDir(pluginsPath)
  109. if nil != err {
  110. logging.LogWarnf("read plugins folder failed: %s", err)
  111. return
  112. }
  113. for _, pluginDir := range pluginDirs {
  114. if !util.IsDirRegularOrSymlink(pluginDir) {
  115. continue
  116. }
  117. dirName := pluginDir.Name()
  118. if name != dirName {
  119. continue
  120. }
  121. plugin, parseErr := PluginJSON(dirName)
  122. if nil != parseErr || nil == plugin {
  123. return
  124. }
  125. found = true
  126. displayName = GetPreferredName(plugin.Package)
  127. incompatible = isIncompatiblePlugin(plugin, frontend)
  128. }
  129. return
  130. }
  131. func InstalledPlugins(frontend string, checkUpdate bool) (ret []*Plugin) {
  132. ret = []*Plugin{}
  133. pluginsPath := filepath.Join(util.DataDir, "plugins")
  134. if !util.IsPathRegularDirOrSymlinkDir(pluginsPath) {
  135. return
  136. }
  137. pluginDirs, err := os.ReadDir(pluginsPath)
  138. if nil != err {
  139. logging.LogWarnf("read plugins folder failed: %s", err)
  140. return
  141. }
  142. var bazaarPlugins []*Plugin
  143. if checkUpdate {
  144. bazaarPlugins = Plugins(frontend)
  145. }
  146. for _, pluginDir := range pluginDirs {
  147. if !util.IsDirRegularOrSymlink(pluginDir) {
  148. continue
  149. }
  150. dirName := pluginDir.Name()
  151. plugin, parseErr := PluginJSON(dirName)
  152. if nil != parseErr || nil == plugin {
  153. continue
  154. }
  155. installPath := filepath.Join(util.DataDir, "plugins", dirName)
  156. plugin.Installed = true
  157. plugin.RepoURL = plugin.URL
  158. plugin.PreviewURL = "/plugins/" + dirName + "/preview.png"
  159. plugin.PreviewURLThumb = "/plugins/" + dirName + "/preview.png"
  160. plugin.IconURL = "/plugins/" + dirName + "/icon.png"
  161. plugin.PreferredFunding = getPreferredFunding(plugin.Funding)
  162. plugin.PreferredName = GetPreferredName(plugin.Package)
  163. plugin.PreferredDesc = getPreferredDesc(plugin.Description)
  164. info, statErr := os.Stat(filepath.Join(installPath, "README.md"))
  165. if nil != statErr {
  166. logging.LogWarnf("stat install theme README.md failed: %s", statErr)
  167. continue
  168. }
  169. plugin.HInstallDate = info.ModTime().Format("2006-01-02")
  170. installSize, _ := util.SizeOfDirectory(installPath)
  171. plugin.InstallSize = installSize
  172. plugin.HInstallSize = humanize.BytesCustomCeil(uint64(installSize), 2)
  173. readmeFilename := getPreferredReadme(plugin.Readme)
  174. readme, readErr := os.ReadFile(filepath.Join(installPath, readmeFilename))
  175. if nil != readErr {
  176. logging.LogWarnf("read installed README.md failed: %s", readErr)
  177. continue
  178. }
  179. plugin.PreferredReadme, _ = renderREADME(plugin.URL, readme)
  180. plugin.Outdated = isOutdatedPlugin(plugin, bazaarPlugins)
  181. plugin.Incompatible = isIncompatiblePlugin(plugin, frontend)
  182. ret = append(ret, plugin)
  183. }
  184. return
  185. }
  186. func InstallPlugin(repoURL, repoHash, installPath string, systemID string) error {
  187. repoURLHash := repoURL + "@" + repoHash
  188. data, err := downloadPackage(repoURLHash, true, systemID)
  189. if nil != err {
  190. return err
  191. }
  192. return installPackage(data, installPath, repoURLHash)
  193. }
  194. func UninstallPlugin(installPath string) error {
  195. return uninstallPackage(installPath)
  196. }
  197. func isIncompatiblePlugin(plugin *Plugin, currentFrontend string) bool {
  198. if 1 > len(plugin.Backends) {
  199. return false
  200. }
  201. backendOk := false
  202. for _, backend := range plugin.Backends {
  203. if backend == getCurrentBackend() || "all" == backend {
  204. backendOk = true
  205. break
  206. }
  207. }
  208. frontendOk := false
  209. for _, frontend := range plugin.Frontends {
  210. if frontend == currentFrontend || "all" == frontend {
  211. frontendOk = true
  212. break
  213. }
  214. }
  215. return !backendOk || !frontendOk
  216. }
  217. func getCurrentBackend() string {
  218. switch util.Container {
  219. case util.ContainerDocker:
  220. return "docker"
  221. case util.ContainerIOS:
  222. return "ios"
  223. case util.ContainerAndroid:
  224. return "android"
  225. default:
  226. return runtime.GOOS
  227. }
  228. }