@@ -12,13 +12,16 @@ import (
12
12
13
13
"github.com/alanshaw/go-carbites"
14
14
"github.com/filecoin-project/go-address"
15
+ "github.com/ipfs/go-blockservice"
15
16
bserv "github.com/ipfs/go-blockservice"
16
17
"github.com/ipfs/go-cid"
17
18
ds "github.com/ipfs/go-datastore"
18
19
dssync "github.com/ipfs/go-datastore/sync"
19
20
blockstore "github.com/ipfs/go-ipfs-blockstore"
21
+ files "github.com/ipfs/go-ipfs-files"
20
22
ipld "github.com/ipfs/go-ipld-format"
21
23
"github.com/ipfs/go-merkledag"
24
+ unixfile "github.com/ipfs/go-unixfs/file"
22
25
"github.com/ipfs/ipfs-cluster/api"
23
26
"github.com/ipld/go-car"
24
27
peer "github.com/libp2p/go-libp2p-core/peer"
@@ -30,15 +33,60 @@ const iso8601 = "2006-01-02T15:04:05Z0700"
30
33
31
34
// Client is a HTTP API client to the web3.storage service.
32
35
type Client interface {
33
- Get (context.Context , cid.Cid ) (GetResponse , error )
36
+ Get (context.Context , cid.Cid ) (* Web3Response , error )
34
37
Put (context.Context , fs.File , ... PutOption ) (cid.Cid , error )
35
38
PutCar (context.Context , io.Reader ) (cid.Cid , error )
36
- Status (context.Context , cid.Cid ) (Status , error )
39
+ Status (context.Context , cid.Cid ) (* Status , error )
37
40
}
38
41
39
- // GetResponse is a response to a call to the Get method.
40
- type GetResponse interface {
41
- Files () []fs.File
42
+ // Web3Response is a response to a call to the Get method.
43
+ type Web3Response struct {
44
+ * http.Response
45
+ }
46
+
47
+ // Files consumes the HTTP response and collects a slice of all UnixFs files.
48
+ func (r * Web3Response ) Files () (fs.File , error ) {
49
+ cr , err := car .NewCarReader (r .Body )
50
+ if err != nil {
51
+ return nil , err
52
+ }
53
+
54
+ bsvc := newMemBlockService ()
55
+ for {
56
+ b , err := cr .Next ()
57
+ if err != nil {
58
+ if err == io .EOF {
59
+ break
60
+ }
61
+ return nil , err
62
+ }
63
+ err = bsvc .AddBlock (b )
64
+ if err != nil {
65
+ return nil , err
66
+ }
67
+ }
68
+
69
+ ctx := r .Request .Context ()
70
+ dsvc := merkledag .NewDAGService (bsvc )
71
+
72
+ rootCid := cr .Header .Roots [0 ]
73
+ rootNd , err := dsvc .Get (ctx , rootCid )
74
+ if err != nil {
75
+ return nil , err
76
+ }
77
+
78
+ f , err := unixfile .NewUnixfsFile (ctx , dsvc , rootNd )
79
+ if err != nil {
80
+ return nil , err
81
+ }
82
+
83
+ return unixfsToFs (f )
84
+ }
85
+
86
+ func unixfsToFs (n files.Node ) (fs.File , error ) {
87
+ if d , ok := n .(files.Directory ); ok {
88
+
89
+ }
42
90
}
43
91
44
92
type PinStatus int
@@ -262,9 +310,13 @@ func NewClient(options ...Option) (Client, error) {
262
310
return & c , nil
263
311
}
264
312
265
- func newMemDag () ipld. DAGService {
313
+ func newMemBlockService () blockservice. BlockService {
266
314
ds := dssync .MutexWrap (ds .NewMapDatastore ())
267
- bs := bserv .New (blockstore .NewBlockstore (ds ), nil )
315
+ return bserv .New (blockstore .NewBlockstore (ds ), nil )
316
+ }
317
+
318
+ func newMemDag () ipld.DAGService {
319
+ bs := newMemBlockService ()
268
320
return merkledag .NewDAGService (bs )
269
321
}
270
322
@@ -294,8 +346,14 @@ func (c *client) sendCar(ctx context.Context, r io.Reader) (cid.Cid, error) {
294
346
return cid .Parse (out .Cid )
295
347
}
296
348
297
- func (c * client ) Get (ctx context.Context , cid cid.Cid ) (GetResponse , error ) {
298
- return nil , fmt .Errorf ("not implemented" )
349
+ func (c * client ) Get (ctx context.Context , cid cid.Cid ) (* Web3Response , error ) {
350
+ req , err := http .NewRequest ("GET" , fmt .Sprintf ("%s/car/%s" , c .cfg .endpoint , cid ), nil )
351
+ if err != nil {
352
+ return nil , err
353
+ }
354
+ req .Header .Add ("Authorization" , fmt .Sprintf ("Bearer %s" , c .cfg .token ))
355
+ res , err := c .hc .Do (req )
356
+ return & Web3Response {res }, err
299
357
}
300
358
301
359
type putConfig struct {
@@ -400,24 +458,24 @@ func (c *client) PutCar(ctx context.Context, car io.Reader) (cid.Cid, error) {
400
458
return root , sendErr
401
459
}
402
460
403
- func (c * client ) Status (ctx context.Context , cid cid.Cid ) (Status , error ) {
404
- var s Status
461
+ func (c * client ) Status (ctx context.Context , cid cid.Cid ) (* Status , error ) {
405
462
req , err := http .NewRequest ("GET" , fmt .Sprintf ("%s/status/%s" , c .cfg .endpoint , cid ), nil )
406
463
if err != nil {
407
- return s , err
464
+ return nil , err
408
465
}
409
466
req .Header .Add ("Authorization" , fmt .Sprintf ("Bearer %s" , c .cfg .token ))
410
467
res , err := c .hc .Do (req )
411
468
if err != nil {
412
- return s , err
469
+ return nil , err
413
470
}
414
471
if res .StatusCode != 200 {
415
- return s , fmt .Errorf ("unexpected response status: %d" , res .StatusCode )
472
+ return nil , fmt .Errorf ("unexpected response status: %d" , res .StatusCode )
416
473
}
474
+ var s Status
417
475
d := json .NewDecoder (res .Body )
418
476
err = d .Decode (& s )
419
477
if err != nil {
420
- return s , err
478
+ return nil , err
421
479
}
422
- return s , nil
480
+ return & s , nil
423
481
}
0 commit comments