Partial Unixfs.Add
This commit was moved from ipfs/go-ipfs-http-client@44696b84f59f6f707858787c9a0750f224596635
Łukasz Magiera committed
Jan 8, 2019 at 00:30 UTC
ab89e0abf99a0bc42ee2d8fb55a9b3de79573635
2 files changed
+97
-2
client/httpapi/api.go
+2
-2
@@ -106,7 +106,7 @@ func (api *HttpApi) request(command string, args ...string) *RequestBuilder {
106
}
107
108
func (api *HttpApi) Unixfs() iface.UnixfsAPI {
109
- return nil
109
+ return (*UnixfsAPI)(api)
110
}
111
112
func (api *HttpApi) Block() iface.BlockAPI {
@@ -118,7 +118,7 @@ func (api *HttpApi) Dag() iface.DagAPI {
118
}
119
120
func (api *HttpApi) Name() iface.NameAPI {
121
- return nil
121
+ return (*NameAPI)(api)
122
}
123
124
func (api *HttpApi) Key() iface.KeyAPI {
client/httpapi/unixfs.go
new
+95
@@ -0,0 +1,95 @@
1
+package httpapi
2
+
3
+import (
4
+ "context"
5
+ "fmt"
6
+ "github.com/ipfs/go-cid"
7
+
8
+ "github.com/ipfs/go-ipfs/core/coreapi/interface"
9
+ caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
10
+
11
+ "github.com/ipfs/go-ipfs-files"
12
+ "github.com/ipfs/go-ipld-format"
13
+ mh "github.com/multiformats/go-multihash"
14
+)
15
+
16
+type addEvent struct {
17
+ Name string
18
+ Hash string `json:",omitempty"`
19
+ Bytes int64 `json:",omitempty"`
20
+ Size string `json:",omitempty"`
21
+}
22
+
23
+type UnixfsAPI HttpApi
24
+
25
+func (api *UnixfsAPI) Add(ctx context.Context, f files.Node, opts ...caopts.UnixfsAddOption) (iface.ResolvedPath, error) {
26
+ options, _, err := caopts.UnixfsAddOptions(opts...)
27
+ if err != nil {
28
+ return nil, err
29
+ }
30
+
31
+ mht, ok := mh.Codes[options.MhType]
32
+ if !ok {
33
+ return nil, fmt.Errorf("unknowm mhType %d", options.MhType)
34
+ }
35
+
36
+ req := api.core().request("add").
37
+ Option("hash", mht).
38
+ Option("chunker", options.Chunker).
39
+ Option("cid-version", options.CidVersion).
40
+ //Option("", options.Events).
41
+ Option("fscache", options.FsCache).
42
+ Option("hidden", options.Hidden).
43
+ Option("inline", options.Inline).
44
+ Option("inline-limit", options.InlineLimit).
45
+ Option("nocopy", options.NoCopy).
46
+ Option("only-hash", options.OnlyHash).
47
+ Option("pin", options.Pin).
48
+ //Option("", options.Progress).
49
+ Option("silent", options.Silent).
50
+ Option("stdin-name", options.StdinName).
51
+ Option("wrap-with-directory", options.Wrap).
52
+ Option("quieter", true) // TODO: rm after event impl
53
+
54
+ if options.RawLeavesSet {
55
+ req.Option("raw-leaves", options.RawLeaves)
56
+ }
57
+
58
+ switch options.Layout {
59
+ case caopts.BalancedLayout:
60
+ // noop, default
61
+ case caopts.TrickleLayout:
62
+ req.Option("trickle", true)
63
+ }
64
+
65
+ switch c := f.(type) {
66
+ case files.Directory:
67
+ req.Body(files.NewMultiFileReader(c, false))
68
+ case files.File:
69
+ req.Body(c)
70
+ }
71
+
72
+ var out addEvent
73
+ if err := req.Exec(ctx, &out); err != nil { //TODO: ndjson events
74
+ return nil, err
75
+ }
76
+
77
+ c, err := cid.Parse(out.Hash)
78
+ if err != nil {
79
+ return nil, err
80
+ }
81
+
82
+ return iface.IpfsPath(c), nil
83
+}
84
+
85
+func (api *UnixfsAPI) Get(context.Context, iface.Path) (files.Node, error) {
86
+ panic("implement me")
87
+}
88
+
89
+func (api *UnixfsAPI) Ls(context.Context, iface.Path) ([]*format.Link, error) {
90
+ panic("implement me")
91
+}
92
+
93
+func (api *UnixfsAPI) core() *HttpApi {
94
+ return (*HttpApi)(api)
95
+}