plugin.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. plugin := &Plugin{}
  50. innerU := util.BazaarOSSServer + "/package/" + repoURL + "/plugin.json"
  51. innerResp, innerErr := httpclient.NewBrowserRequest().SetSuccessResult(plugin).Get(innerU)
  52. if nil != innerErr {
  53. logging.LogErrorf("get bazaar package [%s] failed: %s", repoURL, innerErr)
  54. return
  55. }
  56. if 200 != innerResp.StatusCode {
  57. logging.LogErrorf("get bazaar package [%s] failed: %d", innerU, innerResp.StatusCode)
  58. return
  59. }
  60. if disallowDisplayBazaarPackage(plugin.Package) {
  61. return
  62. }
  63. plugin.Incompatible = isIncompatiblePlugin(plugin, frontend)
  64. plugin.URL = strings.TrimSuffix(plugin.URL, "/")
  65. repoURLHash := strings.Split(repoURL, "@")
  66. plugin.RepoURL = "https://github.com/" + repoURLHash[0]
  67. plugin.RepoHash = repoURLHash[1]
  68. plugin.PreviewURL = util.BazaarOSSServer + "/package/" + repoURL + "/preview.png?imageslim"
  69. plugin.PreviewURLThumb = util.BazaarOSSServer + "/package/" + repoURL + "/preview.png?imageView2/2/w/436/h/232"
  70. plugin.IconURL = util.BazaarOSSServer + "/package/" + repoURL + "/icon.png"
  71. plugin.Funding = repo.Package.Funding
  72. plugin.PreferredFunding = getPreferredFunding(plugin.Funding)
  73. plugin.PreferredName = GetPreferredName(plugin.Package)
  74. plugin.PreferredDesc = getPreferredDesc(plugin.Description)
  75. plugin.Updated = repo.Updated
  76. plugin.Stars = repo.Stars
  77. plugin.OpenIssues = repo.OpenIssues
  78. plugin.Size = repo.Size
  79. plugin.HSize = humanize.Bytes(uint64(plugin.Size))
  80. plugin.HUpdated = formatUpdated(plugin.Updated)
  81. pkg := bazaarIndex[strings.Split(repoURL, "@")[0]]
  82. if nil != pkg {
  83. plugin.Downloads = pkg.Downloads
  84. }
  85. lock.Lock()
  86. plugins = append(plugins, plugin)
  87. lock.Unlock()
  88. })
  89. for _, repo := range stageIndex.Repos {
  90. waitGroup.Add(1)
  91. p.Invoke(repo)
  92. }
  93. waitGroup.Wait()
  94. p.Release()
  95. sort.Slice(plugins, func(i, j int) bool { return plugins[i].Updated > plugins[j].Updated })
  96. return
  97. }
  98. func ParseInstalledPlugin(name, frontend string) (found bool, displayName string, incompatible bool) {
  99. pluginsPath := filepath.Join(util.DataDir, "plugins")
  100. if !util.IsPathRegularDirOrSymlinkDir(pluginsPath) {
  101. return
  102. }
  103. pluginDirs, err := os.ReadDir(pluginsPath)
  104. if nil != err {
  105. logging.LogWarnf("read plugins folder failed: %s", err)
  106. return
  107. }
  108. for _, pluginDir := range pluginDirs {
  109. if !util.IsDirRegularOrSymlink(pluginDir) {
  110. continue
  111. }
  112. dirName := pluginDir.Name()
  113. if name != dirName {
  114. continue
  115. }
  116. plugin, parseErr := PluginJSON(dirName)
  117. if nil != parseErr || nil == plugin {
  118. return
  119. }
  120. found = true
  121. displayName = GetPreferredName(plugin.Package)
  122. incompatible = isIncompatiblePlugin(plugin, frontend)
  123. }
  124. return
  125. }
  126. func InstalledPlugins(frontend string, checkUpdate bool) (ret []*Plugin) {
  127. ret = []*Plugin{}
  128. pluginsPath := filepath.Join(util.DataDir, "plugins")
  129. if !util.IsPathRegularDirOrSymlinkDir(pluginsPath) {
  130. return
  131. }
  132. pluginDirs, err := os.ReadDir(pluginsPath)
  133. if nil != err {
  134. logging.LogWarnf("read plugins folder failed: %s", err)
  135. return
  136. }
  137. var bazaarPlugins []*Plugin
  138. if checkUpdate {
  139. bazaarPlugins = Plugins(frontend)
  140. }
  141. for _, pluginDir := range pluginDirs {
  142. if !util.IsDirRegularOrSymlink(pluginDir) {
  143. continue
  144. }
  145. dirName := pluginDir.Name()
  146. plugin, parseErr := PluginJSON(dirName)
  147. if nil != parseErr || nil == plugin {
  148. continue
  149. }
  150. installPath := filepath.Join(util.DataDir, "plugins", dirName)
  151. plugin.Installed = true
  152. plugin.RepoURL = plugin.URL
  153. plugin.PreviewURL = "/plugins/" + dirName + "/preview.png"
  154. plugin.PreviewURLThumb = "/plugins/" + dirName + "/preview.png"
  155. plugin.IconURL = "/plugins/" + dirName + "/icon.png"
  156. plugin.PreferredFunding = getPreferredFunding(plugin.Funding)
  157. plugin.PreferredName = GetPreferredName(plugin.Package)
  158. plugin.PreferredDesc = getPreferredDesc(plugin.Description)
  159. info, statErr := os.Stat(filepath.Join(installPath, "README.md"))
  160. if nil != statErr {
  161. logging.LogWarnf("stat install theme README.md failed: %s", statErr)
  162. continue
  163. }
  164. plugin.HInstallDate = info.ModTime().Format("2006-01-02")
  165. installSize, _ := util.SizeOfDirectory(installPath)
  166. plugin.InstallSize = installSize
  167. plugin.HInstallSize = humanize.Bytes(uint64(installSize))
  168. readmeFilename := getPreferredReadme(plugin.Readme)
  169. readme, readErr := os.ReadFile(filepath.Join(installPath, readmeFilename))
  170. if nil != readErr {
  171. logging.LogWarnf("read installed README.md failed: %s", readErr)
  172. continue
  173. }
  174. plugin.PreferredReadme, _ = renderREADME(plugin.URL, readme)
  175. plugin.Outdated = isOutdatedPlugin(plugin, bazaarPlugins)
  176. plugin.Incompatible = isIncompatiblePlugin(plugin, frontend)
  177. ret = append(ret, plugin)
  178. }
  179. return
  180. }
  181. func InstallPlugin(repoURL, repoHash, installPath string, systemID string) error {
  182. repoURLHash := repoURL + "@" + repoHash
  183. data, err := downloadPackage(repoURLHash, true, systemID)
  184. if nil != err {
  185. return err
  186. }
  187. return installPackage(data, installPath)
  188. }
  189. func UninstallPlugin(installPath string) error {
  190. if err := filelock.Remove(installPath); nil != err {
  191. logging.LogErrorf("remove plugin [%s] failed: %s", installPath, err)
  192. return errors.New("remove community plugin failed")
  193. }
  194. //logging.Logger.Infof("uninstalled plugin [%s]", installPath)
  195. return nil
  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. }