forked from ipfs/go-ipfs-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdag.go
77 lines (65 loc) · 1.44 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
package shell
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"strings"
files "github.com/whyrusleeping/go-multipart-files"
)
func (s *Shell) DagGet(ref string, out interface{}) error {
req := s.newRequest(context.Background(), "dag/get")
req.Args = []string{ref}
resp, err := req.Send(s.httpcli)
if err != nil {
return err
}
defer resp.Close()
if resp.Error != nil {
return resp.Error
}
return json.NewDecoder(resp.Output).Decode(out)
}
func (s *Shell) DagPut(data interface{}, ienc, kind string) (string, error) {
req := s.newRequest(context.Background(), "dag/put")
req.Opts = map[string]string{
"input-enc": ienc,
"format": kind,
}
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)
}
rc := ioutil.NopCloser(r)
fr := files.NewReaderFile("", "", rc, nil)
slf := files.NewSliceFile("", "", []files.File{fr})
fileReader := files.NewMultiFileReader(slf, true)
req.Body = fileReader
resp, err := req.Send(s.httpcli)
if err != nil {
return "", err
}
defer resp.Close()
if resp.Error != nil {
return "", resp.Error
}
var out struct {
Cid struct {
Target string `json:"/"`
}
}
err = json.NewDecoder(resp.Output).Decode(&out)
if err != nil {
return "", err
}
return out.Cid.Target, nil
}