@@ -18,77 +18,25 @@ import (
18
18
ds "github.com/ipfs/go-datastore"
19
19
dssync "github.com/ipfs/go-datastore/sync"
20
20
blockstore "github.com/ipfs/go-ipfs-blockstore"
21
- files "github.com/ipfs/go-ipfs-files"
22
- ipld "github.com/ipfs/go-ipld-format"
23
21
"github.com/ipfs/go-merkledag"
24
- unixfile "github.com/ipfs/go-unixfs/file"
25
22
"github.com/ipfs/ipfs-cluster/api"
26
23
"github.com/ipld/go-car"
27
24
peer "github.com/libp2p/go-libp2p-core/peer"
28
25
"github.com/web3-storage/go-w3s-client/adder"
26
+ w3http "github.com/web3-storage/go-w3s-client/http"
29
27
)
30
28
31
29
const targetChunkSize = 1024 * 1024 * 10
32
30
const iso8601 = "2006-01-02T15:04:05Z0700"
33
31
34
32
// Client is a HTTP API client to the web3.storage service.
35
33
type Client interface {
36
- Get (context.Context , cid.Cid ) (* Web3Response , error )
34
+ Get (context.Context , cid.Cid ) (* w3http. Web3Response , error )
37
35
Put (context.Context , fs.File , ... PutOption ) (cid.Cid , error )
38
36
PutCar (context.Context , io.Reader ) (cid.Cid , error )
39
37
Status (context.Context , cid.Cid ) (* Status , error )
40
38
}
41
39
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
- }
90
- }
91
-
92
40
type PinStatus int
93
41
94
42
const (
@@ -284,9 +232,9 @@ type clientConfig struct {
284
232
}
285
233
286
234
type client struct {
287
- cfg * clientConfig
288
- dag ipld. DAGService
289
- hc * http.Client
235
+ cfg * clientConfig
236
+ bsvc blockservice. BlockService
237
+ hc * http.Client
290
238
}
291
239
292
240
// NewClient creates a new web3.storage API client.
@@ -304,22 +252,14 @@ func NewClient(options ...Option) (Client, error) {
304
252
}
305
253
c := client {cfg : & cfg , hc : & http.Client {}}
306
254
if cfg .ds != nil {
307
- bs := bserv .New (blockstore .NewBlockstore (cfg .ds ), nil )
308
- c .dag = merkledag .NewDAGService (bs )
255
+ c .bsvc = bserv .New (blockstore .NewBlockstore (cfg .ds ), nil )
256
+ } else {
257
+ ds := dssync .MutexWrap (ds .NewMapDatastore ())
258
+ c .bsvc = bserv .New (blockstore .NewBlockstore (ds ), nil )
309
259
}
310
260
return & c , nil
311
261
}
312
262
313
- func newMemBlockService () blockservice.BlockService {
314
- ds := dssync .MutexWrap (ds .NewMapDatastore ())
315
- return bserv .New (blockstore .NewBlockstore (ds ), nil )
316
- }
317
-
318
- func newMemDag () ipld.DAGService {
319
- bs := newMemBlockService ()
320
- return merkledag .NewDAGService (bs )
321
- }
322
-
323
263
// TODO: retry
324
264
func (c * client ) sendCar (ctx context.Context , r io.Reader ) (cid.Cid , error ) {
325
265
req , err := http .NewRequestWithContext (ctx , "POST" , c .cfg .endpoint + "/car" , r )
@@ -346,14 +286,14 @@ func (c *client) sendCar(ctx context.Context, r io.Reader) (cid.Cid, error) {
346
286
return cid .Parse (out .Cid )
347
287
}
348
288
349
- func (c * client ) Get (ctx context.Context , cid cid.Cid ) (* Web3Response , error ) {
289
+ func (c * client ) Get (ctx context.Context , cid cid.Cid ) (* w3http. Web3Response , error ) {
350
290
req , err := http .NewRequest ("GET" , fmt .Sprintf ("%s/car/%s" , c .cfg .endpoint , cid ), nil )
351
291
if err != nil {
352
292
return nil , err
353
293
}
354
294
req .Header .Add ("Authorization" , fmt .Sprintf ("Bearer %s" , c .cfg .token ))
355
295
res , err := c .hc .Do (req )
356
- return & Web3Response { res } , err
296
+ return w3http . NewWeb3Response ( res , c . bsvc ) , err
357
297
}
358
298
359
299
type putConfig struct {
@@ -373,16 +313,12 @@ func (c *client) Put(ctx context.Context, file fs.File, options ...PutOption) (c
373
313
}
374
314
}
375
315
376
- dag := c .dag
377
- if dag == nil {
378
- dag = newMemDag ()
379
- }
380
-
381
316
info , err := file .Stat ()
382
317
if err != nil {
383
318
return cid .Undef , err
384
319
}
385
320
321
+ dag := merkledag .NewDAGService (c .bsvc )
386
322
dagFmtr , err := adder .NewAdder (ctx , dag )
387
323
if err != nil {
388
324
return cid .Undef , err
0 commit comments