Skip to content

Commit c2246e4

Browse files
author
Kevin Neaton
committed
feat: Directory page UI improvements
These changes are needed to prepare for the Directory page UI improvements implemented in ipfs/dir-index-html#37. - update dir-index-html type structs - emit gateway URL for root links - emit CID of each directoryItem - emit size of directory - emit breadcrumbs
1 parent a61132e commit c2246e4

File tree

3 files changed

+86
-11
lines changed

3 files changed

+86
-11
lines changed

core/corehttp/gateway_handler.go

+37-5
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,20 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
328328
size = humanize.Bytes(uint64(s))
329329
}
330330

331+
hash := ""
332+
if r, err := i.api.ResolvePath(r.Context(), ipath.Join(resolvedPath, dirit.Name())); err == nil {
333+
// Path may not be resolved. Continue anyways.
334+
hash = r.Cid().String()
335+
}
336+
331337
// See comment above where originalUrlPath is declared.
332-
di := directoryItem{size, dirit.Name(), gopath.Join(originalUrlPath, dirit.Name())}
338+
di := directoryItem{
339+
Size: size,
340+
Name: dirit.Name(),
341+
Path: gopath.Join(originalUrlPath, dirit.Name()),
342+
Hash: hash,
343+
ShortHash: shortHash(hash),
344+
}
333345
dirListing = append(dirListing, di)
334346
}
335347
if dirit.Err() != nil {
@@ -359,14 +371,34 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
359371
}
360372
}
361373

374+
size := "?"
375+
if s, err := dir.Size(); err == nil {
376+
// Size may not be defined/supported. Continue anyways.
377+
size = humanize.Bytes(uint64(s))
378+
}
379+
362380
hash := resolvedPath.Cid().String()
363381

382+
// Storage for gateway URL to be used when linking to other rootIDs. This
383+
// will be blank unless subdomain resolution is being used for this request.
384+
var gwURL string
385+
386+
// Get gateway hostname and build gateway URL.
387+
if h, ok := r.Context().Value("gw-hostname").(string); ok {
388+
gwURL = "//" + h
389+
} else {
390+
gwURL = ""
391+
}
392+
364393
// See comment above where originalUrlPath is declared.
365394
tplData := listingTemplateData{
366-
Listing: dirListing,
367-
Path: urlPath,
368-
BackLink: backLink,
369-
Hash: hash,
395+
GatewayURL: gwURL,
396+
Listing: dirListing,
397+
Size: size,
398+
Path: urlPath,
399+
Breadcrumbs: breadcrumbs(urlPath),
400+
BackLink: backLink,
401+
Hash: hash,
370402
}
371403

372404
err = listingTemplate.Execute(w, tplData)

core/corehttp/gateway_indexPage.go

+44-5
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,61 @@ import (
77
"strings"
88

99
"github.com/ipfs/go-ipfs/assets"
10+
ipfspath "github.com/ipfs/go-path"
1011
)
1112

1213
// structs for directory listing
1314
type listingTemplateData struct {
14-
Listing []directoryItem
15-
Path string
16-
BackLink string
17-
Hash string
15+
GatewayURL string
16+
Listing []directoryItem
17+
Size string
18+
Path string
19+
Breadcrumbs []breadcrumb
20+
BackLink string
21+
Hash string
1822
}
1923

2024
type directoryItem struct {
21-
Size string
25+
Size string
26+
Name string
27+
Path string
28+
Hash string
29+
ShortHash string
30+
}
31+
32+
type breadcrumb struct {
2233
Name string
2334
Path string
2435
}
2536

37+
func breadcrumbs(urlPath string) []breadcrumb {
38+
var ret []breadcrumb
39+
40+
p, err := ipfspath.ParsePath(urlPath)
41+
if err != nil {
42+
// No breadcrumbs, fallback to bare Path in template
43+
return ret
44+
}
45+
46+
segs := p.Segments()
47+
for i, seg := range segs {
48+
if i == 0 {
49+
ret = append(ret, breadcrumb{Name: seg})
50+
} else {
51+
ret = append(ret, breadcrumb{
52+
Name: seg,
53+
Path: "/" + strings.Join(segs[0:i+1], "/"),
54+
})
55+
}
56+
}
57+
58+
return ret
59+
}
60+
61+
func shortHash(hash string) string {
62+
return (hash[0:4] + "\u2026" + hash[len(hash)-4:])
63+
}
64+
2665
var listingTemplate *template.Template
2766

2867
func init() {

core/corehttp/hostname.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ func HostnameOption() ServeOption {
141141
if gw, hostname, ns, rootID, ok := knownSubdomainDetails(r.Host, knownGateways); ok {
142142
// Looks like we're using known subdomain gateway.
143143

144+
// Add gateway hostname context for linking to other root ids.
145+
// Example: localhost/ipfs/{cid}
146+
ctx := context.WithValue(r.Context(), "gw-hostname", hostname)
147+
144148
// Assemble original path prefix.
145149
pathPrefix := "/" + ns + "/" + rootID
146150

@@ -167,7 +171,7 @@ func HostnameOption() ServeOption {
167171
r.URL.Path = pathPrefix + r.URL.Path
168172

169173
// Serve path request
170-
childMux.ServeHTTP(w, r)
174+
childMux.ServeHTTP(w, r.WithContext(ctx))
171175
return
172176
}
173177
// We don't have a known gateway. Fallback on DNSLink lookup

0 commit comments

Comments
 (0)