-
Notifications
You must be signed in to change notification settings - Fork 1
/
operation-ls.go
78 lines (66 loc) · 1.65 KB
/
operation-ls.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
package component
import (
"context"
"encoding/json"
"errors"
"fmt"
ipfsPath "github.com/ipfs/interface-go-ipfs-core/path"
"github.com/dapr/components-contrib/bindings"
"github.com/dapr/components-contrib/metadata"
)
// Handler for the "ls" operation, which retrieves a document
func (b *IPFSBinding) lsOperation(ctx context.Context, req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
reqMetadata := &lsRequestMetadata{}
err := reqMetadata.FromMap(req.Metadata)
if err != nil {
return nil, err
}
if reqMetadata.Path == "" {
return nil, errors.New("metadata property 'path' is empty")
}
p := ipfsPath.New(reqMetadata.Path)
err = p.IsValid()
if err != nil {
return nil, fmt.Errorf("invalid value for metadata property 'path': %v", err)
}
ls, err := b.ipfsAPI.Unixfs().Ls(ctx, p)
if err != nil {
return nil, err
}
res := lsOperationResponse{}
for e := range ls {
if e.Err != nil {
return nil, e.Err
}
res = append(res, lsOperationResponseItem{
Name: e.Name,
Size: e.Size,
Type: e.Type.String(),
Cid: e.Cid.String(),
})
}
j, _ := json.Marshal(res)
return &bindings.InvokeResponse{
Data: j,
Metadata: nil,
}, nil
}
type lsOperationResponseItem struct {
Name string `json:"name,omitempty"`
Size uint64 `json:"size,omitempty"`
Cid string `json:"cid,omitempty"`
Type string `json:"type,omitempty"`
}
type lsOperationResponse []lsOperationResponseItem
type lsRequestMetadata struct {
Path string `mapstructure:"path"`
}
func (m *lsRequestMetadata) FromMap(mp map[string]string) (err error) {
if len(mp) > 0 {
err = metadata.DecodeMetadata(mp, m)
if err != nil {
return err
}
}
return nil
}