You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

static.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2016 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. //go:build bindata
  5. // +build bindata
  6. package templates
  7. import (
  8. "html/template"
  9. "io/ioutil"
  10. "os"
  11. "path"
  12. "path/filepath"
  13. "strings"
  14. texttmpl "text/template"
  15. "code.gitea.io/gitea/modules/log"
  16. "code.gitea.io/gitea/modules/setting"
  17. "code.gitea.io/gitea/modules/util"
  18. )
  19. var (
  20. subjectTemplates = texttmpl.New("")
  21. bodyTemplates = template.New("")
  22. )
  23. // GetAsset get a special asset, only for chi
  24. func GetAsset(name string) ([]byte, error) {
  25. bs, err := ioutil.ReadFile(filepath.Join(setting.CustomPath, name))
  26. if err != nil && !os.IsNotExist(err) {
  27. return nil, err
  28. } else if err == nil {
  29. return bs, nil
  30. }
  31. return Asset(strings.TrimPrefix(name, "templates/"))
  32. }
  33. // GetAssetNames only for chi
  34. func GetAssetNames() []string {
  35. realFS := Assets.(vfsgen۰FS)
  36. var tmpls = make([]string, 0, len(realFS))
  37. for k := range realFS {
  38. tmpls = append(tmpls, "templates/"+k[1:])
  39. }
  40. customDir := path.Join(setting.CustomPath, "templates")
  41. customTmpls := getDirAssetNames(customDir)
  42. return append(tmpls, customTmpls...)
  43. }
  44. // Mailer provides the templates required for sending notification mails.
  45. func Mailer() (*texttmpl.Template, *template.Template) {
  46. for _, funcs := range NewTextFuncMap() {
  47. subjectTemplates.Funcs(funcs)
  48. }
  49. for _, funcs := range NewFuncMap() {
  50. bodyTemplates.Funcs(funcs)
  51. }
  52. for _, assetPath := range AssetNames() {
  53. if !strings.HasPrefix(assetPath, "mail/") {
  54. continue
  55. }
  56. if !strings.HasSuffix(assetPath, ".tmpl") {
  57. continue
  58. }
  59. content, err := Asset(assetPath)
  60. if err != nil {
  61. log.Warn("Failed to read embedded %s template. %v", assetPath, err)
  62. continue
  63. }
  64. buildSubjectBodyTemplate(subjectTemplates,
  65. bodyTemplates,
  66. strings.TrimPrefix(
  67. strings.TrimSuffix(
  68. assetPath,
  69. ".tmpl",
  70. ),
  71. "mail/",
  72. ),
  73. content)
  74. }
  75. customDir := path.Join(setting.CustomPath, "templates", "mail")
  76. isDir, err := util.IsDir(customDir)
  77. if err != nil {
  78. log.Warn("Failed to check if custom directory %s is a directory. %v", err)
  79. }
  80. if isDir {
  81. files, err := util.StatDir(customDir)
  82. if err != nil {
  83. log.Warn("Failed to read %s templates dir. %v", customDir, err)
  84. } else {
  85. for _, filePath := range files {
  86. if !strings.HasSuffix(filePath, ".tmpl") {
  87. continue
  88. }
  89. content, err := ioutil.ReadFile(path.Join(customDir, filePath))
  90. if err != nil {
  91. log.Warn("Failed to read custom %s template. %v", filePath, err)
  92. continue
  93. }
  94. buildSubjectBodyTemplate(subjectTemplates,
  95. bodyTemplates,
  96. strings.TrimSuffix(
  97. filePath,
  98. ".tmpl",
  99. ),
  100. content)
  101. }
  102. }
  103. }
  104. return subjectTemplates, bodyTemplates
  105. }
  106. func Asset(name string) ([]byte, error) {
  107. f, err := Assets.Open("/" + name)
  108. if err != nil {
  109. return nil, err
  110. }
  111. defer f.Close()
  112. return ioutil.ReadAll(f)
  113. }
  114. func AssetNames() []string {
  115. realFS := Assets.(vfsgen۰FS)
  116. var results = make([]string, 0, len(realFS))
  117. for k := range realFS {
  118. results = append(results, k[1:])
  119. }
  120. return results
  121. }
  122. func AssetIsDir(name string) (bool, error) {
  123. if f, err := Assets.Open("/" + name); err != nil {
  124. return false, err
  125. } else {
  126. defer f.Close()
  127. if fi, err := f.Stat(); err != nil {
  128. return false, err
  129. } else {
  130. return fi.IsDir(), nil
  131. }
  132. }
  133. }