-
Notifications
You must be signed in to change notification settings - Fork 5
/
ipfs.go
189 lines (168 loc) · 4.37 KB
/
ipfs.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package rpcx
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"sync"
"github.com/Rican7/retry"
"github.com/Rican7/retry/strategy"
shell "github.com/ipfs/go-ipfs-api"
"github.com/meshplus/bitxhub-model/pb"
)
// IPFSPutFromLocal puts local file to ipfs
// args@localPath e.g. /tmp/eg.json
// returns cid of file stored on ipfs
func (cli *ChainClient) IPFSPutFromLocal(localfPath string) (*pb.Response, error) {
res, err := cli.ipfsClient.PutFromLocal(localfPath)
if err != nil {
return nil, err
}
return &pb.Response{Data: res}, nil
}
// IPFSGet gets from ipfs
// args@path e.g. /ipfs/QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG/readme
func (cli *ChainClient) IPFSGet(path string) (*pb.Response, error) {
res, err := cli.ipfsClient.Get(path)
if err != nil {
return nil, err
}
return &pb.Response{Data: res}, nil
}
// IPFSGetToLocal gets from ipfs and saves to local file path
// args@path e.g. /ipfs/QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG/readme
// args@localPath e.g. /tmp/readme
func (cli *ChainClient) IPFSGetToLocal(path string, localfPath string) (*pb.Response, error) {
err := cli.ipfsClient.GetToLocal(path, localfPath)
if err != nil {
return nil, err
}
return nil, nil
}
// IPFSClient .
type IPFSClient struct {
apiShells sync.Map //map[string]*shell.Shell
}
// NewIPFSClient .
func NewIPFSClient(options ...func(*IPFSClient)) (*IPFSClient, error) {
// make(map[string]*shell.Shell)
c := &IPFSClient{
apiShells: sync.Map{},
}
for _, option := range options {
option(c)
}
return c, nil
}
// WithAPIAddrs returns ipfs client
// e.g []string{"http://localhost:5001"}
func WithAPIAddrs(addrs []string) func(*IPFSClient) {
return func(i *IPFSClient) {
for _, addr := range addrs {
i.AddAPIShell(addr)
}
}
}
// AddAPIShell add ipfs api address
func (ipfsClient *IPFSClient) AddAPIShell(addr string) {
ipfsClient.apiShells.Store(addr, shell.NewShell(addr))
// ipfsClient.apiShells[addr] = shell.NewShell(addr)
}
// RmAPIAddr rm ipfs api address
func (ipfsClient *IPFSClient) RmAPIAddr(addr string) {
ipfsClient.apiShells.Delete(addr)
// delete(ipfsClient.apiShells, addr)
}
// IPFSResponse describes ipfs add response
type IPFSResponse struct {
Name string `json:"Name"`
Hash string `json:"Hash"`
Size string `json:"Size"`
}
// PutFromLocal puts local file to ipfs
// args@localPath e.g. /tmp/eg.json
// returns cid of file stored on ipfs
func (ipfsClient *IPFSClient) PutFromLocal(localfPath string) ([]byte, error) {
var shells []*shell.Shell
ipfsClient.apiShells.Range(func(key interface{}, value interface{}) bool {
shells = append(shells, value.(*shell.Shell))
return true
})
if len(shells) <= 0 {
return nil, fmt.Errorf("api shells are null")
}
limit := uint(len(shells) - 1)
var response string
err := retry.Retry(func(attempt uint) error {
localFile, err := os.Open(localfPath)
if err != nil {
return err
}
response, err = shells[attempt].Add(localFile)
if err != nil {
return err
}
localFile.Close()
return nil
},
strategy.Limit(limit),
)
if err != nil {
return nil, err
}
return []byte(response), nil
}
// Get gets from ipfs
// args@path e.g. /ipfs/QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG/readme
// returns content of file
func (ipfsClient *IPFSClient) Get(path string) ([]byte, error) {
var shells []*shell.Shell
ipfsClient.apiShells.Range(func(key interface{}, value interface{}) bool {
shells = append(shells, value.(*shell.Shell))
return true
})
if len(shells) <= 0 {
return nil, fmt.Errorf("api shells are null")
}
limit := uint(len(shells) - 1)
var response []byte
err := retry.Retry(func(attempt uint) error {
res, err := shells[attempt].Cat(path)
if err != nil {
return err
}
defer res.Close()
response, err = ioutil.ReadAll(res)
if err != nil {
return err
}
return nil
},
strategy.Limit(limit),
)
if err != nil {
return nil, err
}
return response, err
}
// GetToLocal gets from ipfs and saves to local file path
// args@path e.g. /ipfs/QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG/readme
// args@localPath e.g. /tmp/readme
func (ipfsClient *IPFSClient) GetToLocal(path string, localfPath string) error {
content, err := ipfsClient.Get(path)
if err != nil {
return err
}
f, err := os.Create(localfPath)
if err != nil {
return err
}
w := bufio.NewWriter(f)
_, err = w.WriteString(string(content))
if err != nil {
return err
}
w.Flush()
f.Close()
return nil
}