Skip to content

Commit

Permalink
coreapi: start implementing it
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Lars Gierth <larsg@systemli.org>
  • Loading branch information
Lars Gierth committed Jun 19, 2016
1 parent 1afebc2 commit 5e328bf
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
64 changes: 64 additions & 0 deletions core/coreapi/coreapi.go
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
}
38 changes: 38 additions & 0 deletions core/coreapi/interface/interface.go
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")

0 comments on commit 5e328bf

Please sign in to comment.