forked from ipfs/go-ipfs-api
-
Notifications
You must be signed in to change notification settings - Fork 4
/
dag.go
58 lines (49 loc) · 1.31 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
package shell
import (
"bytes"
"context"
"fmt"
"io"
"strings"
"github.com/TRON-US/go-btfs-api/options"
files "github.com/TRON-US/go-btfs-files"
)
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{}, ienc, kind string) (string, error) {
return s.DagPutWithOpts(data, options.Dag.InputEnc(ienc), options.Dag.Kind(kind))
}
func (s *Shell) DagPutWithOpts(data interface{}, opts ...options.DagPutOption) (string, error) {
cfg, err := options.DagPutOptions(opts...)
if err != nil {
return "", err
}
var r io.Reader
switch data := data.(type) {
case string:
r = strings.NewReader(data)
case []byte:
r = bytes.NewReader(data)
case io.Reader:
r = data
default:
return "", fmt.Errorf("cannot current handle putting values of type %T", data)
}
fr := files.NewReaderFile(r)
slf := files.NewSliceDirectory([]files.DirEntry{files.FileEntry("", fr)})
fileReader := files.NewMultiFileReader(slf, true)
var out struct {
Cid struct {
Target string `json:"/"`
}
}
return out.Cid.Target, s.
Request("dag/put").
Option("input-enc", cfg.InputEnc).
Option("format", cfg.Kind).
Option("pin", cfg.Pin).
Option("hash", cfg.Hash).
Body(fileReader).
Exec(context.Background(), &out)
}