Skip to content

Commit

Permalink
Merge pull request #3244 from ipfs/feat/gateway-post-coreapi
Browse files Browse the repository at this point in the history
gateway: use core api for serving GET/HEAD/POST
  • Loading branch information
whyrusleeping authored Nov 14, 2016
2 parents 580584a + f610e19 commit aae9eb7
Show file tree
Hide file tree
Showing 7 changed files with 475 additions and 69 deletions.
26 changes: 26 additions & 0 deletions core/coreapi/coreapi.go
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
}
54 changes: 54 additions & 0 deletions core/coreapi/interface/interface.go
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")
59 changes: 59 additions & 0 deletions core/coreapi/unixfs.go
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
}
Loading

0 comments on commit aae9eb7

Please sign in to comment.