-
Notifications
You must be signed in to change notification settings - Fork 167
/
node.go
128 lines (107 loc) · 2.6 KB
/
node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package ipfs
import (
"context"
chunk "gx/ipfs/QmWo8jYc19ppG7YoTsrr2kEtLRbARTJho5oNXFTR6B7Peq/go-ipfs-chunker"
ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
"io"
"os"
"path/filepath"
"github.com/golang/glog"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/coreunix"
"github.com/ipfs/go-ipfs/importer/balanced"
ihelper "github.com/ipfs/go-ipfs/importer/helpers"
"github.com/ipfs/go-ipfs/importer/trickle"
"github.com/ipfs/go-ipfs/repo/config"
"github.com/ipfs/go-ipfs/repo/fsrepo"
)
type IpfsApi interface {
Add(r io.Reader) (string, error)
}
type IpfsCoreApi core.IpfsNode
const (
nBitsForKeypairDefault = 2048
)
func StartIpfs(ctx context.Context, repoPath string) (*IpfsCoreApi, error) {
if !fsrepo.IsInitialized(repoPath) {
conf, err := config.Init(os.Stdout, nBitsForKeypairDefault)
if err != nil {
return nil, err
}
if err := fsrepo.Init(repoPath, conf); err != nil {
return nil, err
}
}
repo, err := fsrepo.Open(repoPath)
if err != nil {
return nil, err
}
ncfg := &core.BuildCfg{
Repo: repo,
Online: true,
Permanent: true,
Routing: core.DHTOption,
}
node, err := core.NewNode(ctx, ncfg)
if err != nil {
return nil, err
}
go func() {
for {
select {
case <-ctx.Done():
glog.Infof("Closing IPFS...")
closeIpfs(node, repoPath)
return
}
}
}()
return (*IpfsCoreApi)(node), nil
}
func closeIpfs(node *core.IpfsNode, repoPath string) {
repoLockFile := filepath.Join(repoPath, fsrepo.LockFile)
os.Remove(repoLockFile)
node.Close()
}
func (ipfs *IpfsCoreApi) Add(r io.Reader) (string, error) {
node := ipfs.node()
return addAndPin(node.Context(), node, r)
}
func addAndPin(ctx context.Context, n *core.IpfsNode, r io.Reader) (string, error) {
defer n.Blockstore.PinLock().Unlock()
fileAdder, err := coreunix.NewAdder(n.Context(), n.Pinning, n.Blockstore, n.DAG)
if err != nil {
return "", err
}
chnk, err := chunk.FromString(r, fileAdder.Chunker)
if err != nil {
return "", err
}
params := ihelper.DagBuilderParams{
Dagserv: n.DAG,
RawLeaves: fileAdder.RawLeaves,
Maxlinks: ihelper.DefaultLinksPerBlock,
NoCopy: fileAdder.NoCopy,
Prefix: fileAdder.Prefix,
}
var node ipld.Node
if fileAdder.Trickle {
node, err = trickle.Layout(params.New(chnk))
if err != nil {
return "", err
}
} else {
node, err = balanced.Layout(params.New(chnk))
if err != nil {
return "", err
}
}
err = fileAdder.PinRoot()
if err != nil {
return "", err
}
return node.Cid().String(), nil
}
func (ipfs *IpfsCoreApi) node() *core.IpfsNode {
return (*core.IpfsNode)(ipfs)
}