1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-05-17 23:16:11 +08:00

fix(gw): links in CID column on dir listing

This switches go-ipfs to dir-index-html after
https://github.com/ipfs/dir-index-html/pull/43
got merged to master
This commit is contained in:
Marcin Rataj
2020-09-28 14:09:29 +02:00
parent cd1feb3af4
commit c94bd768d2
5 changed files with 25 additions and 11 deletions

File diff suppressed because one or more lines are too long

View File

@ -2,5 +2,5 @@
package assets
const (
BindataVersionHash = "514e5ae28d8adb84955801b56ef47aca44bf9cc8"
BindataVersionHash = "605b5945438e1fe2eaf8a6571cca7ecda12d5599"
)

View File

@ -391,13 +391,16 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
gwURL = ""
}
dnslink := hasDNSLinkOrigin(gwURL, urlPath)
// See comment above where originalUrlPath is declared.
tplData := listingTemplateData{
GatewayURL: gwURL,
DNSLink: dnslink,
Listing: dirListing,
Size: size,
Path: urlPath,
Breadcrumbs: breadcrumbs(urlPath, gwURL),
Breadcrumbs: breadcrumbs(urlPath, dnslink),
BackLink: backLink,
Hash: hash,
}

View File

@ -13,6 +13,7 @@ import (
// structs for directory listing
type listingTemplateData struct {
GatewayURL string
DNSLink bool
Listing []directoryItem
Size string
Path string
@ -34,7 +35,7 @@ type breadcrumb struct {
Path string
}
func breadcrumbs(urlPath string, gwRootURL string) []breadcrumb {
func breadcrumbs(urlPath string, dnslinkOrigin bool) []breadcrumb {
var ret []breadcrumb
p, err := ipfspath.ParsePath(urlPath)
@ -43,7 +44,6 @@ func breadcrumbs(urlPath string, gwRootURL string) []breadcrumb {
return ret
}
segs := p.Segments()
ns := segs[0]
contentRoot := segs[1]
for i, seg := range segs {
if i == 0 {
@ -56,10 +56,11 @@ func breadcrumbs(urlPath string, gwRootURL string) []breadcrumb {
}
}
// Drop the /ipns/<fqdn> prefix from breadcrumb Paths when directory listing
// on a DNSLink website (loaded due to Host header in HTTP request).
// Necessary because gwRootURL won't have a public gateway mounted.
if ns == "ipns" && (("//" + contentRoot) == gwRootURL) {
// Drop the /ipns/<fqdn> prefix from 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) {
@ -77,6 +78,16 @@ func shortHash(hash string) string {
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 != "" {
dnslinkRoot := strings.Replace(gwURL, "//", "/ipns/", 1)
return strings.HasPrefix(path, dnslinkRoot)
}
return false
}
var listingTemplate *template.Template
func init() {