template.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. "time"
  24. "github.com/88250/go-humanize"
  25. "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 Template struct {
  31. *Package
  32. }
  33. func Templates() (templates []*Template) {
  34. templates = []*Template{}
  35. stageIndex, err := getStageIndex("templates")
  36. if nil != err {
  37. return
  38. }
  39. bazaarIndex := getBazaarIndex()
  40. waitGroup := &sync.WaitGroup{}
  41. lock := &sync.Mutex{}
  42. p, _ := ants.NewPoolWithFunc(2, 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. templates = append(templates, pkg.(*Template))
  49. lock.Unlock()
  50. return
  51. }
  52. template := &Template{}
  53. innerU := util.BazaarOSSServer + "/package/" + repoURL + "/template.json"
  54. innerResp, innerErr := httpclient.NewBrowserRequest().SetSuccessResult(template).Get(innerU)
  55. if nil != innerErr {
  56. logging.LogErrorf("get community template [%s] failed: %s", repoURL, 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(template.Package) {
  64. return
  65. }
  66. template.URL = strings.TrimSuffix(template.URL, "/")
  67. repoURLHash := strings.Split(repoURL, "@")
  68. template.RepoURL = "https://github.com/" + repoURLHash[0]
  69. template.RepoHash = repoURLHash[1]
  70. template.PreviewURL = util.BazaarOSSServer + "/package/" + repoURL + "/preview.png?imageslim"
  71. template.PreviewURLThumb = util.BazaarOSSServer + "/package/" + repoURL + "/preview.png?imageView2/2/w/436/h/232"
  72. template.IconURL = util.BazaarOSSServer + "/package/" + repoURL + "/icon.png"
  73. template.Funding = repo.Package.Funding
  74. template.PreferredFunding = getPreferredFunding(template.Funding)
  75. template.PreferredName = GetPreferredName(template.Package)
  76. template.PreferredDesc = getPreferredDesc(template.Description)
  77. template.Updated = repo.Updated
  78. template.Stars = repo.Stars
  79. template.OpenIssues = repo.OpenIssues
  80. template.Size = repo.Size
  81. template.HSize = humanize.BytesCustomCeil(uint64(template.Size), 2)
  82. template.InstallSize = repo.InstallSize
  83. template.HInstallSize = humanize.BytesCustomCeil(uint64(template.InstallSize), 2)
  84. packageInstallSizeCache.SetDefault(template.RepoURL, template.InstallSize)
  85. template.HUpdated = formatUpdated(template.Updated)
  86. pkg := bazaarIndex[strings.Split(repoURL, "@")[0]]
  87. if nil != pkg {
  88. template.Downloads = pkg.Downloads
  89. }
  90. lock.Lock()
  91. templates = append(templates, template)
  92. lock.Unlock()
  93. packageCache.SetDefault(repoURL, template)
  94. })
  95. for _, repo := range stageIndex.Repos {
  96. waitGroup.Add(1)
  97. p.Invoke(repo)
  98. }
  99. waitGroup.Wait()
  100. p.Release()
  101. templates = filterLegacyTemplates(templates)
  102. sort.Slice(templates, func(i, j int) bool { return templates[i].Updated > templates[j].Updated })
  103. return
  104. }
  105. func InstalledTemplates() (ret []*Template) {
  106. ret = []*Template{}
  107. templatesPath := filepath.Join(util.DataDir, "templates")
  108. if !util.IsPathRegularDirOrSymlinkDir(templatesPath) {
  109. return
  110. }
  111. templateDirs, err := os.ReadDir(templatesPath)
  112. if nil != err {
  113. logging.LogWarnf("read templates folder failed: %s", err)
  114. return
  115. }
  116. bazaarTemplates := Templates()
  117. for _, templateDir := range templateDirs {
  118. if !util.IsDirRegularOrSymlink(templateDir) {
  119. continue
  120. }
  121. dirName := templateDir.Name()
  122. template, parseErr := TemplateJSON(dirName)
  123. if nil != parseErr || nil == template {
  124. continue
  125. }
  126. installPath := filepath.Join(util.DataDir, "templates", dirName)
  127. template.Installed = true
  128. template.RepoURL = template.URL
  129. template.PreviewURL = "/templates/" + dirName + "/preview.png"
  130. template.PreviewURLThumb = "/templates/" + dirName + "/preview.png"
  131. template.IconURL = "/templates/" + dirName + "/icon.png"
  132. template.PreferredFunding = getPreferredFunding(template.Funding)
  133. template.PreferredName = GetPreferredName(template.Package)
  134. template.PreferredDesc = getPreferredDesc(template.Description)
  135. info, statErr := os.Stat(filepath.Join(installPath, "README.md"))
  136. if nil != statErr {
  137. logging.LogWarnf("stat install theme README.md failed: %s", statErr)
  138. continue
  139. }
  140. template.HInstallDate = info.ModTime().Format("2006-01-02")
  141. if installSize, ok := packageInstallSizeCache.Get(template.RepoURL); ok {
  142. template.InstallSize = installSize.(int64)
  143. } else {
  144. is, _ := util.SizeOfDirectory(installPath)
  145. template.InstallSize = is
  146. packageInstallSizeCache.SetDefault(template.RepoURL, is)
  147. }
  148. template.HInstallSize = humanize.BytesCustomCeil(uint64(template.InstallSize), 2)
  149. readmeFilename := getPreferredReadme(template.Readme)
  150. readme, readErr := os.ReadFile(filepath.Join(installPath, readmeFilename))
  151. if nil != readErr {
  152. logging.LogWarnf("read installed README.md failed: %s", readErr)
  153. continue
  154. }
  155. template.PreferredReadme, _ = renderREADME(template.URL, readme)
  156. template.Outdated = isOutdatedTemplate(template, bazaarTemplates)
  157. ret = append(ret, template)
  158. }
  159. return
  160. }
  161. func InstallTemplate(repoURL, repoHash, installPath string, systemID string) error {
  162. repoURLHash := repoURL + "@" + repoHash
  163. data, err := downloadPackage(repoURLHash, true, systemID)
  164. if nil != err {
  165. return err
  166. }
  167. return installPackage(data, installPath, repoURLHash)
  168. }
  169. func UninstallTemplate(installPath string) error {
  170. return uninstallPackage(installPath)
  171. }
  172. func filterLegacyTemplates(templates []*Template) (ret []*Template) {
  173. verTime, _ := time.Parse("2006-01-02T15:04:05", "2021-05-12T00:00:00")
  174. for _, theme := range templates {
  175. if "" != theme.Updated {
  176. updated := theme.Updated[:len("2006-01-02T15:04:05")]
  177. t, err := time.Parse("2006-01-02T15:04:05", updated)
  178. if nil != err {
  179. logging.LogErrorf("convert update time [%s] failed: %s", updated, err)
  180. continue
  181. }
  182. if t.After(verTime) {
  183. ret = append(ret, theme)
  184. }
  185. }
  186. }
  187. return
  188. }