theme.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.HUpdated = formatUpdated(theme.Updated)
  83. pkg := bazaarIndex[strings.Split(repoURL, "@")[0]]
  84. if nil != pkg {
  85. theme.Downloads = pkg.Downloads
  86. }
  87. lock.Lock()
  88. ret = append(ret, theme)
  89. lock.Unlock()
  90. packageCache.SetDefault(repoURL, theme)
  91. })
  92. for _, repo := range stageIndex.Repos {
  93. waitGroup.Add(1)
  94. p.Invoke(repo)
  95. }
  96. waitGroup.Wait()
  97. p.Release()
  98. sort.Slice(ret, func(i, j int) bool { return ret[i].Updated > ret[j].Updated })
  99. return
  100. }
  101. func InstalledThemes() (ret []*Theme) {
  102. ret = []*Theme{}
  103. if !util.IsPathRegularDirOrSymlinkDir(util.ThemesPath) {
  104. return
  105. }
  106. themeDirs, err := os.ReadDir(util.ThemesPath)
  107. if nil != err {
  108. logging.LogWarnf("read appearance themes folder failed: %s", err)
  109. return
  110. }
  111. bazaarThemes := Themes()
  112. for _, themeDir := range themeDirs {
  113. if !util.IsDirRegularOrSymlink(themeDir) {
  114. continue
  115. }
  116. dirName := themeDir.Name()
  117. if isBuiltInTheme(dirName) {
  118. continue
  119. }
  120. theme, parseErr := ThemeJSON(dirName)
  121. if nil != parseErr || nil == theme {
  122. continue
  123. }
  124. installPath := filepath.Join(util.ThemesPath, dirName)
  125. theme.Installed = true
  126. theme.RepoURL = theme.URL
  127. theme.PreviewURL = "/appearance/themes/" + dirName + "/preview.png"
  128. theme.PreviewURLThumb = "/appearance/themes/" + dirName + "/preview.png"
  129. theme.IconURL = "/appearance/themes/" + dirName + "/icon.png"
  130. theme.PreferredFunding = getPreferredFunding(theme.Funding)
  131. theme.PreferredName = GetPreferredName(theme.Package)
  132. theme.PreferredDesc = getPreferredDesc(theme.Description)
  133. info, statErr := os.Stat(filepath.Join(installPath, "README.md"))
  134. if nil != statErr {
  135. logging.LogWarnf("stat install theme README.md failed: %s", statErr)
  136. continue
  137. }
  138. theme.HInstallDate = info.ModTime().Format("2006-01-02")
  139. installSize, _ := util.SizeOfDirectory(installPath)
  140. theme.InstallSize = installSize
  141. theme.HInstallSize = humanize.BytesCustomCeil(uint64(installSize), 2)
  142. readmeFilename := getPreferredReadme(theme.Readme)
  143. readme, readErr := os.ReadFile(filepath.Join(installPath, readmeFilename))
  144. if nil != readErr {
  145. logging.LogWarnf("read installed README.md failed: %s", readErr)
  146. continue
  147. }
  148. theme.PreferredReadme, _ = renderREADME(theme.URL, readme)
  149. theme.Outdated = isOutdatedTheme(theme, bazaarThemes)
  150. ret = append(ret, theme)
  151. }
  152. return
  153. }
  154. func isBuiltInTheme(dirName string) bool {
  155. return "daylight" == dirName || "midnight" == dirName
  156. }
  157. func InstallTheme(repoURL, repoHash, installPath string, systemID string) error {
  158. repoURLHash := repoURL + "@" + repoHash
  159. data, err := downloadPackage(repoURLHash, true, systemID)
  160. if nil != err {
  161. return err
  162. }
  163. return installPackage(data, installPath, repoURLHash)
  164. }
  165. func UninstallTheme(installPath string) error {
  166. return uninstallPackage(installPath)
  167. }