-
Notifications
You must be signed in to change notification settings - Fork 92
/
data.go
251 lines (226 loc) · 6.7 KB
/
data.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package client
import (
"context"
"fmt"
"io"
"net/http"
"os"
"strings"
files "github.com/ipfs/go-ipfs-files"
httpapi "github.com/ipfs/go-ipfs-http-client"
"github.com/ipfs/interface-go-ipfs-core/options"
ipfspath "github.com/ipfs/interface-go-ipfs-core/path"
"github.com/multiformats/go-multiaddr"
userPb "github.com/textileio/powergate/api/gen/powergate/user/v1"
"github.com/textileio/powergate/util"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// Data provides access to Powergate general data APIs.
type Data struct {
client userPb.UserServiceClient
}
// WatchLogsOption is a function that changes GetLogsConfig.
type WatchLogsOption func(r *userPb.WatchLogsRequest)
// WithJobIDFilter filters only log messages of a Cid related to
// the Job with id jid.
func WithJobIDFilter(jobID string) WatchLogsOption {
return func(r *userPb.WatchLogsRequest) {
r.JobId = jobID
}
}
// WithHistory indicates that prior history logs should
// be sent in the channel before getting real time logs.
func WithHistory(enabled bool) WatchLogsOption {
return func(r *userPb.WatchLogsRequest) {
r.History = enabled
}
}
// WatchLogsEvent represents an event for watching cid logs.
type WatchLogsEvent struct {
Res *userPb.WatchLogsResponse
Err error
}
// Stage allows to temporarily stage data in hot storage in preparation for pushing a cid storage config.
func (d *Data) Stage(ctx context.Context, data io.Reader) (*userPb.StageResponse, error) {
stream, err := d.client.Stage(ctx)
if err != nil {
return nil, err
}
buffer := make([]byte, 1024*32) // 32KB
for {
bytesRead, err := data.Read(buffer)
if err != nil && err != io.EOF {
return nil, err
}
sendErr := stream.Send(&userPb.StageRequest{Chunk: buffer[:bytesRead]})
if sendErr != nil {
if sendErr == io.EOF {
var noOp interface{}
return nil, stream.RecvMsg(noOp)
}
return nil, sendErr
}
if err == io.EOF {
break
}
}
return stream.CloseAndRecv()
}
// StageFolder allows to temporarily stage a folder in hot storage in preparation for pushing a cid storage config.
func (d *Data) StageFolder(ctx context.Context, ipfsRevProxyAddr string, folderPath string) (string, error) {
ffsToken := ctx.Value(AuthKey).(string)
ipfs, err := newDecoratedIPFSAPI(ipfsRevProxyAddr, ffsToken)
if err != nil {
return "", fmt.Errorf("creating IPFS HTTP client: %s", err)
}
stat, err := os.Lstat(folderPath)
if err != nil {
return "", err
}
ff, err := files.NewSerialFile(folderPath, false, stat)
if err != nil {
return "", err
}
defer func() { _ = ff.Close() }()
opts := []options.UnixfsAddOption{
options.Unixfs.CidVersion(1),
options.Unixfs.Pin(true),
}
pth, err := ipfs.Unixfs().Add(context.Background(), files.ToDir(ff), opts...)
if err != nil {
return "", err
}
_, err = d.client.StageCid(ctx, &userPb.StageCidRequest{Cid: pth.Cid().String()})
if err != nil {
return "", fmt.Errorf("stage pinning cid: %s", err)
}
return pth.Cid().String(), nil
}
// ReplaceData pushes a StorageConfig for c2 equal to that of c1, and removes c1. This operation
// is more efficient than manually removing and adding in two separate operations.
func (d *Data) ReplaceData(ctx context.Context, cid1, cid2 string) (*userPb.ReplaceDataResponse, error) {
return d.client.ReplaceData(ctx, &userPb.ReplaceDataRequest{Cid1: cid1, Cid2: cid2})
}
// Get returns an io.Reader for reading a stored Cid from hot storage.
func (d *Data) Get(ctx context.Context, cid string) (io.Reader, error) {
stream, err := d.client.Get(ctx, &userPb.GetRequest{
Cid: cid,
})
if err != nil {
return nil, err
}
reader, writer := io.Pipe()
go func() {
for {
res, err := stream.Recv()
if err == io.EOF {
_ = writer.Close()
break
} else if err != nil {
_ = writer.CloseWithError(err)
break
}
_, err = writer.Write(res.GetChunk())
if err != nil {
_ = writer.CloseWithError(err)
break
}
}
}()
return reader, nil
}
// GetFolder retrieves to outputDir a Cid which corresponds to a folder.
func (d *Data) GetFolder(ctx context.Context, ipfsRevProxyAddr, cid, outputDir string) error {
token := ctx.Value(AuthKey).(string)
ipfs, err := newDecoratedIPFSAPI(ipfsRevProxyAddr, token)
if err != nil {
return fmt.Errorf("creating decorated IPFS client: %s", err)
}
ci, err := util.CidFromString(cid)
if err != nil {
return fmt.Errorf("decoding cid: %s", err)
}
n, err := ipfs.Unixfs().Get(ctx, ipfspath.IpfsPath(ci))
if err != nil {
return fmt.Errorf("getting folder DAG from IPFS: %s", err)
}
err = files.WriteTo(n, outputDir)
if err != nil {
return fmt.Errorf("saving folder DAG to output folder: %s", err)
}
return nil
}
// WatchLogs pushes human-friendly messages about Cid executions. The method is blocking
// and will continue to send messages until the context is canceled. The provided channel
// is owned by the method and must not be closed.
func (d *Data) WatchLogs(ctx context.Context, ch chan<- WatchLogsEvent, cid string, opts ...WatchLogsOption) error {
r := &userPb.WatchLogsRequest{Cid: cid}
for _, opt := range opts {
opt(r)
}
stream, err := d.client.WatchLogs(ctx, r)
if err != nil {
return err
}
go func() {
for {
res, err := stream.Recv()
if err == io.EOF || status.Code(err) == codes.Canceled {
close(ch)
break
}
if err != nil {
ch <- WatchLogsEvent{Err: err}
close(ch)
break
}
ch <- WatchLogsEvent{Res: res}
}
}()
return nil
}
// CidInfo returns information about cids stored by the user.
func (d *Data) CidInfo(ctx context.Context, cids ...string) (*userPb.CidInfoResponse, error) {
return d.client.CidInfo(ctx, &userPb.CidInfoRequest{Cids: cids})
}
func newDecoratedIPFSAPI(proxyAddr, ffsToken string) (*httpapi.HttpApi, error) {
ipport := strings.Split(proxyAddr, ":")
if len(ipport) != 2 {
return nil, fmt.Errorf("ipfs addr is invalid")
}
cm, err := multiaddr.NewComponent("dns4", ipport[0])
if err != nil {
return nil, err
}
cp, err := multiaddr.NewComponent("tcp", ipport[1])
if err != nil {
return nil, err
}
useHTTPS := ipport[1] == "443"
ipfsMaddr := cm.Encapsulate(cp)
customClient := http.DefaultClient
customClient.Transport = newFFSHeaderDecorator(ffsToken, useHTTPS)
ipfs, err := httpapi.NewApiWithClient(ipfsMaddr, customClient)
if err != nil {
return nil, err
}
return ipfs, nil
}
type ffsHeaderDecorator struct {
ffsToken string
useHTTPS bool
}
func newFFSHeaderDecorator(ffsToken string, useHTTPS bool) *ffsHeaderDecorator {
return &ffsHeaderDecorator{
ffsToken: ffsToken,
useHTTPS: useHTTPS,
}
}
func (fhd ffsHeaderDecorator) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header["x-ipfs-ffs-auth"] = []string{fhd.ffsToken}
if fhd.useHTTPS {
req.URL.Scheme = "https"
}
return http.DefaultTransport.RoundTrip(req)
}