Skip to content

Commit

Permalink
feat: Directory page UI improvements
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Kevin Neaton committed Jul 15, 2020
1 parent a61132e commit be143cf
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
30 changes: 25 additions & 5 deletions core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,20 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
size = humanize.Bytes(uint64(s))
}

hash := ""
if r, err := i.api.ResolvePath(r.Context(), ipath.Join(resolvedPath, dirit.Name())); err == nil {
// Path may not be resolved. Continue anyways.
hash = r.Cid().String()
}

// See comment above where originalUrlPath is declared.
di := directoryItem{size, dirit.Name(), gopath.Join(originalUrlPath, dirit.Name())}
di := directoryItem{
Size: size,
Name: dirit.Name(),
Path: gopath.Join(originalUrlPath, dirit.Name()),
Hash: hash,
ShortHash: shortHash(hash),
}
dirListing = append(dirListing, di)
}
if dirit.Err() != nil {
Expand Down Expand Up @@ -359,14 +371,22 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
}
}

size := "?"
if s, err := dir.Size(); err == nil {
// Size may not be defined/supported. Continue anyways.
size = humanize.Bytes(uint64(s))
}

hash := resolvedPath.Cid().String()

// See comment above where originalUrlPath is declared.
tplData := listingTemplateData{
Listing: dirListing,
Path: urlPath,
BackLink: backLink,
Hash: hash,
Listing: dirListing,
Size: size,
Path: urlPath,
Breadcrumbs: breadcrumbs(urlPath),
BackLink: backLink,
Hash: hash,
}

err = listingTemplate.Execute(w, tplData)
Expand Down
42 changes: 37 additions & 5 deletions core/corehttp/gateway_indexPage.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,50 @@ import (

// structs for directory listing
type listingTemplateData struct {
Listing []directoryItem
Path string
BackLink string
Hash string
Listing []directoryItem
Size string
Path string
Breadcrumbs []breadcrumb
BackLink string
Hash string
}

type directoryItem struct {
Size string
Size string
Name string
Path string
Hash string
ShortHash string
}

type breadcrumb struct {
Name string
Path string
}

func breadcrumbs(path string) []breadcrumb {
var ret []breadcrumb
var pathParts = strings.Split(path, "/")
for i, pathPart := range pathParts {
if pathPart == "" {
continue
}
if pathPart == "ipfs" {
ret = append(ret, breadcrumb{Name: pathPart})
} else {
ret = append(ret, breadcrumb{
Name: pathPart,
Path: "/" + strings.Join(pathParts[1:i+1], "/"),
})
}
}
return ret
}

func shortHash(hash string) string {
return (hash[0:4] + "\u2026" + hash[len(hash)-4:])
}

var listingTemplate *template.Template

func init() {
Expand Down

0 comments on commit be143cf

Please sign in to comment.