theme.go 5.6 KB

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