theme.go 5.9 KB

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