Skip to content

Commit be143cf

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 CID of each directoryItem - emit size of directory - emit breadcrumbs
1 parent a61132e commit be143cf

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed

core/corehttp/gateway_handler.go

Lines changed: 25 additions & 5 deletions
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,22 @@ 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

364382
// See comment above where originalUrlPath is declared.
365383
tplData := listingTemplateData{
366-
Listing: dirListing,
367-
Path: urlPath,
368-
BackLink: backLink,
369-
Hash: hash,
384+
Listing: dirListing,
385+
Size: size,
386+
Path: urlPath,
387+
Breadcrumbs: breadcrumbs(urlPath),
388+
BackLink: backLink,
389+
Hash: hash,
370390
}
371391

372392
err = listingTemplate.Execute(w, tplData)

core/corehttp/gateway_indexPage.go

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,50 @@ import (
1111

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

2022
type directoryItem struct {
21-
Size string
23+
Size string
24+
Name string
25+
Path string
26+
Hash string
27+
ShortHash string
28+
}
29+
30+
type breadcrumb struct {
2231
Name string
2332
Path string
2433
}
2534

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

2860
func init() {

0 commit comments

Comments
 (0)