Skip to content
This repository was archived by the owner on Apr 18, 2024. It is now read-only.

Commit 54a83bc

Browse files
author
Alan Shaw
committed
feat: working get
1 parent 384200f commit 54a83bc

File tree

7 files changed

+266
-115
lines changed

7 files changed

+266
-115
lines changed

client.go

Lines changed: 12 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -18,77 +18,25 @@ import (
1818
ds "github.com/ipfs/go-datastore"
1919
dssync "github.com/ipfs/go-datastore/sync"
2020
blockstore "github.com/ipfs/go-ipfs-blockstore"
21-
files "github.com/ipfs/go-ipfs-files"
22-
ipld "github.com/ipfs/go-ipld-format"
2321
"github.com/ipfs/go-merkledag"
24-
unixfile "github.com/ipfs/go-unixfs/file"
2522
"github.com/ipfs/ipfs-cluster/api"
2623
"github.com/ipld/go-car"
2724
peer "github.com/libp2p/go-libp2p-core/peer"
2825
"github.com/web3-storage/go-w3s-client/adder"
26+
w3http "github.com/web3-storage/go-w3s-client/http"
2927
)
3028

3129
const targetChunkSize = 1024 * 1024 * 10
3230
const iso8601 = "2006-01-02T15:04:05Z0700"
3331

3432
// Client is a HTTP API client to the web3.storage service.
3533
type Client interface {
36-
Get(context.Context, cid.Cid) (*Web3Response, error)
34+
Get(context.Context, cid.Cid) (*w3http.Web3Response, error)
3735
Put(context.Context, fs.File, ...PutOption) (cid.Cid, error)
3836
PutCar(context.Context, io.Reader) (cid.Cid, error)
3937
Status(context.Context, cid.Cid) (*Status, error)
4038
}
4139

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-
9240
type PinStatus int
9341

9442
const (
@@ -284,9 +232,9 @@ type clientConfig struct {
284232
}
285233

286234
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
290238
}
291239

292240
// NewClient creates a new web3.storage API client.
@@ -304,22 +252,14 @@ func NewClient(options ...Option) (Client, error) {
304252
}
305253
c := client{cfg: &cfg, hc: &http.Client{}}
306254
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)
309259
}
310260
return &c, nil
311261
}
312262

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-
323263
// TODO: retry
324264
func (c *client) sendCar(ctx context.Context, r io.Reader) (cid.Cid, error) {
325265
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) {
346286
return cid.Parse(out.Cid)
347287
}
348288

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) {
350290
req, err := http.NewRequest("GET", fmt.Sprintf("%s/car/%s", c.cfg.endpoint, cid), nil)
351291
if err != nil {
352292
return nil, err
353293
}
354294
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.cfg.token))
355295
res, err := c.hc.Do(req)
356-
return &Web3Response{res}, err
296+
return w3http.NewWeb3Response(res, c.bsvc), err
357297
}
358298

359299
type putConfig struct {
@@ -373,16 +313,12 @@ func (c *client) Put(ctx context.Context, file fs.File, options ...PutOption) (c
373313
}
374314
}
375315

376-
dag := c.dag
377-
if dag == nil {
378-
dag = newMemDag()
379-
}
380-
381316
info, err := file.Stat()
382317
if err != nil {
383318
return cid.Undef, err
384319
}
385320

