-
-
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.
License: MIT Signed-off-by: Lars Gierth <[email protected]>
- Loading branch information
Lars Gierth
committed
Jun 21, 2016
1 parent
c6443fd
commit 4716b70
Showing
2 changed files
with
102 additions
and
0 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,64 @@ | ||
package coreapi | ||
|
||
import ( | ||
core "github.com/ipfs/go-ipfs/core" | ||
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" | ||
dag "github.com/ipfs/go-ipfs/merkledag" | ||
path "github.com/ipfs/go-ipfs/path" | ||
uio "github.com/ipfs/go-ipfs/unixfs/io" | ||
context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context" | ||
) | ||
|
||
type CoreAPI struct { | ||
ctx context.Context | ||
node *core.IpfsNode | ||
} | ||
|
||
func NewCoreAPI(ctx context.Context, node *core.IpfsNode) (*CoreAPI, error) { | ||
api := &CoreAPI{ctx: ctx, node: node} | ||
return api, nil | ||
} | ||
|
||
func (api *CoreAPI) Context() context.Context { | ||
return api.ctx | ||
} | ||
|
||
func (api *CoreAPI) IpfsNode() *core.IpfsNode { | ||
return api.node | ||
} | ||
|
||
func (api *CoreAPI) resolve(p string) (*dag.Node, error) { | ||
dagnode, err := core.Resolve(api.ctx, api.node, path.Path(p)) | ||
if err == core.ErrNoNamesys && !api.node.OnlineMode() { | ||
return nil, coreiface.ErrOffline | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
return dagnode, nil | ||
} | ||
|
||
func (api *CoreAPI) Cat(p string) (coreiface.Data, error) { | ||
dagnode, err := api.resolve(p) | ||
if err != nil { | ||
return nil, err | ||
} | ||
r, err := uio.NewDagReader(api.ctx, dagnode, api.node.DAG) | ||
if err == uio.ErrIsDir { | ||
return nil, coreiface.ErrDir | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
return r, nil | ||
} | ||
|
||
func (api *CoreAPI) Ls(p string) ([]coreiface.Link, error) { | ||
dagnode, err := api.resolve(p) | ||
if err != nil { | ||
return nil, err | ||
} | ||
links := make([]coreiface.Link, len(dagnode.Links)) | ||
for i, l := range dagnode.Links { | ||
links[i] = coreiface.Link{Name: l.Name, Size: l.Size, Hash: l.Hash} | ||
} | ||
return links, 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,38 @@ | ||
package iface | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
|
||
core "github.com/ipfs/go-ipfs/core" | ||
|
||
mh "gx/ipfs/QmYf7ng2hG5XBtJA3tN34DQ2GUN5HNksEw1rLDkmr6vGku/go-multihash" | ||
context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context" | ||
) | ||
|
||
type CoreAPI interface { | ||
Context() context.Context | ||
IpfsNode() *core.IpfsNode // XXX temporary | ||
Cat(string) (Data, error) | ||
Ls(string) ([]Link, error) | ||
} | ||
|
||
type Object struct { | ||
Links []Link | ||
Data Data | ||
} | ||
|
||
type Link struct { | ||
Name string // utf-8 | ||
Size uint64 | ||
Hash mh.Multihash | ||
} | ||
|
||
type Data interface { | ||
io.Reader | ||
io.Seeker | ||
io.Closer | ||
} | ||
|
||
var ErrDir = errors.New("object is a directory") | ||
var ErrOffline = errors.New("can't resolve, ipfs node is offline") |