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.5KB

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