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