template.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. "sort"
  21. "strings"
  22. "sync"
  23. "time"
  24. "github.com/dustin/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. Author string `json:"author"`
  32. URL string `json:"url"`
  33. Version string `json:"version"`
  34. Name string `json:"name"`
  35. RepoURL string `json:"repoURL"`
  36. RepoHash string `json:"repoHash"`
  37. PreviewURL string `json:"previewURL"`
  38. PreviewURLThumb string `json:"previewURLThumb"`
  39. README string `json:"readme"`
  40. Installed bool `json:"installed"`
  41. Outdated bool `json:"outdated"`
  42. Updated string `json:"updated"`
  43. Stars int `json:"stars"`
  44. OpenIssues int `json:"openIssues"`
  45. Size int64 `json:"size"`
  46. HSize string `json:"hSize"`
  47. HUpdated string `json:"hUpdated"`
  48. Downloads int `json:"downloads"`
  49. }
  50. func Templates() (templates []*Template) {
  51. templates = []*Template{}
  52. result, err := util.GetRhyResult(false)
  53. if nil != err {
  54. return
  55. }
  56. bazaarIndex := getBazaarIndex()
  57. bazaarHash := result["bazaar"].(string)
  58. result = map[string]interface{}{}
  59. request := httpclient.NewBrowserRequest()
  60. u := util.BazaarOSSServer + "/bazaar@" + bazaarHash + "/stage/templates.json"
  61. resp, reqErr := request.SetResult(&result).Get(u)
  62. if nil != reqErr {
  63. logging.LogErrorf("get community stage index [%s] failed: %s", u, reqErr)
  64. return
  65. }
  66. if 200 != resp.StatusCode {
  67. logging.LogErrorf("get community stage index [%s] failed: %d", u, resp.StatusCode)
  68. return
  69. }
  70. repos := result["repos"].([]interface{})
  71. waitGroup := &sync.WaitGroup{}
  72. lock := &sync.Mutex{}
  73. p, _ := ants.NewPoolWithFunc(2, func(arg interface{}) {
  74. defer waitGroup.Done()
  75. repo := arg.(map[string]interface{})
  76. repoURL := repo["url"].(string)
  77. template := &Template{}
  78. innerU := util.BazaarOSSServer + "/package/" + repoURL + "/template.json"
  79. innerResp, innerErr := httpclient.NewBrowserRequest().SetResult(template).Get(innerU)
  80. if nil != innerErr {
  81. logging.LogErrorf("get community template [%s] failed: %s", repoURL, innerErr)
  82. return
  83. }
  84. if 200 != innerResp.StatusCode {
  85. logging.LogErrorf("get bazaar package [%s] failed: %d", innerU, innerResp.StatusCode)
  86. return
  87. }
  88. repoURLHash := strings.Split(repoURL, "@")
  89. template.RepoURL = "https://github.com/" + repoURLHash[0]
  90. template.RepoHash = repoURLHash[1]
  91. template.PreviewURL = util.BazaarOSSServer + "/package/" + repoURL + "/preview.png?imageslim"
  92. template.PreviewURLThumb = util.BazaarOSSServer + "/package/" + repoURL + "/preview.png?imageView2/2/w/436/h/232"
  93. template.Updated = repo["updated"].(string)
  94. template.Stars = int(repo["stars"].(float64))
  95. template.OpenIssues = int(repo["openIssues"].(float64))
  96. template.Size = int64(repo["size"].(float64))
  97. template.HSize = humanize.Bytes(uint64(template.Size))
  98. template.HUpdated = formatUpdated(template.Updated)
  99. pkg := bazaarIndex[strings.Split(repoURL, "@")[0]]
  100. if nil != pkg {
  101. template.Downloads = pkg.Downloads
  102. }
  103. lock.Lock()
  104. templates = append(templates, template)
  105. lock.Unlock()
  106. })
  107. for _, repo := range repos {
  108. waitGroup.Add(1)
  109. p.Invoke(repo)
  110. }
  111. waitGroup.Wait()
  112. p.Release()
  113. templates = filterLegacyTemplates(templates)
  114. sort.Slice(templates, func(i, j int) bool { return templates[i].Updated > templates[j].Updated })
  115. return
  116. }
  117. func InstallTemplate(repoURL, repoHash, installPath string, systemID string) error {
  118. repoURLHash := repoURL + "@" + repoHash
  119. data, err := downloadPackage(repoURLHash, true, systemID)
  120. if nil != err {
  121. return err
  122. }
  123. return installPackage(data, installPath)
  124. }
  125. func UninstallTemplate(installPath string) error {
  126. if err := os.RemoveAll(installPath); nil != err {
  127. logging.LogErrorf("remove template [%s] failed: %s", installPath, err)
  128. return errors.New("remove community template failed")
  129. }
  130. return nil
  131. }
  132. func filterLegacyTemplates(templates []*Template) (ret []*Template) {
  133. verTime, _ := time.Parse("2006-01-02T15:04:05", "2021-05-12T00:00:00")
  134. for _, theme := range templates {
  135. if "" != theme.Updated {
  136. updated := theme.Updated[:len("2006-01-02T15:04:05")]
  137. t, err := time.Parse("2006-01-02T15:04:05", updated)
  138. if nil != err {
  139. logging.LogErrorf("convert update time [%s] failed: %s", updated, err)
  140. continue
  141. }
  142. if t.After(verTime) {
  143. ret = append(ret, theme)
  144. }
  145. }
  146. }
  147. return
  148. }