plugin.go 7.5 KB

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