/
client.go
412 lines (378 loc) · 10 KB
/
client.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package client
import (
"context"
"fmt"
"io"
"github.com/gogo/status"
"github.com/ipfs/go-cid"
"github.com/ipfs/interface-go-ipfs-core/path"
pb "github.com/textileio/textile/v2/api/buckets/pb"
"github.com/textileio/textile/v2/buckets"
"github.com/textileio/textile/v2/util"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
)
const (
// chunkSize for add file requests.
chunkSize = 1024 * 32
)
// Client provides the client api.
type Client struct {
c pb.APIServiceClient
conn *grpc.ClientConn
}
// NewClient starts the client.
func NewClient(target string, opts ...grpc.DialOption) (*Client, error) {
conn, err := grpc.Dial(target, opts...)
if err != nil {
return nil, err
}
return &Client{
c: pb.NewAPIServiceClient(conn),
conn: conn,
}, nil
}
// Close closes the client's grpc connection and cancels any active requests.
func (c *Client) Close() error {
return c.conn.Close()
}
// Create initializes a new bucket.
// The bucket name is only meant to help identify a bucket in a UI and is not unique.
func (c *Client) Create(ctx context.Context, opts ...CreateOption) (*pb.CreateResponse, error) {
args := &createOptions{}
for _, opt := range opts {
opt(args)
}
var strCid string
if args.fromCid.Defined() {
strCid = args.fromCid.String()
}
return c.c.Create(ctx, &pb.CreateRequest{
Name: args.name,
Private: args.private,
BootstrapCid: strCid,
})
}
// Root returns the bucket root.
func (c *Client) Root(ctx context.Context, key string) (*pb.RootResponse, error) {
return c.c.Root(ctx, &pb.RootRequest{
Key: key,
})
}
// Links returns a list of bucket path URL links.
func (c *Client) Links(ctx context.Context, key, pth string) (*pb.LinksResponse, error) {
return c.c.Links(ctx, &pb.LinksRequest{
Key: key,
Path: pth,
})
}
// List returns a list of all bucket roots.
func (c *Client) List(ctx context.Context) (*pb.ListResponse, error) {
return c.c.List(ctx, &pb.ListRequest{})
}
// ListIpfsPath returns items at a particular path in a UnixFS path living in the IPFS network.
func (c *Client) ListIpfsPath(ctx context.Context, pth path.Path) (*pb.ListIpfsPathResponse, error) {
return c.c.ListIpfsPath(ctx, &pb.ListIpfsPathRequest{Path: pth.String()})
}
// ListPath returns information about a bucket path.
func (c *Client) ListPath(ctx context.Context, key, pth string) (*pb.ListPathResponse, error) {
return c.c.ListPath(ctx, &pb.ListPathRequest{
Key: key,
Path: pth,
})
}
// SetPath set a particular path to an existing IPFS UnixFS DAG.
func (c *Client) SetPath(ctx context.Context, key, pth string, remoteCid cid.Cid) (*pb.SetPathResponse, error) {
return c.c.SetPath(ctx, &pb.SetPathRequest{
Key: key,
Path: pth,
Cid: remoteCid.String(),
})
}
type pushPathResult struct {
path path.Resolved
root path.Resolved
err error
}
// PushPath pushes a file to a bucket path.
// This will return the resolved path and the bucket's new root path.
func (c *Client) PushPath(ctx context.Context, key, pth string, reader io.Reader, opts ...Option) (result path.Resolved, root path.Resolved, err error) {
args := &options{}
for _, opt := range opts {
opt(args)
}
if args.progress != nil {
defer close(args.progress)
}
stream, err := c.c.PushPath(ctx)
if err != nil {
return nil, nil, err
}
var xr string
if args.root != nil {
xr = args.root.String()
}
if err = stream.Send(&pb.PushPathRequest{
Payload: &pb.PushPathRequest_Header_{
Header: &pb.PushPathRequest_Header{
Key: key,
Path: pth,
Root: xr,
},
},
}); err != nil {
return nil, nil, err
}
waitCh := make(chan pushPathResult)
go func() {
defer close(waitCh)
for {
rep, err := stream.Recv()
if err == io.EOF {
return
} else if err != nil {
waitCh <- pushPathResult{err: err}
return
}
switch payload := rep.Payload.(type) {
case *pb.PushPathResponse_Event_:
if payload.Event.Path != "" {
id, err := cid.Parse(payload.Event.Path)
if err != nil {
waitCh <- pushPathResult{err: err}
return
}
r, err := util.NewResolvedPath(payload.Event.Root.Path)
if err != nil {
waitCh <- pushPathResult{err: err}
return
}
waitCh <- pushPathResult{
path: path.IpfsPath(id),
root: r,
}
} else if args.progress != nil {
args.progress <- payload.Event.Bytes
}
case *pb.PushPathResponse_Error:
waitCh <- pushPathResult{err: fmt.Errorf(payload.Error)}
return
default:
waitCh <- pushPathResult{err: fmt.Errorf("invalid reply")}
return
}
}
}()
buf := make([]byte, chunkSize)
for {
n, err := reader.Read(buf)
if n > 0 {
if err := stream.Send(&pb.PushPathRequest{
Payload: &pb.PushPathRequest_Chunk{
Chunk: buf[:n],
},
}); err == io.EOF {
break
} else if err != nil {
_ = stream.CloseSend()
return nil, nil, err
}
}
if err == io.EOF {
break
} else if err != nil {
_ = stream.CloseSend()
return nil, nil, err
}
}
if err = stream.CloseSend(); err != nil {
return nil, nil, err
}
res := <-waitCh
return res.path, res.root, res.err
}
// PullPath pulls the bucket path, writing it to writer if it's a file.
func (c *Client) PullPath(ctx context.Context, key, pth string, writer io.Writer, opts ...Option) error {
args := &options{}
for _, opt := range opts {
opt(args)
}
if args.progress != nil {
defer close(args.progress)
}
stream, err := c.c.PullPath(ctx, &pb.PullPathRequest{
Key: key,
Path: pth,
})
if err != nil {
return err
}
var written int64
for {
rep, err := stream.Recv()
if err == io.EOF {
break
} else if err != nil {
return err
}
n, err := writer.Write(rep.Chunk)
if err != nil {
return err
}
written += int64(n)
if args.progress != nil {
args.progress <- written
}
}
return nil
}
// PullIpfsPath pulls the path from a remote UnixFS dag, writing it to writer if it's a file.
func (c *Client) PullIpfsPath(ctx context.Context, pth path.Path, writer io.Writer, opts ...Option) error {
args := &options{}
for _, opt := range opts {
opt(args)
}
if args.progress != nil {
defer close(args.progress)
}
stream, err := c.c.PullIpfsPath(ctx, &pb.PullIpfsPathRequest{
Path: pth.String(),
})
if err != nil {
return err
}
var written int64
for {
rep, err := stream.Recv()
if err == io.EOF {
break
} else if err != nil {
return err
}
n, err := writer.Write(rep.Chunk)
if err != nil {
return err
}
written += int64(n)
if args.progress != nil {
args.progress <- written
}
}
return nil
}
// Remove removes an entire bucket.
// Files and directories will be unpinned.
func (c *Client) Remove(ctx context.Context, key string) error {
_, err := c.c.Remove(ctx, &pb.RemoveRequest{
Key: key,
})
return err
}
// RemovePath removes the file or directory at path.
// Files and directories will be unpinned.
func (c *Client) RemovePath(ctx context.Context, key, pth string, opts ...Option) (path.Resolved, error) {
args := &options{}
for _, opt := range opts {
opt(args)
}
var xr string
if args.root != nil {
xr = args.root.String()
}
res, err := c.c.RemovePath(ctx, &pb.RemovePathRequest{
Key: key,
Path: pth,
Root: xr,
})
if err != nil {
return nil, err
}
return util.NewResolvedPath(res.Root.Path)
}
// PushPathAccessRoles updates path access roles by merging the pushed roles with existing roles.
// roles is a map of string marshaled public keys to path roles. A non-nil error is returned
// if the map keys are not unmarshalable to public keys.
// To delete a role for a public key, set its value to buckets.None.
func (c *Client) PushPathAccessRoles(ctx context.Context, key, pth string, roles map[string]buckets.Role) error {
pbroles, err := buckets.RolesToPb(roles)
if err != nil {
return err
}
_, err = c.c.PushPathAccessRoles(ctx, &pb.PushPathAccessRolesRequest{
Key: key,
Path: pth,
Roles: pbroles,
})
return err
}
// PullPathAccessRoles returns access roles for a path.
func (c *Client) PullPathAccessRoles(ctx context.Context, key, pth string) (map[string]buckets.Role, error) {
res, err := c.c.PullPathAccessRoles(ctx, &pb.PullPathAccessRolesRequest{
Key: key,
Path: pth,
})
if err != nil {
return nil, err
}
return buckets.RolesFromPb(res.Roles)
}
// DefaultArchiveConfig gets the default archive config for the specified Bucket.
func (c *Client) DefaultArchiveConfig(ctx context.Context, key string) (*pb.ArchiveConfig, error) {
res, err := c.c.DefaultArchiveConfig(ctx, &pb.DefaultArchiveConfigRequest{Key: key})
if err != nil {
return nil, err
}
return res.ArchiveConfig, nil
}
// SetDefaultArchiveConfig sets the default archive config for the specified Bucket.
func (c *Client) SetDefaultArchiveConfig(ctx context.Context, key string, config *pb.ArchiveConfig) error {
req := &pb.SetDefaultArchiveConfigRequest{
Key: key,
ArchiveConfig: config,
}
_, err := c.c.SetDefaultArchiveConfig(ctx, req)
return err
}
// Archive creates a Filecoin bucket archive via Powergate.
func (c *Client) Archive(ctx context.Context, key string, opts ...ArchiveOption) error {
req := &pb.ArchiveRequest{
Key: key,
}
for _, opt := range opts {
opt(req)
}
_, err := c.c.Archive(ctx, req)
return err
}
// ArchiveStatus returns the status of a Filecoin bucket archive.
func (c *Client) ArchiveStatus(ctx context.Context, key string) (*pb.ArchiveStatusResponse, error) {
return c.c.ArchiveStatus(ctx, &pb.ArchiveStatusRequest{
Key: key,
})
}
// ArchiveWatch watches status events from a Filecoin bucket archive.
func (c *Client) ArchiveWatch(ctx context.Context, key string, ch chan<- string) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
stream, err := c.c.ArchiveWatch(ctx, &pb.ArchiveWatchRequest{Key: key})
if err != nil {
return err
}
for {
reply, err := stream.Recv()
if err == io.EOF || status.Code(err) == codes.Canceled {
break
}
if err != nil {
return err
}
ch <- reply.Msg
}
return nil
}
// ArchiveInfo returns info about a Filecoin bucket archive.
func (c *Client) ArchiveInfo(ctx context.Context, key string) (*pb.ArchiveInfoResponse, error) {
return c.c.ArchiveInfo(ctx, &pb.ArchiveInfoRequest{
Key: key,
})
}