forked from ipfs/go-ipfs-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dag.go
153 lines (125 loc) · 3.17 KB
/
dag.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package shell
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"strings"
files "github.com/ipfs/boxo/files"
"github.com/ipfs/go-ipfs-api/options"
)
type DagPutOutput struct {
Cid struct {
Target string `json:"/"`
}
}
type DagImportRoot struct {
Root struct {
Cid struct {
Value string `json:"/"`
}
}
Stats *DagImportStats `json:"Stats,omitempty"`
}
type DagImportStats struct {
BlockBytesCount uint64
BlockCount uint64
}
type DagImportOutput struct {
Roots []DagImportRoot
Stats *DagImportStats
}
func (s *Shell) DagGet(ref string, out interface{}) error {
return s.Request("dag/get", ref).Exec(context.Background(), out)
}
func (s *Shell) DagPut(data interface{}, inputCodec, storeCodec string) (string, error) {
return s.DagPutWithOpts(data, options.Dag.InputCodec(inputCodec), options.Dag.StoreCodec(storeCodec))
}
func (s *Shell) DagPutWithOpts(data interface{}, opts ...options.DagPutOption) (string, error) {
cfg, err := options.DagPutOptions(opts...)
if err != nil {
return "", err
}
fileReader, err := dagToFilesReader(data)
if err != nil {
return "", err
}
var out DagPutOutput
return out.Cid.Target, s.
Request("dag/put").
Option("input-codec", cfg.InputCodec).
Option("store-codec", cfg.StoreCodec).
Option("pin", cfg.Pin).
Option("hash", cfg.Hash).
Body(fileReader).
Exec(context.Background(), &out)
}
// DagImport imports the contents of .car files (with default parameters)
func (s *Shell) DagImport(data interface{}, silent, stats bool) (*DagImportOutput, error) {
return s.DagImportWithOpts(data, options.Dag.Silent(silent), options.Dag.Stats(stats))
}
// DagImportWithOpts imports the contents of .car files
func (s *Shell) DagImportWithOpts(data interface{}, opts ...options.DagImportOption) (*DagImportOutput, error) {
cfg, err := options.DagImportOptions(opts...)
if err != nil {
return nil, err
}
fileReader, err := dagToFilesReader(data)
if err != nil {
return nil, err
}
res, err := s.Request("dag/import").
Option("pin-roots", cfg.PinRoots).
Option("silent", cfg.Silent).
Option("stats", cfg.Stats).
Option("allow-big-block", cfg.AllowBigBlock).
Body(fileReader).
Send(context.Background())
if err != nil {
return nil, err
}
defer res.Close()
if res.Error != nil {
return nil, res.Error
}
if cfg.Silent {
return nil, nil
}
out := DagImportOutput{
Roots: []DagImportRoot{},
}
dec := json.NewDecoder(res.Output)
for {
var root DagImportRoot
err := dec.Decode(&root)
if err == io.EOF {
break
}
if root.Stats != nil {
out.Stats = root.Stats
break
}
out.Roots = append(out.Roots, root)
}
return &out, err
}
func dagToFilesReader(data interface{}) (*files.MultiFileReader, error) {
var r io.Reader
switch data := data.(type) {
case *files.MultiFileReader:
return data, nil
case string:
r = strings.NewReader(data)
case []byte:
r = bytes.NewReader(data)
case io.Reader:
r = data
default:
return nil, fmt.Errorf("values of type %T cannot be handled as DAG input", data)
}
fr := files.NewReaderFile(r)
slf := files.NewSliceDirectory([]files.DirEntry{files.FileEntry("", fr)})
fileReader := files.NewMultiFileReader(slf, true)
return fileReader, nil
}