template.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. "time"
  25. "github.com/dustin/go-humanize"
  26. "github.com/panjf2000/ants/v2"
  27. "github.com/siyuan-note/httpclient"
  28. "github.com/siyuan-note/logging"
  29. "github.com/siyuan-note/siyuan/kernel/util"
  30. )
  31. type Template struct {
  32. Package
  33. }
  34. func Templates() (templates []*Template) {
  35. templates = []*Template{}
  36. pkgIndex, err := getPkgIndex("templates")
  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(2, func(arg interface{}) {
  45. defer waitGroup.Done()
  46. repo := arg.(map[string]interface{})
  47. repoURL := repo["url"].(string)
  48. template := &Template{}
  49. innerU := util.BazaarOSSServer + "/package/" + repoURL + "/template.json"
  50. innerResp, innerErr := httpclient.NewBrowserRequest().SetSuccessResult(template).Get(innerU)
  51. if nil != innerErr {
  52. logging.LogErrorf("get community template [%s] failed: %s", repoURL, 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. template.URL = strings.TrimSuffix(template.URL, "/")
  60. repoURLHash := strings.Split(repoURL, "@")
  61. template.RepoURL = "https://github.com/" + repoURLHash[0]
  62. template.RepoHash = repoURLHash[1]
  63. template.PreviewURL = util.BazaarOSSServer + "/package/" + repoURL + "/preview.png?imageslim"
  64. template.PreviewURLThumb = util.BazaarOSSServer + "/package/" + repoURL + "/preview.png?imageView2/2/w/436/h/232"
  65. template.Updated = repo["updated"].(string)
  66. template.Stars = int(repo["stars"].(float64))
  67. template.OpenIssues = int(repo["openIssues"].(float64))
  68. template.Size = int64(repo["size"].(float64))
  69. template.HSize = humanize.Bytes(uint64(template.Size))
  70. template.HUpdated = formatUpdated(template.Updated)
  71. pkg := bazaarIndex[strings.Split(repoURL, "@")[0]]
  72. if nil != pkg {
  73. template.Downloads = pkg.Downloads
  74. }
  75. lock.Lock()
  76. templates = append(templates, template)
  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. templates = filterLegacyTemplates(templates)
  86. sort.Slice(templates, func(i, j int) bool { return templates[i].Updated > templates[j].Updated })
  87. return
  88. }
  89. func InstalledTemplates() (ret []*Template) {
  90. ret = []*Template{}
  91. templateDirs, err := os.ReadDir(filepath.Join(util.DataDir, "templates"))
  92. if nil != err {
  93. logging.LogWarnf("read templates folder failed: %s", err)
  94. return
  95. }
  96. bazaarTemplates := Templates()
  97. for _, templateDir := range templateDirs {
  98. if !templateDir.IsDir() {
  99. continue
  100. }
  101. dirName := templateDir.Name()
  102. templateConf, parseErr := TemplateJSON(dirName)
  103. if nil != parseErr || nil == templateConf {
  104. continue
  105. }
  106. installPath := filepath.Join(util.DataDir, "templates", dirName)
  107. template := &Template{}
  108. template.Installed = true
  109. template.Name = templateConf["name"].(string)
  110. template.Author = templateConf["author"].(string)
  111. template.URL = templateConf["url"].(string)
  112. template.URL = strings.TrimSuffix(template.URL, "/")
  113. template.Version = templateConf["version"].(string)
  114. template.RepoURL = template.URL
  115. template.PreviewURL = "/templates/" + dirName + "/preview.png"
  116. template.PreviewURLThumb = "/templates/" + dirName + "/preview.png"
  117. info, statErr := os.Stat(filepath.Join(installPath, "README.md"))
  118. if nil != statErr {
  119. logging.LogWarnf("stat install theme README.md failed: %s", statErr)
  120. continue
  121. }
  122. template.HInstallDate = info.ModTime().Format("2006-01-02")
  123. installSize, _ := util.SizeOfDirectory(installPath)
  124. template.InstallSize = installSize
  125. template.HInstallSize = humanize.Bytes(uint64(installSize))
  126. readme, readErr := os.ReadFile(filepath.Join(installPath, "README.md"))
  127. if nil != readErr {
  128. logging.LogWarnf("read install template README.md failed: %s", readErr)
  129. continue
  130. }
  131. template.README, _ = renderREADME(template.URL, readme)
  132. template.Outdated = isOutdatedTemplate(template, bazaarTemplates)
  133. ret = append(ret, template)
  134. }
  135. return
  136. }
  137. func InstallTemplate(repoURL, repoHash, installPath string, systemID string) error {
  138. repoURLHash := repoURL + "@" + repoHash
  139. data, err := downloadPackage(repoURLHash, true, systemID)
  140. if nil != err {
  141. return err
  142. }
  143. return installPackage(data, installPath)
  144. }
  145. func UninstallTemplate(installPath string) error {
  146. if err := os.RemoveAll(installPath); nil != err {
  147. logging.LogErrorf("remove template [%s] failed: %s", installPath, err)
  148. return errors.New("remove community template failed")
  149. }
  150. return nil
  151. }
  152. func filterLegacyTemplates(templates []*Template) (ret []*Template) {
  153. verTime, _ := time.Parse("2006-01-02T15:04:05", "2021-05-12T00:00:00")
  154. for _, theme := range templates {
  155. if "" != theme.Updated {
  156. updated := theme.Updated[:len("2006-01-02T15:04:05")]
  157. t, err := time.Parse("2006-01-02T15:04:05", updated)
  158. if nil != err {
  159. logging.LogErrorf("convert update time [%s] failed: %s", updated, err)
  160. continue
  161. }
  162. if t.After(verTime) {
  163. ret = append(ret, theme)
  164. }
  165. }
  166. }
  167. return
  168. }