Skip to content

Commit c94bd76

Browse files
committed
fix(gw): links in CID column on dir listing
This switches go-ipfs to dir-index-html after ipfs/dir-index-html#43 got merged to master
1 parent cd1feb3 commit c94bd76

File tree

5 files changed

+25
-11
lines changed

5 files changed

+25
-11
lines changed

assets/bindata.go

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/bindata_version_hash.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
package assets
33

44
const (
5-
BindataVersionHash = "514e5ae28d8adb84955801b56ef47aca44bf9cc8"
5+
BindataVersionHash = "605b5945438e1fe2eaf8a6571cca7ecda12d5599"
66
)

core/corehttp/gateway_handler.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -391,13 +391,16 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
391391
gwURL = ""
392392
}
393393

394+
dnslink := hasDNSLinkOrigin(gwURL, urlPath)
395+
394396
// See comment above where originalUrlPath is declared.
395397
tplData := listingTemplateData{
396398
GatewayURL: gwURL,
399+
DNSLink: dnslink,
397400
Listing: dirListing,
398401
Size: size,
399402
Path: urlPath,
400-
Breadcrumbs: breadcrumbs(urlPath, gwURL),
403+
Breadcrumbs: breadcrumbs(urlPath, dnslink),
401404
BackLink: backLink,
402405
Hash: hash,
403406
}

core/corehttp/gateway_indexPage.go

+17-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
// structs for directory listing
1414
type listingTemplateData struct {
1515
GatewayURL string
16+
DNSLink bool
1617
Listing []directoryItem
1718
Size string
1819
Path string
@@ -34,7 +35,7 @@ type breadcrumb struct {
3435
Path string
3536
}
3637

37-
func breadcrumbs(urlPath string, gwRootURL string) []breadcrumb {
38+
func breadcrumbs(urlPath string, dnslinkOrigin bool) []breadcrumb {
3839
var ret []breadcrumb
3940

4041
p, err := ipfspath.ParsePath(urlPath)
@@ -43,7 +44,6 @@ func breadcrumbs(urlPath string, gwRootURL string) []breadcrumb {
4344
return ret
4445
}
4546
segs := p.Segments()
46-
ns := segs[0]
4747
contentRoot := segs[1]
4848
for i, seg := range segs {
4949
if i == 0 {
@@ -56,10 +56,11 @@ func breadcrumbs(urlPath string, gwRootURL string) []breadcrumb {
5656
}
5757
}
5858

59-
// Drop the /ipns/<fqdn> prefix from breadcrumb Paths when directory listing
60-
// on a DNSLink website (loaded due to Host header in HTTP request).
61-
// Necessary because gwRootURL won't have a public gateway mounted.
62-
if ns == "ipns" && (("//" + contentRoot) == gwRootURL) {
59+
// Drop the /ipns/<fqdn> prefix from breadcrumb Paths when directory
60+
// listing on a DNSLink website (loaded due to Host header in HTTP
61+
// request). Necessary because the hostname most likely won't have a
62+
// public gateway mounted.
63+
if dnslinkOrigin {
6364
prefix := "/ipns/" + contentRoot
6465
for i, crumb := range ret {
6566
if strings.HasPrefix(crumb.Path, prefix) {
@@ -77,6 +78,16 @@ func shortHash(hash string) string {
7778
return (hash[0:4] + "\u2026" + hash[len(hash)-4:])
7879
}
7980

81+
// helper to detect DNSLink website context
82+
// (when hostname from gwURL is matching /ipns/<fqdn> in path)
83+
func hasDNSLinkOrigin(gwURL string, path string) bool {
84+
if gwURL != "" {
85+
dnslinkRoot := strings.Replace(gwURL, "//", "/ipns/", 1)
86+
return strings.HasPrefix(path, dnslinkRoot)
87+
}
88+
return false
89+
}
90+
8091
var listingTemplate *template.Template
8192

8293
func init() {

0 commit comments

Comments
 (0)