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
9
caopts "github.com/ipfs/kubo/core/coreiface/options"
16
10
)
@@ -21,138 +15,6 @@ type objectOut struct {
21
15
Hash string
22
16
}
23
17
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
18
func (api * ObjectAPI ) AddLink (ctx context.Context , base path.Path , name string , child path.Path , opts ... caopts.ObjectAddLinkOption ) (path.ImmutablePath , error ) {
157
19
options , err := caopts .ObjectAddLinkOptions (opts ... )
158
20
if err != nil {
@@ -191,40 +53,6 @@ func (api *ObjectAPI) RmLink(ctx context.Context, base path.Path, link string) (
191
53
return path .FromCid (c ), nil
192
54
}
193
55
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
56
type change struct {
229
57
Type iface.ChangeType
230
58
Path string
0 commit comments