Implement .Unixfs.Ls()
This commit was moved from ipfs/go-ipfs-http-client@eb1944fae32f4ccc43969ecd632681c2f2cbfa00
Łukasz Magiera committed
Jan 8, 2019 at 02:53 UTC
1acf4163902ae3b9e0e182761deafee5e67415ba
2 files changed
+54
-3
client/httpapi/response.go
+13
-1
@@ -33,7 +33,19 @@ func (r *Response) Decode(dec interface{}) error {
33
return r.Error
34
}
35
36
- return json.NewDecoder(r.Output).Decode(dec)
36
+ n := 0
37
+ var err error
38
+ for {
39
+ err = json.NewDecoder(r.Output).Decode(dec)
40
+ if err != nil {
41
+ break
42
+ }
43
+ n++
44
+ }
45
+ if n > 0 && err == io.EOF {
46
+ err = nil
47
+ }
48
+ return err
49
}
50
51
type Error struct {
client/httpapi/unixfs.go
+41
-2
@@ -4,6 +4,7 @@ import (
4
"context"
5
"fmt"
6
"github.com/ipfs/go-cid"
7
+ "github.com/pkg/errors"
8
9
"github.com/ipfs/go-ipfs/core/coreapi/interface"
10
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
@@ -11,6 +12,7 @@ import (
12
"github.com/ipfs/go-ipfs-files"
13
"github.com/ipfs/go-ipld-format"
14
mh "github.com/multiformats/go-multihash"
15
+ unixfspb "gx/ipfs/Qmbvw7kpSM2p6rbQ57WGRhhqNfCiNGW6EKH4xgHLw4bsnB/go-unixfs/pb"
16
)
17
18
type addEvent struct {
@@ -87,8 +89,45 @@ func (api *UnixfsAPI) Get(context.Context, iface.Path) (files.Node, error) {
89
panic("implement me")
90
}
91
90
-func (api *UnixfsAPI) Ls(context.Context, iface.Path) ([]*format.Link, error) {
91
- panic("implement me")
92
+type lsLink struct {
93
+ Name, Hash string
94
+ Size uint64
95
+ Type unixfspb.Data_DataType
96
+}
97
+
98
+type lsObject struct {
99
+ Hash string
100
+ Links []lsLink
101
+}
102
+
103
+type lsOutput struct {
104
+ Objects []lsObject
105
+}
106
+
107
+func (api *UnixfsAPI) Ls(ctx context.Context, p iface.Path) ([]*format.Link, error) {
108
+ var out lsOutput
109
+ err := api.core().request("ls", p.String()).Exec(ctx, &out)
110
+ if err != nil {
111
+ return nil, err
112
+ }
113
+
114
+ if len(out.Objects) != 1 {
115
+ return nil, errors.New("unexpected objects len")
116
+ }
117
+
118
+ links := make([]*format.Link, len(out.Objects[0].Links))
119
+ for i, l := range out.Objects[0].Links {
120
+ c, err := cid.Parse(l.Hash)
121
+ if err != nil {
122
+ return nil, err
123
+ }
124
+ links[i] = &format.Link{
125
+ Name: l.Name,
126
+ Size: l.Size,
127
+ Cid: c,
128
+ }
129
+ }
130
+ return links, nil
131
}
132
133
func (api *UnixfsAPI) core() *HttpApi {