summaryrefslogtreecommitdiffstats
path: root/modules/templates/mailer.go
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2023-04-12 18:16:45 +0800
committerGitHub <noreply@github.com>2023-04-12 18:16:45 +0800
commit50a72e7a83a16d183a264e969a73cdbc7fb808f4 (patch)
tree013456110621c36edb3fa0d1bb77906ba8d4e013 /modules/templates/mailer.go
parent42919ccb7cd32ab67d0878baf2bac6cd007899a8 (diff)
downloadgitea-50a72e7a83a16d183a264e969a73cdbc7fb808f4.tar.gz
gitea-50a72e7a83a16d183a264e969a73cdbc7fb808f4.zip
Use a general approach to access custom/static/builtin assets (#24022)
The idea is to use a Layered Asset File-system (modules/assetfs/layered.go) For example: when there are 2 layers: "custom", "builtin", when access to asset "my/page.tmpl", the Layered Asset File-system will first try to use "custom" assets, if not found, then use "builtin" assets. This approach will hugely simplify a lot of code, make them testable. Other changes: * Simplify the AssetsHandlerFunc code * Simplify the `gitea embedded` sub-command code --------- Co-authored-by: Jason Song <i@wolfogre.com> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'modules/templates/mailer.go')
-rw-r--r--modules/templates/mailer.go63
1 files changed, 13 insertions, 50 deletions
diff --git a/modules/templates/mailer.go b/modules/templates/mailer.go
index d0c49e1025..280ac0e587 100644
--- a/modules/templates/mailer.go
+++ b/modules/templates/mailer.go
@@ -6,15 +6,12 @@ package templates
import (
"context"
"html/template"
- "io/fs"
- "os"
"strings"
texttmpl "text/template"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
- "code.gitea.io/gitea/modules/watcher"
)
// mailSubjectTextFuncMap returns functions for injecting to text templates, it's only used for mail subject
@@ -62,54 +59,23 @@ func Mailer(ctx context.Context) (*texttmpl.Template, *template.Template) {
bodyTemplates.Funcs(funcs)
}
+ assetFS := AssetFS()
refreshTemplates := func() {
- for _, assetPath := range BuiltinAssetNames() {
- if !strings.HasPrefix(assetPath, "mail/") {
- continue
- }
-
- if !strings.HasSuffix(assetPath, ".tmpl") {
- continue
- }
-
- content, err := BuiltinAsset(assetPath)
- if err != nil {
- log.Warn("Failed to read embedded %s template. %v", assetPath, err)
- continue
- }
-
- assetName := strings.TrimPrefix(strings.TrimSuffix(assetPath, ".tmpl"), "mail/")
-
- log.Trace("Adding built-in mailer template for %s", assetName)
- buildSubjectBodyTemplate(subjectTemplates,
- bodyTemplates,
- assetName,
- content)
+ assetPaths, err := ListMailTemplateAssetNames(assetFS)
+ if err != nil {
+ log.Error("Failed to list mail templates: %v", err)
+ return
}
- if err := walkMailerTemplates(func(path, name string, d fs.DirEntry, err error) error {
- if err != nil {
- return err
- }
- if d.IsDir() {
- return nil
- }
-
- content, err := os.ReadFile(path)
+ for _, assetPath := range assetPaths {
+ content, layerName, err := assetFS.ReadLayeredFile(assetPath)
if err != nil {
- log.Warn("Failed to read custom %s template. %v", path, err)
- return nil
+ log.Warn("Failed to read mail template %s by %s: %v", assetPath, layerName, err)
+ continue
}
-
- assetName := strings.TrimSuffix(name, ".tmpl")
- log.Trace("Adding mailer template for %s from %q", assetName, path)
- buildSubjectBodyTemplate(subjectTemplates,
- bodyTemplates,
- assetName,
- content)
- return nil
- }); err != nil && !os.IsNotExist(err) {
- log.Warn("Error whilst walking mailer templates directories. %v", err)
+ tmplName := strings.TrimPrefix(strings.TrimSuffix(assetPath, ".tmpl"), "mail/")
+ log.Trace("Adding mail template %s: %s by %s", tmplName, assetPath, layerName)
+ buildSubjectBodyTemplate(subjectTemplates, bodyTemplates, tmplName, content)
}
}
@@ -118,10 +84,7 @@ func Mailer(ctx context.Context) (*texttmpl.Template, *template.Template) {
if !setting.IsProd {
// Now subjectTemplates and bodyTemplates are both synchronized
// thus it is safe to call refresh from a different goroutine
- watcher.CreateWatcher(ctx, "Mailer Templates", &watcher.CreateWatcherOpts{
- PathsCallback: walkMailerTemplates,
- BetweenCallback: refreshTemplates,
- })
+ go assetFS.WatchLocalChanges(ctx, refreshTemplates)
}
return subjectTemplates, bodyTemplates