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

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