2016-12-06 23:28:31 +05:30
|
|
|
|
// Copyright 2016 The Gitea Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
2021-08-24 22:17:09 +05:30
|
|
|
|
//go:build bindata
|
|
|
|
|
// +build bindata
|
|
|
|
|
|
2016-12-06 23:28:31 +05:30
|
|
|
|
package templates
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"html/template"
|
2021-09-22 11:08:34 +05:30
|
|
|
|
"io"
|
2021-01-05 18:35:40 +05:30
|
|
|
|
"os"
|
2016-12-06 23:28:31 +05:30
|
|
|
|
"path"
|
2021-01-05 18:35:40 +05:30
|
|
|
|
"path/filepath"
|
2016-12-06 23:28:31 +05:30
|
|
|
|
"strings"
|
2019-11-07 19:04:28 +05:30
|
|
|
|
texttmpl "text/template"
|
2022-01-07 08:03:17 +05:30
|
|
|
|
"time"
|
2016-12-06 23:28:31 +05:30
|
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-01-07 08:03:17 +05:30
|
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2020-11-28 08:12:08 +05:30
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
2016-12-06 23:28:31 +05:30
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2019-11-07 19:04:28 +05:30
|
|
|
|
subjectTemplates = texttmpl.New("")
|
|
|
|
|
bodyTemplates = template.New("")
|
2016-12-06 23:28:31 +05:30
|
|
|
|
)
|
|
|
|
|
|
2022-01-07 08:03:17 +05:30
|
|
|
|
// GlobalModTime provide a gloabl mod time for embedded asset files
|
|
|
|
|
func GlobalModTime(filename string) time.Time {
|
|
|
|
|
return timeutil.GetExecutableModTime()
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-05 18:35:40 +05:30
|
|
|
|
// GetAsset get a special asset, only for chi
|
|
|
|
|
func GetAsset(name string) ([]byte, error) {
|
2021-09-22 11:08:34 +05:30
|
|
|
|
bs, err := os.ReadFile(filepath.Join(setting.CustomPath, name))
|
2021-01-05 18:35:40 +05:30
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
|
return nil, err
|
|
|
|
|
} else if err == nil {
|
|
|
|
|
return bs, nil
|
|
|
|
|
}
|
|
|
|
|
return Asset(strings.TrimPrefix(name, "templates/"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetAssetNames only for chi
|
|
|
|
|
func GetAssetNames() []string {
|
|
|
|
|
realFS := Assets.(vfsgen۰FS)
|
|
|
|
|
var tmpls = make([]string, 0, len(realFS))
|
|
|
|
|
for k := range realFS {
|
|
|
|
|
tmpls = append(tmpls, "templates/"+k[1:])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
customDir := path.Join(setting.CustomPath, "templates")
|
|
|
|
|
customTmpls := getDirAssetNames(customDir)
|
|
|
|
|
return append(tmpls, customTmpls...)
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-06 23:28:31 +05:30
|
|
|
|
// Mailer provides the templates required for sending notification mails.
|
2019-11-07 19:04:28 +05:30
|
|
|
|
func Mailer() (*texttmpl.Template, *template.Template) {
|
|
|
|
|
for _, funcs := range NewTextFuncMap() {
|
|
|
|
|
subjectTemplates.Funcs(funcs)
|
|
|
|
|
}
|
2016-12-06 23:28:31 +05:30
|
|
|
|
for _, funcs := range NewFuncMap() {
|
2019-11-07 19:04:28 +05:30
|
|
|
|
bodyTemplates.Funcs(funcs)
|
2016-12-06 23:28:31 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, assetPath := range AssetNames() {
|
|
|
|
|
if !strings.HasPrefix(assetPath, "mail/") {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !strings.HasSuffix(assetPath, ".tmpl") {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
content, err := Asset(assetPath)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Warn("Failed to read embedded %s template. %v", assetPath, err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-07 19:04:28 +05:30
|
|
|
|
buildSubjectBodyTemplate(subjectTemplates,
|
|
|
|
|
bodyTemplates,
|
2016-12-06 23:28:31 +05:30
|
|
|
|
strings.TrimPrefix(
|
|
|
|
|
strings.TrimSuffix(
|
|
|
|
|
assetPath,
|
|
|
|
|
".tmpl",
|
|
|
|
|
),
|
|
|
|
|
"mail/",
|
|
|
|
|
),
|
2019-11-07 19:04:28 +05:30
|
|
|
|
content)
|
2016-12-06 23:28:31 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
customDir := path.Join(setting.CustomPath, "templates", "mail")
|
2020-11-28 08:12:08 +05:30
|
|
|
|
isDir, err := util.IsDir(customDir)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Warn("Failed to check if custom directory %s is a directory. %v", err)
|
|
|
|
|
}
|
|
|
|
|
if isDir {
|
2020-12-22 05:10:57 +05:30
|
|
|
|
files, err := util.StatDir(customDir)
|
2016-12-06 23:28:31 +05:30
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Warn("Failed to read %s templates dir. %v", customDir, err)
|
|
|
|
|
} else {
|
|
|
|
|
for _, filePath := range files {
|
|
|
|
|
if !strings.HasSuffix(filePath, ".tmpl") {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-22 11:08:34 +05:30
|
|
|
|
content, err := os.ReadFile(path.Join(customDir, filePath))
|
2016-12-06 23:28:31 +05:30
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Warn("Failed to read custom %s template. %v", filePath, err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-07 19:04:28 +05:30
|
|
|
|
buildSubjectBodyTemplate(subjectTemplates,
|
|
|
|
|
bodyTemplates,
|
2016-12-06 23:28:31 +05:30
|
|
|
|
strings.TrimSuffix(
|
|
|
|
|
filePath,
|
|
|
|
|
".tmpl",
|
|
|
|
|
),
|
2019-11-07 19:04:28 +05:30
|
|
|
|
content)
|
2016-12-06 23:28:31 +05:30
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-07 19:04:28 +05:30
|
|
|
|
return subjectTemplates, bodyTemplates
|
2016-12-06 23:28:31 +05:30
|
|
|
|
}
|
2019-06-03 03:13:47 +05:30
|
|
|
|
|
|
|
|
|
func Asset(name string) ([]byte, error) {
|
|
|
|
|
f, err := Assets.Open("/" + name)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer f.Close()
|
2021-09-22 11:08:34 +05:30
|
|
|
|
return io.ReadAll(f)
|
2019-06-03 03:13:47 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func AssetNames() []string {
|
|
|
|
|
realFS := Assets.(vfsgen۰FS)
|
|
|
|
|
var results = make([]string, 0, len(realFS))
|
|
|
|
|
for k := range realFS {
|
|
|
|
|
results = append(results, k[1:])
|
|
|
|
|
}
|
|
|
|
|
return results
|
|
|
|
|
}
|
2020-02-02 07:47:44 +05:30
|
|
|
|
|
|
|
|
|
func AssetIsDir(name string) (bool, error) {
|
|
|
|
|
if f, err := Assets.Open("/" + name); err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
} else {
|
|
|
|
|
defer f.Close()
|
|
|
|
|
if fi, err := f.Stat(); err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
} else {
|
|
|
|
|
return fi.IsDir(), nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|