-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.go
175 lines (135 loc) · 4.11 KB
/
api.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package ipfs
import (
"context"
"fmt"
"net/url"
"time"
filestore "github.com/ipfs/go-filestore"
files "github.com/ipfs/go-ipfs-files"
core "github.com/ipfs/go-ipfs/core"
coreapi "github.com/ipfs/go-ipfs/core/coreapi"
icore "github.com/ipfs/interface-go-ipfs-core"
ioptions "github.com/ipfs/interface-go-ipfs-core/options"
path "github.com/ipfs/interface-go-ipfs-core/path"
)
var ipfsNode *core.IpfsNode
var hasNode = false
// HasNode – checks if we already have a node on memory
func HasNode(v core.IpfsNode) bool {
return hasNode
}
// GetNode – gets current node in memory
func GetNode() *core.IpfsNode {
if hasNode {
return ipfsNode
}
return nil
}
// StartNode – gets and starts a node in case it wasnt started yet
func StartNode(repoPath string) {
if !hasNode {
fmt.Println("-- Getting an IPFS node running -- ")
ctx, _ := context.WithCancel(context.Background())
fmt.Println("Spawning node on the provided repo path")
if err := setupPlugins(repoPath); err != nil {
panic(fmt.Errorf("error while setting up plugins: %s", err))
}
cfg, err := createConfig()
if err != nil {
panic(fmt.Errorf("failed to create ipfs config: %s", err))
}
node, err := createNode(ctx, cfg, repoPath)
if err != nil {
panic(fmt.Errorf("failed to spawn ipfs node: %s", err))
}
fmt.Println("IPFS node is running")
printSwarmAddrs(node)
// construct http gateway
_, err = serveHTTPGateway(node, cfg)
if err != nil {
panic(fmt.Errorf("failed to spawn http gateway: %s", err))
}
ipfsNode = node
hasNode = true
}
}
// GetCoreAPI – returns the ipfs node wrapped with the core API interface
func GetCoreAPI() (icore.CoreAPI, error) {
if !hasNode {
return nil, fmt.Errorf("error getting CoreAPI: doesnt exist yet")
}
return coreapi.NewCoreAPI(&*ipfsNode)
}
// HasFile – checks if ipfs has the specific file
func HasFile(multihash string) (bool, error) {
fmt.Println("-- HasFile: before has node -- ")
if !hasNode {
return false, fmt.Errorf("error checking multihash: node is off")
}
fmt.Println("-- HasFile: before GetCoreAPI -- ")
api, _ := GetCoreAPI()
fmt.Println("-- HasFile: before context.WithTimeout -- ")
ctx, cancel := context.WithTimeout(ipfsNode.Context(), 5*time.Second)
defer cancel()
fmt.Println("-- HasFile: before Unixfs.Ls -- ")
results, err := api.Unixfs().Ls(ctx, path.New(multihash))
fmt.Println("-- HasFile: before Unixfs.Ls err -- ")
if err != nil {
return false, fmt.Errorf("error ls multihash: ", err)
}
fmt.Println("-- HasFile: before for results -- ")
for link := range results {
if link.Err != nil {
return false, nil
// panic(link.Err)
}
return true, nil
}
fmt.Println("-- HasFile: before return -- ")
return false, nil
}
// AddFromURL – adds a file from an url
func AddFromURL(urlString string, pin bool) (string, error) {
if !hasNode {
return "", fmt.Errorf("error adding from url: node is off")
}
if !filestore.IsURL(urlString) {
return "", fmt.Errorf("unsupported url syntax: %s", urlString)
}
api, _ := GetCoreAPI()
ctx, cancel := context.WithCancel(ipfsNode.Context())
defer cancel()
opts := []ioptions.UnixfsAddOption{
ioptions.Unixfs.Pin(pin),
ioptions.Unixfs.CidVersion(0),
ioptions.Unixfs.Nocopy(false),
}
url, err := url.Parse(urlString)
if err != nil {
return "", fmt.Errorf("error parsing url: ", err)
}
file := files.NewWebFile(url)
pth, err := api.Unixfs().Add(ctx, file, opts...)
if err != nil {
return "", fmt.Errorf("error adding url: ", err)
}
return pth.Cid().String(), nil
}
// // PinNode pins an ipld node
// func PinNode(node *core.IpfsNode, nd ipld.Node, recursive bool) error {
// ctx, cancel := context.WithTimeout(node.Context(), PinTimeout)
// defer cancel()
// defer node.Blockstore.PinLock().Unlock()
// err := node.Pinning.Pin(ctx, nd, recursive)
// if err != nil {
// if strings.Contains(err.Error(), "already pinned recursively") {
// return nil
// }
// return err
// }
// return node.Pinning.Flush()
// }
// // UnpinNode unpins an ipld node
// func UnpinNode(node *core.IpfsNode, nd ipld.Node, recursive bool) error {
// return UnpinCid(node, nd.Cid(), recursive)
// }