-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_asset.go
94 lines (76 loc) · 2.71 KB
/
context_asset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package admin
import (
"path/filepath"
oscommon "github.com/moisespsena-go/os-common"
"github.com/moisespsena-go/assetfs"
"github.com/moisespsena-go/assetfs/assetfsapi"
)
type assetfsCache struct {
assetfsapi.Interface
}
func (this *Context) templateFsS() []assetfs.Interface {
return []assetfs.Interface{this.SiteTemplateFS, assetfsCache{this.Admin.TemplateFS}}
}
func (this *Context) staticFsS() []assetfs.Interface {
return []assetfs.Interface{this.SiteStaticFS, assetfsCache{this.Admin.StaticFS}}
}
func (this *Context) Asset(layouts ...string) (asset assetfs.AssetInterface, err error) {
return this.getAsset(this.templateFsS(), layouts...)
}
func (this *Context) StaticAsset(layouts ...string) (asset assetfs.AssetInterface, err error) {
return this.getAsset(this.staticFsS(), layouts...)
}
func (this *Context) glob(pattern assetfs.GlobPatter, cb func(info assetfsapi.FileInfo), fs ...assetfs.Interface) {
for _, fs := range fs {
fs.NewGlob(pattern).Info(func(info assetfsapi.FileInfo) error {
cb(info)
return nil
})
}
}
func (this *Context) GlobTemplate(pattern assetfs.GlobPatter, cb func(info assetfsapi.FileInfo)) {
this.glob(pattern, cb, this.SiteTemplateFS, this.Admin.TemplateFS)
}
func (this *Context) GlobStatic(pattern assetfs.GlobPatter, cb func(info assetfsapi.FileInfo)) {
this.glob(pattern, cb, this.SiteStaticFS, this.Admin.StaticFS)
}
func (this *Context) findAsset(fs []assetfs.Interface, layouts ...string) (asset assetfs.AssetInterface, afs assetfs.Interface, err error) {
var prefixes, themes []string
if this.Request != nil {
if theme := this.Request.URL.Query().Get("theme"); theme != "" {
themes = append(themes, theme)
}
}
if len(themes) == 0 && this.Resource != nil {
for _, theme := range this.Resource.Config.Themes {
themes = append(themes, theme.GetName())
}
}
if resourcePath := this.resourcePath(); resourcePath != "" {
for _, theme := range themes {
prefixes = append(prefixes, filepath.Join("themes", theme, resourcePath))
}
prefixes = append(prefixes, resourcePath)
}
for _, theme := range themes {
prefixes = append(prefixes, filepath.Join("themes", theme))
}
return this.findAssetThemes(layouts, prefixes, fs...)
}
func (this *Context) findAssetThemes(layouts, prefixes []string, fs ...assetfs.Interface) (asset assetfs.AssetInterface, afs assetfs.Interface, err error) {
for _, layout := range layouts {
for _, prefix := range prefixes {
for _, afs = range fs {
if asset, err = afs.Asset(filepath.Join(prefix, layout)); err == nil {
return
}
}
}
for _, afs = range fs {
if asset, err = afs.Asset(layout); err == nil {
return
}
}
}
return nil, nil, oscommon.ErrNotFound(PathStack(layouts).String(), "theme not found")
}