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.

base.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package templates
  4. import (
  5. "fmt"
  6. "io/fs"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "time"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/setting"
  13. "code.gitea.io/gitea/modules/util"
  14. )
  15. // Vars represents variables to be render in golang templates
  16. type Vars map[string]interface{}
  17. // Merge merges another vars to the current, another Vars will override the current
  18. func (vars Vars) Merge(another map[string]interface{}) Vars {
  19. for k, v := range another {
  20. vars[k] = v
  21. }
  22. return vars
  23. }
  24. // BaseVars returns all basic vars
  25. func BaseVars() Vars {
  26. startTime := time.Now()
  27. return map[string]interface{}{
  28. "IsLandingPageHome": setting.LandingPageURL == setting.LandingPageHome,
  29. "IsLandingPageExplore": setting.LandingPageURL == setting.LandingPageExplore,
  30. "IsLandingPageOrganizations": setting.LandingPageURL == setting.LandingPageOrganizations,
  31. "ShowRegistrationButton": setting.Service.ShowRegistrationButton,
  32. "ShowMilestonesDashboardPage": setting.Service.ShowMilestonesDashboardPage,
  33. "ShowFooterBranding": setting.ShowFooterBranding,
  34. "ShowFooterVersion": setting.ShowFooterVersion,
  35. "DisableDownloadSourceArchives": setting.Repository.DisableDownloadSourceArchives,
  36. "EnableSwagger": setting.API.EnableSwagger,
  37. "EnableOpenIDSignIn": setting.Service.EnableOpenIDSignIn,
  38. "PageStartTime": startTime,
  39. }
  40. }
  41. func getDirTemplateAssetNames(dir string) []string {
  42. return getDirAssetNames(dir, false)
  43. }
  44. func getDirAssetNames(dir string, mailer bool) []string {
  45. var tmpls []string
  46. if mailer {
  47. dir += filepath.Join(dir, "mail")
  48. }
  49. f, err := os.Stat(dir)
  50. if err != nil {
  51. if os.IsNotExist(err) {
  52. return tmpls
  53. }
  54. log.Warn("Unable to check if templates dir %s is a directory. Error: %v", dir, err)
  55. return tmpls
  56. }
  57. if !f.IsDir() {
  58. log.Warn("Templates dir %s is a not directory.", dir)
  59. return tmpls
  60. }
  61. files, err := util.StatDir(dir)
  62. if err != nil {
  63. log.Warn("Failed to read %s templates dir. %v", dir, err)
  64. return tmpls
  65. }
  66. prefix := "templates/"
  67. if mailer {
  68. prefix += "mail/"
  69. }
  70. for _, filePath := range files {
  71. if !mailer && strings.HasPrefix(filePath, "mail/") {
  72. continue
  73. }
  74. if !strings.HasSuffix(filePath, ".tmpl") {
  75. continue
  76. }
  77. tmpls = append(tmpls, prefix+filePath)
  78. }
  79. return tmpls
  80. }
  81. func walkAssetDir(root string, skipMail bool, callback func(path, name string, d fs.DirEntry, err error) error) error {
  82. mailRoot := filepath.Join(root, "mail")
  83. if err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
  84. name := path[len(root):]
  85. if len(name) > 0 && name[0] == '/' {
  86. name = name[1:]
  87. }
  88. if err != nil {
  89. if os.IsNotExist(err) {
  90. return callback(path, name, d, err)
  91. }
  92. return err
  93. }
  94. if skipMail && path == mailRoot && d.IsDir() {
  95. return fs.SkipDir
  96. }
  97. if util.CommonSkip(d.Name()) {
  98. if d.IsDir() {
  99. return fs.SkipDir
  100. }
  101. return nil
  102. }
  103. if strings.HasSuffix(d.Name(), ".tmpl") || d.IsDir() {
  104. return callback(path, name, d, err)
  105. }
  106. return nil
  107. }); err != nil && !os.IsNotExist(err) {
  108. return fmt.Errorf("unable to get files for template assets in %s: %w", root, err)
  109. }
  110. return nil
  111. }