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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 returns asset content via name
  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 ioutil.ReadFile(filepath.Join(setting.StaticRootPath, name))
  32. }
  33. // GetAssetNames returns assets list
  34. func GetAssetNames() []string {
  35. tmpls := getDirAssetNames(filepath.Join(setting.CustomPath, "templates"))
  36. tmpls2 := getDirAssetNames(filepath.Join(setting.StaticRootPath, "templates"))
  37. return append(tmpls, tmpls2...)
  38. }
  39. // Mailer provides the templates required for sending notification mails.
  40. func Mailer() (*texttmpl.Template, *template.Template) {
  41. for _, funcs := range NewTextFuncMap() {
  42. subjectTemplates.Funcs(funcs)
  43. }
  44. for _, funcs := range NewFuncMap() {
  45. bodyTemplates.Funcs(funcs)
  46. }
  47. staticDir := path.Join(setting.StaticRootPath, "templates", "mail")
  48. isDir, err := util.IsDir(staticDir)
  49. if err != nil {
  50. log.Warn("Unable to check if templates dir %s is a directory. Error: %v", staticDir, err)
  51. }
  52. if isDir {
  53. files, err := util.StatDir(staticDir)
  54. if err != nil {
  55. log.Warn("Failed to read %s templates dir. %v", staticDir, err)
  56. } else {
  57. for _, filePath := range files {
  58. if !strings.HasSuffix(filePath, ".tmpl") {
  59. continue
  60. }
  61. content, err := ioutil.ReadFile(path.Join(staticDir, filePath))
  62. if err != nil {
  63. log.Warn("Failed to read static %s template. %v", filePath, err)
  64. continue
  65. }
  66. buildSubjectBodyTemplate(subjectTemplates, bodyTemplates, strings.TrimSuffix(filePath, ".tmpl"), content)
  67. }
  68. }
  69. }
  70. customDir := path.Join(setting.CustomPath, "templates", "mail")
  71. isDir, err = util.IsDir(customDir)
  72. if err != nil {
  73. log.Warn("Unable to check if templates dir %s is a directory. Error: %v", customDir, err)
  74. }
  75. if isDir {
  76. files, err := util.StatDir(customDir)
  77. if err != nil {
  78. log.Warn("Failed to read %s templates dir. %v", customDir, err)
  79. } else {
  80. for _, filePath := range files {
  81. if !strings.HasSuffix(filePath, ".tmpl") {
  82. continue
  83. }
  84. content, err := ioutil.ReadFile(path.Join(customDir, filePath))
  85. if err != nil {
  86. log.Warn("Failed to read custom %s template. %v", filePath, err)
  87. continue
  88. }
  89. buildSubjectBodyTemplate(subjectTemplates, bodyTemplates, strings.TrimSuffix(filePath, ".tmpl"), content)
  90. }
  91. }
  92. }
  93. return subjectTemplates, bodyTemplates
  94. }