提交 afa0c344 作者: Jeromy Johnson 提交者: GitHub

Merge pull request #3897 from ipfs/feat/faster-listing

improved gateway directory listing for sharded nodes
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"os"
gopath "path" gopath "path"
"runtime/debug" "runtime/debug"
"strings" "strings"
...@@ -20,6 +21,7 @@ import ( ...@@ -20,6 +21,7 @@ import (
dagutils "github.com/ipfs/go-ipfs/merkledag/utils" dagutils "github.com/ipfs/go-ipfs/merkledag/utils"
path "github.com/ipfs/go-ipfs/path" path "github.com/ipfs/go-ipfs/path"
ft "github.com/ipfs/go-ipfs/unixfs" ft "github.com/ipfs/go-ipfs/unixfs"
uio "github.com/ipfs/go-ipfs/unixfs/io"
humanize "gx/ipfs/QmPSBJL4momYnE7DcUyk2DVhD6rH488ZmHBGLbxNdhU44K/go-humanize" humanize "gx/ipfs/QmPSBJL4momYnE7DcUyk2DVhD6rH488ZmHBGLbxNdhU44K/go-humanize"
cid "gx/ipfs/QmYhQaCYEcaPPjxJX7YcPcVKkQfRy6sJ7B3XmGFk82XYdQ/go-cid" cid "gx/ipfs/QmYhQaCYEcaPPjxJX7YcPcVKkQfRy6sJ7B3XmGFk82XYdQ/go-cid"
...@@ -227,20 +229,22 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr ...@@ -227,20 +229,22 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
return return
} }
links, err := i.api.Unixfs().Ls(ctx, resolvedPath) nd, err := i.api.ResolveNode(ctx, resolvedPath)
if err != nil { if err != nil {
internalWebError(w, err) internalWebError(w, err)
return return
} }
// storage for directory listing dirr, err := uio.NewDirectoryFromNode(i.node.DAG, nd)
var dirListing []directoryItem if err != nil {
// loop through files internalWebError(w, err)
foundIndex := false return
for _, link := range links { }
if link.Name == "index.html" {
ixnd, err := dirr.Find(ctx, "index.html")
switch {
case err == nil:
log.Debugf("found index.html link for %s", urlPath) log.Debugf("found index.html link for %s", urlPath)
foundIndex = true
if urlPath[len(urlPath)-1] != '/' { if urlPath[len(urlPath)-1] != '/' {
// See comment above where originalUrlPath is declared. // See comment above where originalUrlPath is declared.
...@@ -249,7 +253,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr ...@@ -249,7 +253,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
return return
} }
dr, err := i.api.Unixfs().Cat(ctx, coreapi.ParseCid(link.Cid)) dr, err := i.api.Unixfs().Cat(ctx, coreapi.ParseCid(ixnd.Cid()))
if err != nil { if err != nil {
internalWebError(w, err) internalWebError(w, err)
return return
...@@ -258,16 +262,26 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr ...@@ -258,16 +262,26 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
// write to request // write to request
http.ServeContent(w, r, "index.html", modtime, dr) http.ServeContent(w, r, "index.html", modtime, dr)
break return
default:
internalWebError(w, err)
return
case os.IsNotExist(err):
}
if r.Method == "HEAD" {
return
} }
// storage for directory listing
var dirListing []directoryItem
dirr.ForEachLink(ctx, func(link *node.Link) error {
// See comment above where originalUrlPath is declared. // See comment above where originalUrlPath is declared.
di := directoryItem{humanize.Bytes(link.Size), link.Name, gopath.Join(originalUrlPath, link.Name)} di := directoryItem{humanize.Bytes(link.Size), link.Name, gopath.Join(originalUrlPath, link.Name)}
dirListing = append(dirListing, di) dirListing = append(dirListing, di)
} return nil
})
if !foundIndex {
if r.Method != "HEAD" {
// construct the correct back link // construct the correct back link
// https://github.com/ipfs/go-ipfs/issues/1365 // https://github.com/ipfs/go-ipfs/issues/1365
var backLink string = prefix + urlPath var backLink string = prefix + urlPath
...@@ -306,13 +320,11 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr ...@@ -306,13 +320,11 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
Path: originalUrlPath, Path: originalUrlPath,
BackLink: backLink, BackLink: backLink,
} }
err := listingTemplate.Execute(w, tplData) err = listingTemplate.Execute(w, tplData)
if err != nil { if err != nil {
internalWebError(w, err) internalWebError(w, err)
return return
} }
}
}
} }
func (i *gatewayHandler) postHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { func (i *gatewayHandler) postHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论