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

import (
	"io"

Jeromy's avatar
Jeromy committed
6
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.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"
11
	uio "github.com/jbenet/go-ipfs/unixfs/io"
verokarhu's avatar
verokarhu committed
12 13 14 15 16 17 18
	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
19
	NewDagReader(nd *dag.Node) (io.Reader, error)
verokarhu's avatar
verokarhu committed
20 21 22 23 24 25 26 27 28 29 30
}

type ipfsHandler struct {
	node *core.IpfsNode
}

func (i *ipfsHandler) ResolvePath(path string) (*dag.Node, error) {
	return i.node.Resolver.ResolvePath(path)
}

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

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

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