321+
dag := merkledag.NewDAGService(c.bsvc)
386322
dagFmtr, err := adder.NewAdder(ctx, dag)
387323
if err != nil {
388324
return cid.Undef, err

example/go.sum

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,8 @@ github.com/ipfs/go-ds-leveldb v0.1.0/go.mod h1:hqAW8y4bwX5LWcCtku2rFNX3vjDZCy5LZ
408408
github.com/ipfs/go-ds-leveldb v0.4.1/go.mod h1:jpbku/YqBSsBc1qgME8BkWS4AxzF2cEu1Ii2r79Hh9s=
409409
github.com/ipfs/go-ds-leveldb v0.4.2/go.mod h1:jpbku/YqBSsBc1qgME8BkWS4AxzF2cEu1Ii2r79Hh9s=
410410
github.com/ipfs/go-fetcher v1.4.0/go.mod h1:5pDZ0393oRF/fHiLmtFZtpMNBQfHOYNPtryWedVuSWE=
411+
github.com/ipfs/go-fetcher v1.5.0 h1:oreKTKBzja3S09rSmoZlA3KGVlRiUbJ1pQjtB4K6y3w=
412+
github.com/ipfs/go-fetcher v1.5.0/go.mod h1:5pDZ0393oRF/fHiLmtFZtpMNBQfHOYNPtryWedVuSWE=
411413
github.com/ipfs/go-fs-lock v0.0.7/go.mod h1:Js8ka+FNYmgQRLrRXzU3CB/+Csr1BwrRilEcvYrHhhc=
412414
github.com/ipfs/go-ipfs-api v0.2.0/go.mod h1:zCTyTl+BuyvUqoSmVb8vjezCJLVTW7G/HBZbCXpTgeM=
413415
github.com/ipfs/go-ipfs-blockstore v0.0.1/go.mod h1:d3WClOmRQKFnJ0Jz/jj/zmksX0ma1gROTlovZKBmN08=
@@ -488,8 +490,9 @@ github.com/ipfs/go-metrics-interface v0.0.1/go.mod h1:6s6euYU4zowdslK0GKHmqaIZ3j
488490
github.com/ipfs/go-mfs v0.1.3-0.20210507195338-96fbfa122164 h1:0ATu9s5KktHhm8aYRSe1ysOJPik3dRwU/uag1Bcz+tg=
489491
github.com/ipfs/go-mfs v0.1.3-0.20210507195338-96fbfa122164/go.mod h1:A525zyeY2o078AoxhjJirOlDTXI1GnZxiYQnESGJ9WU=
490492
github.com/ipfs/go-path v0.0.7/go.mod h1:6KTKmeRnBXgqrTvzFrPV3CamxcgvXX/4z79tfAd2Sno=
491-
github.com/ipfs/go-path v0.1.0 h1:fUWwdxBtSD3mY9E+S2BHcAbJUM/heowfFSxZ/EozuXI=
492493
github.com/ipfs/go-path v0.1.0/go.mod h1:ZrGkobObZhQ2jIi7pgnIpn74BTpfbiTr+ih92Z2wXhE=
494+
github.com/ipfs/go-path v0.1.1 h1:0rfiI0IoNTYUyQN0ifz2zQBR6mZhOKv7qW5Jjx/4fG8=
495+
github.com/ipfs/go-path v0.1.1/go.mod h1:vC8q4AKOtrjJz2NnllIrmr2ZbGlF5fW2OKKyhV9ggb0=
493496
github.com/ipfs/go-peertaskqueue v0.0.4/go.mod h1:03H8fhyeMfKNFWqzYEVyMbcPUeYrqP1MX6Kd+aN+rMQ=
494497
github.com/ipfs/go-peertaskqueue v0.1.0/go.mod h1:Jmk3IyCcfl1W3jTW3YpghSwSEC6IJ3Vzz/jUmWw8Z0U=
495498
github.com/ipfs/go-peertaskqueue v0.1.1/go.mod h1:Jmk3IyCcfl1W3jTW3YpghSwSEC6IJ3Vzz/jUmWw8Z0U=
@@ -498,6 +501,7 @@ github.com/ipfs/go-peertaskqueue v0.2.0/go.mod h1:5/eNrBEbtSKWCG+kQK8K8fGNixoYUn
498501
github.com/ipfs/go-unixfs v0.2.4/go.mod h1:SUdisfUjNoSDzzhGVxvCL9QO/nKdwXdr+gbMUdqcbYw=
499502
github.com/ipfs/go-unixfs v0.2.6 h1:gq3U3T2vh8x6tXhfo3uSO3n+2z4yW0tYtNgVP/3sIyA=
500503
github.com/ipfs/go-unixfs v0.2.6/go.mod h1:GTTzQvaZsTZARdNkkdjDKFFnBhmO3e5mIM1PkH/x4p0=
504+
github.com/ipfs/go-unixfsnode v1.1.2 h1:aTsCdhwU0F4dMShMwYGroAj4v4EzSONLdoENebvTRb0=
501505
github.com/ipfs/go-unixfsnode v1.1.2/go.mod h1:5dcE2x03pyjHk4JjamXmunTMzz+VUtqvPwZjIEkfV6s=
502506
github.com/ipfs/go-verifcid v0.0.1 h1:m2HI7zIuR5TFyQ1b79Da5N9dnnCP1vcu2QqawmWlK2E=
503507
github.com/ipfs/go-verifcid v0.0.1/go.mod h1:5Hrva5KBeIog4A+UpqlaIU+DEstipcJYQQZc0g37pY0=

example/main.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ func main() {
2222
panic(err)
2323
}
2424

25-
cid := putSingleFile(c)
26-
getStatusForCid(c, cid)
25+
// cid := putSingleFile(c)
26+
// getStatusForCid(c, cid)
27+
28+
getFiles(c)
2729
}
2830

2931
func putSingleFile(c w3s.Client) cid.Cid {
@@ -91,3 +93,35 @@ func getStatusForKnownCid(c w3s.Client) {
9193
cid, _ := cid.Parse("bafybeig7qnlzyregxe2m63b4kkpx3ujqm5bwmn5wtvtftp7j27tmdtznji")
9294
getStatusForCid(c, cid)
9395
}
96+
97+
func getFiles(c w3s.Client) {
98+
cid, _ := cid.Parse("bafybeide43vps6vt2oo7nbqfwn5zz6l2alyi64mym3sb7reqhmypjnmej4")
99+
100+
res, err := c.Get(context.Background(), cid)
101+
if err != nil {
102+
panic(err)
103+
}
104+
105+
f, fsys, err := res.Files()
106+
if err != nil {
107+
panic(err)
108+
}
109+
110+
info, err := f.Stat()
111+
if err != nil {
112+
panic(err)
113+
}
114+
115+
if info.IsDir() {
116+
err = fs.WalkDir(fsys, "/", func(path string, d fs.DirEntry, err error) error {
117+
info, _ := d.Info()
118+
fmt.Printf("%s (%d bytes)\n", path, info.Size())
119+
return err
120+
})
121+
if err != nil {
122+
panic(err)
123+
}
124+
} else {
125+
fmt.Printf("%s (%d bytes)\n", cid.String(), info.Size())
126+
}
127+
}

0 commit comments

Comments
 (0)