icon.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. "sort"
  21. "strings"
  22. "sync"
  23. "github.com/88250/go-humanize"
  24. ants "github.com/panjf2000/ants/v2"
  25. "github.com/siyuan-note/httpclient"
  26. "github.com/siyuan-note/logging"
  27. "github.com/siyuan-note/siyuan/kernel/util"
  28. )
  29. type Icon struct {
  30. *Package
  31. }
  32. func Icons() (icons []*Icon) {
  33. icons = []*Icon{}
  34. stageIndex, err := getStageIndex("icons")
  35. if nil != err {
  36. return
  37. }
  38. bazaarIndex := getBazaarIndex()
  39. waitGroup := &sync.WaitGroup{}
  40. lock := &sync.Mutex{}
  41. p, _ := ants.NewPoolWithFunc(2, func(arg interface{}) {
  42. defer waitGroup.Done()
  43. repo := arg.(*StageRepo)
  44. repoURL := repo.URL
  45. if pkg, found := packageCache.Get(repoURL); found {
  46. lock.Lock()
  47. icons = append(icons, pkg.(*Icon))
  48. lock.Unlock()
  49. return
  50. }
  51. icon := &Icon{}
  52. innerU := util.BazaarOSSServer + "/package/" + repoURL + "/icon.json"
  53. innerResp, innerErr := httpclient.NewBrowserRequest().SetSuccessResult(icon).Get(innerU)
  54. if nil != innerErr {
  55. logging.LogErrorf("get bazaar package [%s] failed: %s", repoURL, innerErr)
  56. return
  57. }
  58. if 200 != innerResp.StatusCode {
  59. logging.LogErrorf("get bazaar package [%s] failed: %d", innerU, innerResp.StatusCode)
  60. return
  61. }
  62. if disallowDisplayBazaarPackage(icon.Package) {
  63. return
  64. }
  65. icon.URL = strings.TrimSuffix(icon.URL, "/")
  66. repoURLHash := strings.Split(repoURL, "@")
  67. icon.RepoURL = "https://github.com/" + repoURLHash[0]
  68. icon.RepoHash = repoURLHash[1]
  69. icon.PreviewURL = util.BazaarOSSServer + "/package/" + repoURL + "/preview.png?imageslim"
  70. icon.PreviewURLThumb = util.BazaarOSSServer + "/package/" + repoURL + "/preview.png?imageView2/2/w/436/h/232"
  71. icon.IconURL = util.BazaarOSSServer + "/package/" + repoURL + "/icon.png"
  72. icon.Funding = repo.Package.Funding
  73. icon.PreferredFunding = getPreferredFunding(icon.Funding)
  74. icon.PreferredName = GetPreferredName(icon.Package)
  75. icon.PreferredDesc = getPreferredDesc(icon.Description)
  76. icon.Updated = repo.Updated
  77. icon.Stars = repo.Stars
  78. icon.OpenIssues = repo.OpenIssues
  79. icon.Size = repo.Size
  80. icon.HSize = humanize.BytesCustomCeil(uint64(icon.Size), 2)
  81. icon.InstallSize = repo.InstallSize
  82. icon.HInstallSize = humanize.BytesCustomCeil(uint64(icon.InstallSize), 2)
  83. packageInstallSizeCache.SetDefault(icon.RepoURL, icon.InstallSize)
  84. icon.HUpdated = formatUpdated(icon.Updated)
  85. pkg := bazaarIndex[strings.Split(repoURL, "@")[0]]
  86. if nil != pkg {
  87. icon.Downloads = pkg.Downloads
  88. }
  89. lock.Lock()
  90. icons = append(icons, icon)
  91. lock.Unlock()
  92. packageCache.SetDefault(repoURL, icon)
  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(icons, func(i, j int) bool { return icons[i].Updated > icons[j].Updated })
  101. return
  102. }
  103. func InstalledIcons() (ret []*Icon) {
  104. ret = []*Icon{}
  105. if !util.IsPathRegularDirOrSymlinkDir(util.IconsPath) {
  106. return
  107. }
  108. iconDirs, err := os.ReadDir(util.IconsPath)
  109. if nil != err {
  110. logging.LogWarnf("read icons folder failed: %s", err)
  111. return
  112. }
  113. bazaarIcons := Icons()
  114. for _, iconDir := range iconDirs {
  115. if !util.IsDirRegularOrSymlink(iconDir) {
  116. continue
  117. }
  118. dirName := iconDir.Name()
  119. if isBuiltInIcon(dirName) {
  120. continue
  121. }
  122. icon, parseErr := IconJSON(dirName)
  123. if nil != parseErr || nil == icon {
  124. continue
  125. }
  126. installPath := filepath.Join(util.IconsPath, dirName)
  127. icon.Installed = true
  128. icon.RepoURL = icon.URL
  129. icon.PreviewURL = "/appearance/icons/" + dirName + "/preview.png"
  130. icon.PreviewURLThumb = "/appearance/icons/" + dirName + "/preview.png"
  131. icon.IconURL = "/appearance/icons/" + dirName + "/icon.png"
  132. icon.PreferredFunding = getPreferredFunding(icon.Funding)
  133. icon.PreferredName = GetPreferredName(icon.Package)
  134. icon.PreferredDesc = getPreferredDesc(icon.Description)
  135. info, statErr := os.Stat(filepath.Join(installPath, "README.md"))
  136. if nil != statErr {
  137. logging.LogWarnf("stat install theme README.md failed: %s", statErr)
  138. continue
  139. }
  140. icon.HInstallDate = info.ModTime().Format("2006-01-02")
  141. if installSize, ok := packageInstallSizeCache.Get(icon.RepoURL); ok {
  142. icon.InstallSize = installSize.(int64)
  143. } else {
  144. is, _ := util.SizeOfDirectory(installPath)
  145. icon.InstallSize = is
  146. packageInstallSizeCache.SetDefault(icon.RepoURL, is)
  147. }
  148. icon.HInstallSize = humanize.BytesCustomCeil(uint64(icon.InstallSize), 2)
  149. readmeFilename := getPreferredReadme(icon.Readme)
  150. readme, readErr := os.ReadFile(filepath.Join(installPath, readmeFilename))
  151. if nil != readErr {
  152. logging.LogWarnf("read installed README.md failed: %s", readErr)
  153. continue
  154. }
  155. icon.PreferredReadme, _ = renderREADME(icon.URL, readme)
  156. icon.Outdated = isOutdatedIcon(icon, bazaarIcons)
  157. ret = append(ret, icon)
  158. }
  159. return
  160. }
  161. func isBuiltInIcon(dirName string) bool {
  162. return "ant" == dirName || "material" == dirName
  163. }
  164. func InstallIcon(repoURL, repoHash, installPath string, systemID string) error {
  165. repoURLHash := repoURL + "@" + repoHash
  166. data, err := downloadPackage(repoURLHash, true, systemID)
  167. if nil != err {
  168. return err
  169. }
  170. return installPackage(data, installPath, repoURLHash)
  171. }
  172. func UninstallIcon(installPath string) error {
  173. return uninstallPackage(installPath)
  174. }