forked from ipfs/kubo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway_indexPage.go
57 lines (48 loc) · 1.2 KB
/
gateway_indexPage.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
package corehttp
import (
"html/template"
"path"
"strings"
"github.com/ipfs/go-ipfs/assets"
)
// structs for directory listing
type listingTemplateData struct {
Listing []directoryItem
Path string
BackLink string
}
type directoryItem struct {
Size string
Name string
Path string
}
var listingTemplate *template.Template
func init() {
assetPath := "../vendor/dir-index-html-v1.0.0/"
knownIconsBytes, err := assets.Asset(assetPath + "knownIcons.txt")
if err != nil {
panic(err)
}
knownIcons := make(map[string]struct{})
for _, ext := range strings.Split(strings.TrimSuffix(string(knownIconsBytes), "\n"), "\n") {
knownIcons[ext] = struct{}{}
}
// helper to guess the type/icon for it by the extension name
iconFromExt := func(name string) string {
ext := path.Ext(name)
_, ok := knownIcons[ext]
if !ok {
// default blank icon
return "ipfs-_blank"
}
return "ipfs-" + ext[1:] // slice of the first dot
}
// Directory listing template
dirIndexBytes, err := assets.Asset(assetPath + "dir-index.html")
if err != nil {
panic(err)
}
listingTemplate = template.Must(template.New("dir").Funcs(template.FuncMap{
"iconFromExt": iconFromExt,
}).Parse(string(dirIndexBytes)))
}