-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3244 from ipfs/feat/gateway-post-coreapi
gateway: use core api for serving GET/HEAD/POST
- Loading branch information
Showing
7 changed files
with
475 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package coreapi | ||
|
||
import ( | ||
"context" | ||
|
||
core "github.com/ipfs/go-ipfs/core" | ||
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" | ||
path "github.com/ipfs/go-ipfs/path" | ||
|
||
ipld "gx/ipfs/QmU7bFWQ793qmvNy7outdCaMfSDNk8uqhx4VNrxYj5fj5g/go-ipld-node" | ||
) | ||
|
||
func resolve(ctx context.Context, n *core.IpfsNode, p string) (ipld.Node, error) { | ||
pp, err := path.ParsePath(p) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
dagnode, err := core.Resolve(ctx, n.Namesys, n.Resolver, pp) | ||
if err == core.ErrNoNamesys { | ||
return nil, coreiface.ErrOffline | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
return dagnode, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package iface | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io" | ||
|
||
ipld "gx/ipfs/QmU7bFWQ793qmvNy7outdCaMfSDNk8uqhx4VNrxYj5fj5g/go-ipld-node" | ||
cid "gx/ipfs/QmXfiyr2RWEXpVDdaYnD2HNiBk6UBddsvEP4RPfXb6nGqY/go-cid" | ||
) | ||
|
||
// type CoreAPI interface { | ||
// ID() CoreID | ||
// Version() CoreVersion | ||
// } | ||
|
||
type Link ipld.Link | ||
|
||
type Reader interface { | ||
io.ReadSeeker | ||
io.Closer | ||
} | ||
|
||
type UnixfsAPI interface { | ||
Add(context.Context, io.Reader) (*cid.Cid, error) | ||
Cat(context.Context, string) (Reader, error) | ||
Ls(context.Context, string) ([]*Link, error) | ||
} | ||
|
||
// type ObjectAPI interface { | ||
// New() (cid.Cid, Object) | ||
// Get(string) (Object, error) | ||
// Links(string) ([]*Link, error) | ||
// Data(string) (Reader, error) | ||
// Stat(string) (ObjectStat, error) | ||
// Put(Object) (cid.Cid, error) | ||
// SetData(string, Reader) (cid.Cid, error) | ||
// AppendData(string, Data) (cid.Cid, error) | ||
// AddLink(string, string, string) (cid.Cid, error) | ||
// RmLink(string, string) (cid.Cid, error) | ||
// } | ||
|
||
// type ObjectStat struct { | ||
// Cid cid.Cid | ||
// NumLinks int | ||
// BlockSize int | ||
// LinksSize int | ||
// DataSize int | ||
// CumulativeSize int | ||
// } | ||
|
||
var ErrIsDir = errors.New("object is a directory") | ||
var ErrIsNonDag = errors.New("not a merkledag object") | ||
var ErrOffline = errors.New("can't resolve, ipfs node is offline") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package coreapi | ||
|
||
import ( | ||
"context" | ||
"io" | ||
|
||
core "github.com/ipfs/go-ipfs/core" | ||
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" | ||
coreunix "github.com/ipfs/go-ipfs/core/coreunix" | ||
uio "github.com/ipfs/go-ipfs/unixfs/io" | ||
|
||
cid "gx/ipfs/QmXfiyr2RWEXpVDdaYnD2HNiBk6UBddsvEP4RPfXb6nGqY/go-cid" | ||
) | ||
|
||
type UnixfsAPI struct { | ||
node *core.IpfsNode | ||
} | ||
|
||
func NewUnixfsAPI(n *core.IpfsNode) coreiface.UnixfsAPI { | ||
api := &UnixfsAPI{n} | ||
return api | ||
} | ||
|
||
func (api *UnixfsAPI) Add(ctx context.Context, r io.Reader) (*cid.Cid, error) { | ||
k, err := coreunix.AddWithContext(ctx, api.node, r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return cid.Decode(k) | ||
} | ||
|
||
func (api *UnixfsAPI) Cat(ctx context.Context, p string) (coreiface.Reader, error) { | ||
dagnode, err := resolve(ctx, api.node, p) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
r, err := uio.NewDagReader(ctx, dagnode, api.node.DAG) | ||
if err == uio.ErrIsDir { | ||
return nil, coreiface.ErrIsDir | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
return r, nil | ||
} | ||
|
||
func (api *UnixfsAPI) Ls(ctx context.Context, p string) ([]*coreiface.Link, error) { | ||
dagnode, err := resolve(ctx, api.node, p) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
l := dagnode.Links() | ||
links := make([]*coreiface.Link, len(l)) | ||
for i, l := range l { | ||
links[i] = &coreiface.Link{l.Name, l.Size, l.Cid} | ||
} | ||
return links, nil | ||
} |
Oops, something went wrong.