Dag.Put
This commit was moved from ipfs/go-ipfs-http-client@3217104469020f0c66d8457842f8221b64289d74
Łukasz Magiera committed
Jan 9, 2019 at 09:04 UTC
00597e6dff81846a2dbf742dd1ab9bf546b856e2
2 files changed
+70
-1
client/httpapi/api.go
+1
-1
@@ -138,7 +138,7 @@ func (api *HttpApi) Block() iface.BlockAPI {
138
}
139
140
func (api *HttpApi) Dag() iface.DagAPI {
141
- return nil
141
+ return (*DagAPI)(api)
142
}
143
144
func (api *HttpApi) Name() iface.NameAPI {
client/httpapi/dag.go
new
+69
@@ -0,0 +1,69 @@
1
+package httpapi
2
+
3
+import (
4
+ "context"
5
+ "fmt"
6
+ "github.com/ipfs/go-cid"
7
+ "io"
8
+
9
+ "github.com/ipfs/go-ipfs/core/coreapi/interface"
10
+ "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
11
+
12
+ caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
13
+ "github.com/ipfs/go-ipld-format"
14
+ mh "github.com/multiformats/go-multihash"
15
+)
16
+
17
+type DagAPI HttpApi
18
+
19
+func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...options.DagPutOption) (iface.ResolvedPath, error) {
20
+ options, err := caopts.DagPutOptions(opts...)
21
+ if err != nil {
22
+ return nil, err
23
+ }
24
+
25
+ mht, ok := mh.Codes[options.MhType]
26
+ if !ok {
27
+ return nil, fmt.Errorf("unknowm mhType %d", options.MhType)
28
+ }
29
+
30
+ codec, ok := cid.CodecToStr[options.Codec]
31
+ if !ok {
32
+ return nil, fmt.Errorf("unknowm codec %d", options.MhType)
33
+ }
34
+
35
+ if options.MhLength != -1 {
36
+ return nil, fmt.Errorf("setting hash len is not supported yet")
37
+ }
38
+
39
+ var out struct{
40
+ Cid cid.Cid
41
+ }
42
+ err = api.core().request("dht/put").
43
+ Option("hash", mht).
44
+ Option("format", codec).
45
+ Option("input-enc", options.InputEnc).
46
+ FileBody(src).
47
+ Exec(ctx, &out)
48
+ if err != nil {
49
+ return nil, err
50
+ }
51
+
52
+ return iface.IpldPath(out.Cid), nil
53
+}
54
+
55
+func (api *DagAPI) Get(ctx context.Context, path iface.Path) (format.Node, error) {
56
+ panic("implement me")
57
+}
58
+
59
+func (api *DagAPI) Tree(ctx context.Context, path iface.Path, opts ...options.DagTreeOption) ([]iface.Path, error) {
60
+ panic("implement me")
61
+}
62
+
63
+func (api *DagAPI) Batch(ctx context.Context) iface.DagBatch {
64
+ panic("implement me")
65
+}
66
+
67
+func (api *DagAPI) core() *HttpApi {
68
+ return (*HttpApi)(api)
69
+}