mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
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>
This commit is contained in:
@@ -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 {
|
||||
for _, assetPath := range assetPaths {
|
||||
content, layerName, err := assetFS.ReadLayeredFile(assetPath)
|
||||
if err != nil {
|
||||
return err
|
||||
log.Warn("Failed to read mail template %s by %s: %v", assetPath, layerName, err)
|
||||
continue
|
||||
}
|
||||
if d.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
content, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
log.Warn("Failed to read custom %s template. %v", path, err)
|
||||
return nil
|
||||
}
|
||||
|
||||
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
|
||||
|
Reference in New Issue
Block a user