theme.go 6.0 KB

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