Skip to content

Commit

Permalink
refactor: further simplify interface
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Feb 1, 2023
1 parent 7c44868 commit c7cc8b8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
9 changes: 5 additions & 4 deletions gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/ipfs/go-libipfs/blocks"
"github.com/ipfs/go-libipfs/files"
iface "github.com/ipfs/interface-go-ipfs-core"
options "github.com/ipfs/interface-go-ipfs-core/options"
"github.com/ipfs/interface-go-ipfs-core/path"
)

Expand All @@ -21,10 +20,10 @@ type Config struct {
// API defines the minimal set of API services required for a gateway handler.
type API interface {
// GetUnixFsNode returns a read-only handle to a file tree referenced by a path.
GetUnixFsNode(context.Context, path.Path) (files.Node, error)
GetUnixFsNode(context.Context, path.Resolved) (files.Node, error)

// LsUnixFsDir returns the list of links in a directory.
LsUnixFsDir(context.Context, path.Path, ...options.UnixfsLsOption) (<-chan iface.DirEntry, error)
LsUnixFsDir(context.Context, path.Resolved) (<-chan iface.DirEntry, error)

// GetBlock return a block from a certain CID.
GetBlock(context.Context, cid.Cid) (blocks.Block, error)
Expand All @@ -36,7 +35,9 @@ type API interface {
// IsCached returns whether or not the path exists locally.
IsCached(context.Context, path.Path) bool

// ResolvePath resolves the path using UnixFS resolver
// ResolvePath resolves the path using UnixFS resolver. If the path does not
// exist due to a missing link, it should return an error of type:
// https://pkg.go.dev/github.com/ipfs/go-path@v0.3.0/resolver#ErrNoLink
ResolvePath(context.Context, path.Path) (path.Resolved, error)
}

Expand Down
18 changes: 8 additions & 10 deletions gateway/handler_unixfs_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/ipfs/go-libipfs/gateway/assets"
path "github.com/ipfs/go-path"
"github.com/ipfs/go-path/resolver"
options "github.com/ipfs/interface-go-ipfs-core/options"
ipath "github.com/ipfs/interface-go-ipfs-core/path"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -61,9 +60,15 @@ func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r *

// Check if directory has index.html, if so, serveFile
idxPath := ipath.Join(contentPath, "index.html")
idx, err := i.api.GetUnixFsNode(ctx, idxPath)
idxResolvedPath, err := i.api.ResolvePath(ctx, idxPath)
switch err.(type) {
case nil:
idx, err := i.api.GetUnixFsNode(ctx, idxResolvedPath)
if err != nil {
internalWebError(w, err)
return
}

f, ok := idx.(files.File)
if !ok {
internalWebError(w, files.ErrNotReader)
Expand Down Expand Up @@ -104,14 +109,7 @@ func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r *
return
}

// Optimization: use Unixfs.Ls without resolving children, but using the
// cumulative DAG size as the file size. This allows for a fast listing
// while keeping a good enough Size field.
results, err := i.api.LsUnixFsDir(ctx,
resolvedPath,
options.Unixfs.ResolveChildren(false),
options.Unixfs.UseCumulativeSize(true),
)
results, err := i.api.LsUnixFsDir(ctx, resolvedPath)
if err != nil {
internalWebError(w, err)
return
Expand Down

0 comments on commit c7cc8b8

Please sign in to comment.