mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-02 03:28:25 +08:00
refactor: move add and cat to the core
This commit is contained in:
33
core/core.go
33
core/core.go
@ -3,6 +3,7 @@ package core
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||||
b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
|
b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
|
||||||
@ -18,6 +19,8 @@ import (
|
|||||||
bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network"
|
bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network"
|
||||||
"github.com/jbenet/go-ipfs/exchange/offline"
|
"github.com/jbenet/go-ipfs/exchange/offline"
|
||||||
mount "github.com/jbenet/go-ipfs/fuse/mount"
|
mount "github.com/jbenet/go-ipfs/fuse/mount"
|
||||||
|
importer "github.com/jbenet/go-ipfs/importer"
|
||||||
|
chunk "github.com/jbenet/go-ipfs/importer/chunk"
|
||||||
merkledag "github.com/jbenet/go-ipfs/merkledag"
|
merkledag "github.com/jbenet/go-ipfs/merkledag"
|
||||||
namesys "github.com/jbenet/go-ipfs/namesys"
|
namesys "github.com/jbenet/go-ipfs/namesys"
|
||||||
ic "github.com/jbenet/go-ipfs/p2p/crypto"
|
ic "github.com/jbenet/go-ipfs/p2p/crypto"
|
||||||
@ -29,6 +32,8 @@ import (
|
|||||||
pin "github.com/jbenet/go-ipfs/pin"
|
pin "github.com/jbenet/go-ipfs/pin"
|
||||||
routing "github.com/jbenet/go-ipfs/routing"
|
routing "github.com/jbenet/go-ipfs/routing"
|
||||||
dht "github.com/jbenet/go-ipfs/routing/dht"
|
dht "github.com/jbenet/go-ipfs/routing/dht"
|
||||||
|
uio "github.com/jbenet/go-ipfs/unixfs/io"
|
||||||
|
u "github.com/jbenet/go-ipfs/util"
|
||||||
ds2 "github.com/jbenet/go-ipfs/util/datastore2"
|
ds2 "github.com/jbenet/go-ipfs/util/datastore2"
|
||||||
debugerror "github.com/jbenet/go-ipfs/util/debugerror"
|
debugerror "github.com/jbenet/go-ipfs/util/debugerror"
|
||||||
eventlog "github.com/jbenet/go-ipfs/util/eventlog"
|
eventlog "github.com/jbenet/go-ipfs/util/eventlog"
|
||||||
@ -274,6 +279,34 @@ func (n *IpfsNode) Bootstrap(ctx context.Context, peers []peer.PeerInfo) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO we may not want to add these methods to the core. Maybe they should be
|
||||||
|
// defined as free functions in another package that use public fields on the
|
||||||
|
// node.
|
||||||
|
//
|
||||||
|
// e.g. reader, err := unix.Cat(node)
|
||||||
|
|
||||||
|
func (n *IpfsNode) Cat(k u.Key) (io.Reader, error) {
|
||||||
|
catterdag := n.DAG
|
||||||
|
nodeCatted, err := (&path.Resolver{catterdag}).ResolvePath(k.String())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return uio.NewDagReader(nodeCatted, catterdag)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *IpfsNode) Add(r io.Reader) (u.Key, error) {
|
||||||
|
nodeAdded, err := importer.BuildDagFromReader(
|
||||||
|
r,
|
||||||
|
n.DAG,
|
||||||
|
nil,
|
||||||
|
chunk.DefaultSplitter,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return nodeAdded.Key()
|
||||||
|
}
|
||||||
|
|
||||||
func (n *IpfsNode) loadID() error {
|
func (n *IpfsNode) loadID() error {
|
||||||
if n.Identity != "" {
|
if n.Identity != "" {
|
||||||
return debugerror.New("identity already loaded")
|
return debugerror.New("identity already loaded")
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package epictest
|
package epictest
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
|
||||||
|
|
||||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||||
datastore "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
|
datastore "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
|
||||||
sync "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
|
sync "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
|
||||||
@ -13,15 +11,10 @@ import (
|
|||||||
exchange "github.com/jbenet/go-ipfs/exchange"
|
exchange "github.com/jbenet/go-ipfs/exchange"
|
||||||
bitswap "github.com/jbenet/go-ipfs/exchange/bitswap"
|
bitswap "github.com/jbenet/go-ipfs/exchange/bitswap"
|
||||||
bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network"
|
bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network"
|
||||||
importer "github.com/jbenet/go-ipfs/importer"
|
|
||||||
chunk "github.com/jbenet/go-ipfs/importer/chunk"
|
|
||||||
merkledag "github.com/jbenet/go-ipfs/merkledag"
|
merkledag "github.com/jbenet/go-ipfs/merkledag"
|
||||||
host "github.com/jbenet/go-ipfs/p2p/host"
|
host "github.com/jbenet/go-ipfs/p2p/host"
|
||||||
peer "github.com/jbenet/go-ipfs/p2p/peer"
|
peer "github.com/jbenet/go-ipfs/p2p/peer"
|
||||||
path "github.com/jbenet/go-ipfs/path"
|
|
||||||
dht "github.com/jbenet/go-ipfs/routing/dht"
|
dht "github.com/jbenet/go-ipfs/routing/dht"
|
||||||
uio "github.com/jbenet/go-ipfs/unixfs/io"
|
|
||||||
util "github.com/jbenet/go-ipfs/util"
|
|
||||||
"github.com/jbenet/go-ipfs/util/datastore2"
|
"github.com/jbenet/go-ipfs/util/datastore2"
|
||||||
delay "github.com/jbenet/go-ipfs/util/delay"
|
delay "github.com/jbenet/go-ipfs/util/delay"
|
||||||
eventlog "github.com/jbenet/go-ipfs/util/eventlog"
|
eventlog "github.com/jbenet/go-ipfs/util/eventlog"
|
||||||
@ -43,28 +36,6 @@ func (c *Core) Bootstrap(ctx context.Context, p peer.PeerInfo) error {
|
|||||||
return c.IpfsNode.Bootstrap(ctx, []peer.PeerInfo{p})
|
return c.IpfsNode.Bootstrap(ctx, []peer.PeerInfo{p})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Core) Cat(k util.Key) (io.Reader, error) {
|
|
||||||
catterdag := c.IpfsNode.DAG
|
|
||||||
nodeCatted, err := (&path.Resolver{catterdag}).ResolvePath(k.String())
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return uio.NewDagReader(nodeCatted, catterdag)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Core) Add(r io.Reader) (util.Key, error) {
|
|
||||||
nodeAdded, err := importer.BuildDagFromReader(
|
|
||||||
r,
|
|
||||||
c.IpfsNode.DAG,
|
|
||||||
nil,
|
|
||||||
chunk.DefaultSplitter,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return nodeAdded.Key()
|
|
||||||
}
|
|
||||||
|
|
||||||
func makeCore(ctx context.Context, rf RepoFactory) (*Core, error) {
|
func makeCore(ctx context.Context, rf RepoFactory) (*Core, error) {
|
||||||
node, err := rf(ctx)
|
node, err := rf(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Reference in New Issue
Block a user