plugin.go 7.7 KB

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