icon.go 5.5 KB

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