Skip to content

Commit 2327777

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 2327777

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

core/corehttp/gateway_handler.go

+24-5
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,19 @@ 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+
}
333344
dirListing = append(dirListing, di)
334345
}
335346
if dirit.Err() != nil {
@@ -359,14 +370,22 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
359370
}
360371
}
361372

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

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

372391
err = listingTemplate.Execute(w, tplData)

core/corehttp/gateway_indexPage.go

+31-4
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,43 @@ 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 {
2123
Size string
2224
Name string
2325
Path string
26+
Hash string
27+
}
28+
29+
type breadcrumb struct {
30+
Name string
31+
Path string
32+
}
33+
34+
func breadcrumbs(path string) []breadcrumb {
35+
var ret []breadcrumb
36+
var pathParts = strings.Split(path, "/")
37+
for i, pathPart := range pathParts {
38+
if pathPart == "" {
39+
continue
40+
}
41+
if pathPart == "ipfs" {
42+
ret = append(ret, breadcrumb{Name: pathPart})
43+
} else {
44+
ret = append(ret, breadcrumb{
45+
Name: pathPart,
46+
Path: "/" + strings.Join(pathParts[1:i+1], "/"),
47+
})
48+
}
49+
}
50+
return ret
2451
}
2552

2653
var listingTemplate *template.Template

0 commit comments

Comments
 (0)