theme.go 5.3 KB

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