Skip to content

Commit

Permalink
refactor: simplify codec conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Feb 1, 2023
1 parent 872202e commit 1e462b6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
10 changes: 3 additions & 7 deletions gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"sort"

cid "github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-libipfs/blocks"
"github.com/ipfs/go-libipfs/files"
iface "github.com/ipfs/interface-go-ipfs-core"
Expand All @@ -33,16 +32,13 @@ type NodeAPI interface {
// GetBlock attempts to resolve the path and return a reader for data in the block.
GetBlock(context.Context, cid.Cid) (blocks.Block, error)

// IsCached returns whether or not the path exists locally.
IsCached(context.Context, path.Path) bool

// GetIPLDNode returns the IPLD node for the path.
GetIPLDNode(context.Context, cid.Cid) (ipld.Node, error)

// GetIPNSRecord retrieves the best IPNS record for a given CID (libp2p-key)
// from the routing system.
GetIPNSRecord(context.Context, cid.Cid) ([]byte, error)

// IsCached returns whether or not the path exists locally.
IsCached(context.Context, path.Path) bool

// ResolvePath resolves the path using UnixFS resolver
ResolvePath(context.Context, path.Path) (path.Resolved, error)
}
Expand Down
24 changes: 15 additions & 9 deletions gateway/handler_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import (
"time"

cid "github.com/ipfs/go-cid"
ipldlegacy "github.com/ipfs/go-ipld-legacy"
"github.com/ipfs/go-libipfs/gateway/assets"
ipath "github.com/ipfs/interface-go-ipfs-core/path"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/multicodec"
"github.com/ipld/go-ipld-prime/node/basicnode"
mc "github.com/multiformats/go-multicodec"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -176,19 +175,26 @@ func (i *handler) serveCodecRaw(ctx context.Context, w http.ResponseWriter, r *h

// serveCodecConverted returns payload converted to codec specified in toCodec
func (i *handler) serveCodecConverted(ctx context.Context, w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, toCodec mc.Code, modtime time.Time) {
obj, err := i.api.GetIPLDNode(ctx, resolvedPath.Cid())
blockCid := resolvedPath.Cid()
block, err := i.api.GetBlock(ctx, blockCid)
if err != nil {
webError(w, "ipfs dag get "+html.EscapeString(resolvedPath.String()), err, http.StatusInternalServerError)
webError(w, "ipfs block get "+html.EscapeString(resolvedPath.String()), err, http.StatusInternalServerError)
return
}

universal, ok := obj.(ipldlegacy.UniversalNode)
if !ok {
err = fmt.Errorf("%T is not a valid IPLD node", obj)
codec := blockCid.Prefix().Codec
decoder, err := multicodec.LookupDecoder(codec)
if err != nil {
webError(w, err.Error(), err, http.StatusInternalServerError)
return
}

node := basicnode.Prototype.Any.NewBuilder()
err = decoder(node, bytes.NewReader(block.RawData()))
if err != nil {
webError(w, err.Error(), err, http.StatusInternalServerError)
return
}
finalNode := universal.(ipld.Node)

encoder, err := multicodec.LookupEncoder(uint64(toCodec))
if err != nil {
Expand All @@ -198,7 +204,7 @@ func (i *handler) serveCodecConverted(ctx context.Context, w http.ResponseWriter

// Ensure IPLD node conforms to the codec specification.
var buf bytes.Buffer
err = encoder(finalNode, &buf)
err = encoder(node.Build(), &buf)
if err != nil {
webError(w, err.Error(), err, http.StatusInternalServerError)
return
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ require (
github.com/ipfs/go-ipfs-routing v0.3.0
github.com/ipfs/go-ipfs-util v0.0.2
github.com/ipfs/go-ipld-format v0.4.0
github.com/ipfs/go-ipld-legacy v0.1.1
github.com/ipfs/go-ipns v0.3.0
github.com/ipfs/go-log v1.0.5
github.com/ipfs/go-log/v2 v2.5.1
Expand Down Expand Up @@ -73,6 +72,7 @@ require (
github.com/ipfs/go-ipfs-ds-help v1.1.0 // indirect
github.com/ipfs/go-ipfs-pq v0.0.2 // indirect
github.com/ipfs/go-ipld-cbor v0.0.6 // indirect
github.com/ipfs/go-ipld-legacy v0.1.1 // indirect
github.com/ipfs/go-merkledag v0.9.0 // indirect
github.com/ipfs/go-verifcid v0.0.2 // indirect
github.com/ipld/go-codec-dagpb v1.5.0 // indirect
Expand Down

0 comments on commit 1e462b6

Please sign in to comment.