ipfs.go 1.16 KB
Newer Older
verokarhu's avatar
verokarhu committed
1 2 3 4 5
package http

import (
	"io"

6
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
verokarhu's avatar
verokarhu committed
7 8
	core "github.com/jbenet/go-ipfs/core"
	"github.com/jbenet/go-ipfs/importer"
Jeromy's avatar
Jeromy committed
9
	chunk "github.com/jbenet/go-ipfs/importer/chunk"
verokarhu's avatar
verokarhu committed
10
	dag "github.com/jbenet/go-ipfs/merkledag"
Jeromy's avatar
Jeromy committed
11
	path "github.com/jbenet/go-ipfs/path"
12
	uio "github.com/jbenet/go-ipfs/unixfs/io"
verokarhu's avatar
verokarhu committed
13 14 15 16 17 18 19
	u "github.com/jbenet/go-ipfs/util"
)

type ipfs interface {
	ResolvePath(string) (*dag.Node, error)
	NewDagFromReader(io.Reader) (*dag.Node, error)
	AddNodeToDAG(nd *dag.Node) (u.Key, error)
verokarhu's avatar
verokarhu committed
20
	NewDagReader(nd *dag.Node) (io.Reader, error)
verokarhu's avatar
verokarhu committed
21 22 23 24 25 26
}

type ipfsHandler struct {
	node *core.IpfsNode
}

Jeromy's avatar
Jeromy committed
27 28
func (i *ipfsHandler) ResolvePath(fpath string) (*dag.Node, error) {
	return i.node.Resolver.ResolvePath(path.Path(fpath))
verokarhu's avatar
verokarhu committed
29 30 31
}

func (i *ipfsHandler) NewDagFromReader(r io.Reader) (*dag.Node, error) {
Jeromy's avatar
Jeromy committed
32 33
	return importer.BuildDagFromReader(
		r, i.node.DAG, i.node.Pinning.GetManual(), chunk.DefaultSplitter)
verokarhu's avatar
verokarhu committed
34 35 36 37 38
}

func (i *ipfsHandler) AddNodeToDAG(nd *dag.Node) (u.Key, error) {
	return i.node.DAG.Add(nd)
}
verokarhu's avatar
verokarhu committed
39 40

func (i *ipfsHandler) NewDagReader(nd *dag.Node) (io.Reader, error) {
Jeromy's avatar
Jeromy committed
41
	return uio.NewDagReader(context.TODO(), nd, i.node.DAG)
verokarhu's avatar
verokarhu committed
42
}