|
1 | 1 | package rpc
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "bytes" |
5 | 4 | "context"
|
6 |
| - "fmt" |
7 |
| - "io" |
8 | 5 |
|
9 |
| - "github.com/ipfs/boxo/ipld/merkledag" |
10 |
| - ft "github.com/ipfs/boxo/ipld/unixfs" |
11 | 6 | "github.com/ipfs/boxo/path"
|
12 | 7 | "github.com/ipfs/go-cid"
|
13 |
| - ipld "github.com/ipfs/go-ipld-format" |
14 | 8 | iface "github.com/ipfs/kubo/core/coreiface"
|
15 |
| - caopts "github.com/ipfs/kubo/core/coreiface/options" |
16 | 9 | )
|
17 | 10 |
|
18 | 11 | type ObjectAPI HttpApi
|
19 | 12 |
|
20 |
| -type objectOut struct { |
21 |
| - Hash string |
22 |
| -} |
23 |
| - |
24 |
| -func (api *ObjectAPI) New(ctx context.Context, opts ...caopts.ObjectNewOption) (ipld.Node, error) { |
25 |
| - options, err := caopts.ObjectNewOptions(opts...) |
26 |
| - if err != nil { |
27 |
| - return nil, err |
28 |
| - } |
29 |
| - |
30 |
| - var n ipld.Node |
31 |
| - switch options.Type { |
32 |
| - case "empty": |
33 |
| - n = new(merkledag.ProtoNode) |
34 |
| - case "unixfs-dir": |
35 |
| - n = ft.EmptyDirNode() |
36 |
| - default: |
37 |
| - return nil, fmt.Errorf("unknown object type: %s", options.Type) |
38 |
| - } |
39 |
| - |
40 |
| - return n, nil |
41 |
| -} |
42 |
| - |
43 |
| -func (api *ObjectAPI) Put(ctx context.Context, r io.Reader, opts ...caopts.ObjectPutOption) (path.ImmutablePath, error) { |
44 |
| - options, err := caopts.ObjectPutOptions(opts...) |
45 |
| - if err != nil { |
46 |
| - return path.ImmutablePath{}, err |
47 |
| - } |
48 |
| - |
49 |
| - var out objectOut |
50 |
| - err = api.core().Request("object/put"). |
51 |
| - Option("inputenc", options.InputEnc). |
52 |
| - Option("datafieldenc", options.DataType). |
53 |
| - Option("pin", options.Pin). |
54 |
| - FileBody(r). |
55 |
| - Exec(ctx, &out) |
56 |
| - if err != nil { |
57 |
| - return path.ImmutablePath{}, err |
58 |
| - } |
59 |
| - |
60 |
| - c, err := cid.Parse(out.Hash) |
61 |
| - if err != nil { |
62 |
| - return path.ImmutablePath{}, err |
63 |
| - } |
64 |
| - |
65 |
| - return path.FromCid(c), nil |
66 |
| -} |
67 |
| - |
68 |
| -func (api *ObjectAPI) Get(ctx context.Context, p path.Path) (ipld.Node, error) { |
69 |
| - r, err := api.core().Block().Get(ctx, p) |
70 |
| - if err != nil { |
71 |
| - return nil, err |
72 |
| - } |
73 |
| - b, err := io.ReadAll(r) |
74 |
| - if err != nil { |
75 |
| - return nil, err |
76 |
| - } |
77 |
| - |
78 |
| - return merkledag.DecodeProtobuf(b) |
79 |
| -} |
80 |
| - |
81 |
| -func (api *ObjectAPI) Data(ctx context.Context, p path.Path) (io.Reader, error) { |
82 |
| - resp, err := api.core().Request("object/data", p.String()).Send(ctx) |
83 |
| - if err != nil { |
84 |
| - return nil, err |
85 |
| - } |
86 |
| - if resp.Error != nil { |
87 |
| - return nil, resp.Error |
88 |
| - } |
89 |
| - |
90 |
| - // TODO: make Data return ReadCloser to avoid copying |
91 |
| - defer resp.Close() |
92 |
| - b := new(bytes.Buffer) |
93 |
| - if _, err := io.Copy(b, resp.Output); err != nil { |
94 |
| - return nil, err |
95 |
| - } |
96 |
| - |
97 |
| - return b, nil |
98 |
| -} |
99 |
| - |
100 |
| -func (api *ObjectAPI) Links(ctx context.Context, p path.Path) ([]*ipld.Link, error) { |
101 |
| - var out struct { |
102 |
| - Links []struct { |
103 |
| - Name string |
104 |
| - Hash string |
105 |
| - Size uint64 |
106 |
| - } |
107 |
| - } |
108 |
| - if err := api.core().Request("object/links", p.String()).Exec(ctx, &out); err != nil { |
109 |
| - return nil, err |
110 |
| - } |
111 |
| - res := make([]*ipld.Link, len(out.Links)) |
112 |
| - for i, l := range out.Links { |
113 |
| - c, err := cid.Parse(l.Hash) |
114 |
| - if err != nil { |
115 |
| - return nil, err |
116 |
| - } |
117 |
| - |
118 |
| - res[i] = &ipld.Link{ |
119 |
| - Cid: c, |
120 |
| - Name: l.Name, |
121 |
| - Size: l.Size, |
122 |
| - } |
123 |
| - } |
124 |
| - |
125 |
| - return res, nil |
126 |
| -} |
127 |
| - |
128 |
| -func (api *ObjectAPI) Stat(ctx context.Context, p path.Path) (*iface.ObjectStat, error) { |
129 |
| - var out struct { |
130 |
| - Hash string |
131 |
| - NumLinks int |
132 |
| - BlockSize int |
133 |
| - LinksSize int |
134 |
| - DataSize int |
135 |
| - CumulativeSize int |
136 |
| - } |
137 |
| - if err := api.core().Request("object/stat", p.String()).Exec(ctx, &out); err != nil { |
138 |
| - return nil, err |
139 |
| - } |
140 |
| - |
141 |
| - c, err := cid.Parse(out.Hash) |
142 |
| - if err != nil { |
143 |
| - return nil, err |
144 |
| - } |
145 |
| - |
146 |
| - return &iface.ObjectStat{ |
147 |
| - Cid: c, |
148 |
| - NumLinks: out.NumLinks, |
149 |
| - BlockSize: out.BlockSize, |
150 |
| - LinksSize: out.LinksSize, |
151 |
| - DataSize: out.DataSize, |
152 |
| - CumulativeSize: out.CumulativeSize, |
153 |
| - }, nil |
154 |
| -} |
155 |
| - |
156 |
| -func (api *ObjectAPI) AddLink(ctx context.Context, base path.Path, name string, child path.Path, opts ...caopts.ObjectAddLinkOption) (path.ImmutablePath, error) { |
157 |
| - options, err := caopts.ObjectAddLinkOptions(opts...) |
158 |
| - if err != nil { |
159 |
| - return path.ImmutablePath{}, err |
160 |
| - } |
161 |
| - |
162 |
| - var out objectOut |
163 |
| - err = api.core().Request("object/patch/add-link", base.String(), name, child.String()). |
164 |
| - Option("create", options.Create). |
165 |
| - Exec(ctx, &out) |
166 |
| - if err != nil { |
167 |
| - return path.ImmutablePath{}, err |
168 |
| - } |
169 |
| - |
170 |
| - c, err := cid.Parse(out.Hash) |
171 |
| - if err != nil { |
172 |
| - return path.ImmutablePath{}, err |
173 |
| - } |
174 |
| - |
175 |
| - return path.FromCid(c), nil |
176 |
| -} |
177 |
| - |
178 |
| -func (api *ObjectAPI) RmLink(ctx context.Context, base path.Path, link string) (path.ImmutablePath, error) { |
179 |
| - var out objectOut |
180 |
| - err := api.core().Request("object/patch/rm-link", base.String(), link). |
181 |
| - Exec(ctx, &out) |
182 |
| - if err != nil { |
183 |
| - return path.ImmutablePath{}, err |
184 |
| - } |
185 |
| - |
186 |
| - c, err := cid.Parse(out.Hash) |
187 |
| - if err != nil { |
188 |
| - return path.ImmutablePath{}, err |
189 |
| - } |
190 |
| - |
191 |
| - return path.FromCid(c), nil |
192 |
| -} |
193 |
| - |
194 |
| -func (api *ObjectAPI) AppendData(ctx context.Context, p path.Path, r io.Reader) (path.ImmutablePath, error) { |
195 |
| - var out objectOut |
196 |
| - err := api.core().Request("object/patch/append-data", p.String()). |
197 |
| - FileBody(r). |
198 |
| - Exec(ctx, &out) |
199 |
| - if err != nil { |
200 |
| - return path.ImmutablePath{}, err |
201 |
| - } |
202 |
| - |
203 |
| - c, err := cid.Parse(out.Hash) |
204 |
| - if err != nil { |
205 |
| - return path.ImmutablePath{}, err |
206 |
| - } |
207 |
| - |
208 |
| - return path.FromCid(c), nil |
209 |
| -} |
210 |
| - |
211 |
| -func (api *ObjectAPI) SetData(ctx context.Context, p path.Path, r io.Reader) (path.ImmutablePath, error) { |
212 |
| - var out objectOut |
213 |
| - err := api.core().Request("object/patch/set-data", p.String()). |
214 |
| - FileBody(r). |
215 |
| - Exec(ctx, &out) |
216 |
| - if err != nil { |
217 |
| - return path.ImmutablePath{}, err |
218 |
| - } |
219 |
| - |
220 |
| - c, err := cid.Parse(out.Hash) |
221 |
| - if err != nil { |
222 |
| - return path.ImmutablePath{}, err |
223 |
| - } |
224 |
| - |
225 |
| - return path.FromCid(c), nil |
226 |
| -} |
227 |
| - |
228 | 13 | type change struct {
|
229 | 14 | Type iface.ChangeType
|
230 | 15 | Path string
|
|
0 commit comments