mirror of
https://github.com/ipfs/kubo.git
synced 2025-08-06 19:44:01 +08:00

Change the template handler to pass the hash to the directory listing template if the original url doesn't contain the hash. The directory listing template will display the hash in grey under the original url if the hash is passed to it. License: MIT Signed-off-by: Jack Loughran <j@ckloughran.com>
66 lines
1.4 KiB
Go
66 lines
1.4 KiB
Go
package corehttp
|
|
|
|
import (
|
|
"html/template"
|
|
"net/url"
|
|
"path"
|
|
"strings"
|
|
|
|
"github.com/ipfs/go-ipfs/assets"
|
|
)
|
|
|
|
// structs for directory listing
|
|
type listingTemplateData struct {
|
|
Listing []directoryItem
|
|
Path string
|
|
BackLink string
|
|
Hash string
|
|
}
|
|
|
|
type directoryItem struct {
|
|
Size string
|
|
Name string
|
|
Path string
|
|
}
|
|
|
|
var listingTemplate *template.Template
|
|
|
|
func init() {
|
|
knownIconsBytes, err := assets.Asset("dir-index-html/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
|
|
}
|
|
|
|
// custom template-escaping function to escape a full path, including '#' and '?'
|
|
urlEscape := func(rawUrl string) string {
|
|
pathUrl := url.URL{Path: rawUrl}
|
|
return pathUrl.String()
|
|
}
|
|
|
|
// Directory listing template
|
|
dirIndexBytes, err := assets.Asset("dir-index-html/dir-index.html")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
listingTemplate = template.Must(template.New("dir").Funcs(template.FuncMap{
|
|
"iconFromExt": iconFromExt,
|
|
"urlEscape": urlEscape,
|
|
}).Parse(string(dirIndexBytes)))
|
|
}
|