code.gitea.io/gitea@v1.19.3/modules/templates/dynamic.go (about) 1 // Copyright 2016 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 //go:build !bindata 5 6 package templates 7 8 import ( 9 "html/template" 10 "io/fs" 11 "os" 12 "path/filepath" 13 texttmpl "text/template" 14 15 "code.gitea.io/gitea/modules/setting" 16 ) 17 18 var ( 19 subjectTemplates = texttmpl.New("") 20 bodyTemplates = template.New("") 21 ) 22 23 // GetAsset returns asset content via name 24 func GetAsset(name string) ([]byte, error) { 25 bs, err := os.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 32 return os.ReadFile(filepath.Join(setting.StaticRootPath, name)) 33 } 34 35 // GetAssetFilename returns the filename of the provided asset 36 func GetAssetFilename(name string) (string, error) { 37 filename := filepath.Join(setting.CustomPath, name) 38 _, err := os.Stat(filename) 39 if err != nil && !os.IsNotExist(err) { 40 return filename, err 41 } else if err == nil { 42 return filename, nil 43 } 44 45 filename = filepath.Join(setting.StaticRootPath, name) 46 _, err = os.Stat(filename) 47 return filename, err 48 } 49 50 // walkTemplateFiles calls a callback for each template asset 51 func walkTemplateFiles(callback func(path, name string, d fs.DirEntry, err error) error) error { 52 if err := walkAssetDir(filepath.Join(setting.CustomPath, "templates"), true, callback); err != nil && !os.IsNotExist(err) { 53 return err 54 } 55 if err := walkAssetDir(filepath.Join(setting.StaticRootPath, "templates"), true, callback); err != nil && !os.IsNotExist(err) { 56 return err 57 } 58 return nil 59 } 60 61 // GetTemplateAssetNames returns list of template names 62 func GetTemplateAssetNames() []string { 63 tmpls := getDirTemplateAssetNames(filepath.Join(setting.CustomPath, "templates")) 64 tmpls2 := getDirTemplateAssetNames(filepath.Join(setting.StaticRootPath, "templates")) 65 return append(tmpls, tmpls2...) 66 } 67 68 func walkMailerTemplates(callback func(path, name string, d fs.DirEntry, err error) error) error { 69 if err := walkAssetDir(filepath.Join(setting.StaticRootPath, "templates", "mail"), false, callback); err != nil && !os.IsNotExist(err) { 70 return err 71 } 72 if err := walkAssetDir(filepath.Join(setting.CustomPath, "templates", "mail"), false, callback); err != nil && !os.IsNotExist(err) { 73 return err 74 } 75 return nil 76 } 77 78 // BuiltinAsset will read the provided asset from the embedded assets 79 // (This always returns os.ErrNotExist) 80 func BuiltinAsset(name string) ([]byte, error) { 81 return nil, os.ErrNotExist 82 } 83 84 // BuiltinAssetNames returns the names of the embedded assets 85 // (This always returns nil) 86 func BuiltinAssetNames() []string { 87 return nil 88 }