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.

dynamic.go 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. "os"
  9. "path"
  10. "path/filepath"
  11. "strings"
  12. texttmpl "text/template"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/setting"
  15. "code.gitea.io/gitea/modules/util"
  16. )
  17. var (
  18. subjectTemplates = texttmpl.New("")
  19. bodyTemplates = template.New("")
  20. )
  21. // GetAsset returns asset content via name
  22. func GetAsset(name string) ([]byte, error) {
  23. bs, err := os.ReadFile(filepath.Join(setting.CustomPath, name))
  24. if err != nil && !os.IsNotExist(err) {
  25. return nil, err
  26. } else if err == nil {
  27. return bs, nil
  28. }
  29. return os.ReadFile(filepath.Join(setting.StaticRootPath, name))
  30. }
  31. // GetAssetNames returns assets list
  32. func GetAssetNames() []string {
  33. tmpls := getDirAssetNames(filepath.Join(setting.CustomPath, "templates"))
  34. tmpls2 := getDirAssetNames(filepath.Join(setting.StaticRootPath, "templates"))
  35. return append(tmpls, tmpls2...)
  36. }
  37. // Mailer provides the templates required for sending notification mails.
  38. func Mailer() (*texttmpl.Template, *template.Template) {
  39. for _, funcs := range NewTextFuncMap() {
  40. subjectTemplates.Funcs(funcs)
  41. }
  42. for _, funcs := range NewFuncMap() {
  43. bodyTemplates.Funcs(funcs)
  44. }
  45. staticDir := path.Join(setting.StaticRootPath, "templates", "mail")
  46. isDir, err := util.IsDir(staticDir)
  47. if err != nil {
  48. log.Warn("Unable to check if templates dir %s is a directory. Error: %v", staticDir, err)
  49. }
  50. if isDir {
  51. files, err := util.StatDir(staticDir)
  52. if err != nil {
  53. log.Warn("Failed to read %s templates dir. %v", staticDir, err)
  54. } else {
  55. for _, filePath := range files {
  56. if !strings.HasSuffix(filePath, ".tmpl") {
  57. continue
  58. }
  59. content, err := os.ReadFile(path.Join(staticDir, filePath))
  60. if err != nil {
  61. log.Warn("Failed to read static %s template. %v", filePath, err)
  62. continue
  63. }
  64. buildSubjectBodyTemplate(subjectTemplates, bodyTemplates, strings.TrimSuffix(filePath, ".tmpl"), content)
  65. }
  66. }
  67. }
  68. customDir := path.Join(setting.CustomPath, "templates", "mail")
  69. isDir, err = util.IsDir(customDir)
  70. if err != nil {
  71. log.Warn("Unable to check if templates dir %s is a directory. Error: %v", customDir, err)
  72. }
  73. if isDir {
  74. files, err := util.StatDir(customDir)
  75. if err != nil {
  76. log.Warn("Failed to read %s templates dir. %v", customDir, err)
  77. } else {
  78. for _, filePath := range files {
  79. if !strings.HasSuffix(filePath, ".tmpl") {
  80. continue
  81. }
  82. content, err := os.ReadFile(path.Join(customDir, filePath))
  83. if err != nil {
  84. log.Warn("Failed to read custom %s template. %v", filePath, err)
  85. continue
  86. }
  87. buildSubjectBodyTemplate(subjectTemplates, bodyTemplates, strings.TrimSuffix(filePath, ".tmpl"), content)
  88. }
  89. }
  90. }
  91. return subjectTemplates, bodyTemplates
  92. }