Skip to content

Commit e717f72

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 e717f72

File tree

3 files changed

+80
-11
lines changed

3 files changed

+80
-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

+38-5
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,51 @@ import (
1111

1212
// structs for directory listing
1313
type listingTemplateData struct {
14-
Listing []directoryItem
15-
Path string
16-
BackLink string
17-
Hash string
14+
GatewayURL string
15+
Listing []directoryItem
16+
Size string
17+
Path string
18+
Breadcrumbs []breadcrumb
19+
BackLink string
20+
Hash string
1821
}
1922

2023
type directoryItem struct {
21-
Size string
24+
Size string
25+
Name string
26+
Path string
27+
Hash string
28+
ShortHash string
29+
}
30+
31+
type breadcrumb struct {
2232
Name string
2333
Path string
2434
}
2535

36+
func breadcrumbs(path string) []breadcrumb {
37+
var ret []breadcrumb
38+
var pathParts = strings.Split(path, "/")
39+
for i, pathPart := range pathParts {
40+
if pathPart == "" {
41+
continue
42+
}
43+
if pathPart == "ipfs" {
44+
ret = append(ret, breadcrumb{Name: pathPart})
45+
} else {
46+
ret = append(ret, breadcrumb{
47+
Name: pathPart,
48+
Path: "/" + strings.Join(pathParts[1:i+1], "/"),
49+
})
50+
}
51+
}
52+
return ret
53+
}
54+
55+
func shortHash(hash string) string {
56+
return (hash[0:4] + "\u2026" + hash[len(hash)-4:])
57+
}
58+
2659
var listingTemplate *template.Template
2760

2861
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)