forked from ipfs/boxo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assets.go
195 lines (165 loc) · 3.6 KB
/
assets.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package assets
import (
"embed"
"io"
"io/fs"
"net"
"strconv"
"html/template"
"strings"
"github.com/cespare/xxhash/v2"
ipfspath "github.com/stateless-minds/boxo/path"
)
//go:embed *.html *.css
var assets embed.FS
// AssetHash a non-cryptographic hash of all embedded assets
var AssetHash string
var (
DirectoryTemplate *template.Template
DagTemplate *template.Template
ErrorTemplate *template.Template
)
func init() {
initAssetsHash()
initTemplates()
}
func initAssetsHash() {
sum := xxhash.New()
err := fs.WalkDir(assets, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
file, err := assets.Open(path)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(sum, file)
return err
})
if err != nil {
panic("error creating asset sum: " + err.Error())
}
AssetHash = strconv.FormatUint(sum.Sum64(), 32)
}
func initTemplates() {
var err error
// Directory listing template
DirectoryTemplate, err = BuildTemplate(assets, "directory.html")
if err != nil {
panic(err)
}
// DAG Index template
DagTemplate, err = BuildTemplate(assets, "dag.html")
if err != nil {
panic(err)
}
// Error template
ErrorTemplate, err = BuildTemplate(assets, "error.html")
if err != nil {
panic(err)
}
}
type MenuItem struct {
URL string
Title string
}
type GlobalData struct {
Menu []MenuItem
GatewayURL string
DNSLink bool
}
type DagTemplateData struct {
GlobalData
Path string
CID string
CodecName string
CodecHex string
Node *ParsedNode
}
type ErrorTemplateData struct {
GlobalData
StatusCode int
StatusText string
Error string
}
type DirectoryTemplateData struct {
GlobalData
Listing []DirectoryItem
Size string
Path string
Breadcrumbs []Breadcrumb
BackLink string
Hash string
}
type DirectoryItem struct {
Size string
Name string
Path string
Hash string
ShortHash string
}
type Breadcrumb struct {
Name string
Path string
}
func Breadcrumbs(urlPath string, dnslinkOrigin bool) []Breadcrumb {
var ret []Breadcrumb
p, err := ipfspath.ParsePath(urlPath)
if err != nil {
// No assets.Breadcrumbs, fallback to bare Path in template
return ret
}
segs := p.Segments()
contentRoot := segs[1]
for i, seg := range segs {
if i == 0 {
ret = append(ret, Breadcrumb{Name: seg})
} else {
ret = append(ret, Breadcrumb{
Name: seg,
Path: "/" + strings.Join(segs[0:i+1], "/"),
})
}
}
// Drop the /ipns/<fqdn> prefix from assets.Breadcrumb Paths when directory
// listing on a DNSLink website (loaded due to Host header in HTTP
// request). Necessary because the hostname most likely won't have a
// public gateway mounted.
if dnslinkOrigin {
prefix := "/ipns/" + contentRoot
for i, crumb := range ret {
if strings.HasPrefix(crumb.Path, prefix) {
ret[i].Path = strings.Replace(crumb.Path, prefix, "", 1)
}
}
// Make contentRoot assets.Breadcrumb link to the website root
ret[1].Path = "/"
}
return ret
}
func ShortHash(hash string) string {
if len(hash) <= 8 {
return hash
}
return (hash[0:4] + "\u2026" + hash[len(hash)-4:])
}
// helper to detect DNSLink website context
// (when hostname from gwURL is matching /ipns/<fqdn> in path)
func HasDNSLinkOrigin(gwURL string, path string) bool {
if gwURL != "" {
fqdn := stripPort(strings.TrimPrefix(gwURL, "//"))
return strings.HasPrefix(path, "/ipns/"+fqdn)
}
return false
}
func stripPort(hostname string) string {
host, _, err := net.SplitHostPort(hostname)
if err == nil {
return host
}
return hostname
}