object.go
1 package rpc 2 3 import ( 4 "context" 5 6 "github.com/ipfs/boxo/path" 7 "github.com/ipfs/go-cid" 8 iface "github.com/ipfs/kubo/core/coreiface" 9 caopts "github.com/ipfs/kubo/core/coreiface/options" 10 ) 11 12 type ObjectAPI HttpApi 13 14 type objectOut struct { 15 Hash string 16 } 17 18 func (api *ObjectAPI) AddLink(ctx context.Context, base path.Path, name string, child path.Path, opts ...caopts.ObjectAddLinkOption) (path.ImmutablePath, error) { 19 options, err := caopts.ObjectAddLinkOptions(opts...) 20 if err != nil { 21 return path.ImmutablePath{}, err 22 } 23 24 var out objectOut 25 err = api.core().Request("object/patch/add-link", base.String(), name, child.String()). 26 Option("create", options.Create). 27 Exec(ctx, &out) 28 if err != nil { 29 return path.ImmutablePath{}, err 30 } 31 32 c, err := cid.Parse(out.Hash) 33 if err != nil { 34 return path.ImmutablePath{}, err 35 } 36 37 return path.FromCid(c), nil 38 } 39 40 func (api *ObjectAPI) RmLink(ctx context.Context, base path.Path, link string) (path.ImmutablePath, error) { 41 var out objectOut 42 err := api.core().Request("object/patch/rm-link", base.String(), link). 43 Exec(ctx, &out) 44 if err != nil { 45 return path.ImmutablePath{}, err 46 } 47 48 c, err := cid.Parse(out.Hash) 49 if err != nil { 50 return path.ImmutablePath{}, err 51 } 52 53 return path.FromCid(c), nil 54 } 55 56 type change struct { 57 Type iface.ChangeType 58 Path string 59 Before cid.Cid 60 After cid.Cid 61 } 62 63 func (api *ObjectAPI) Diff(ctx context.Context, a path.Path, b path.Path) ([]iface.ObjectChange, error) { 64 var out struct { 65 Changes []change 66 } 67 if err := api.core().Request("object/diff", a.String(), b.String()).Exec(ctx, &out); err != nil { 68 return nil, err 69 } 70 res := make([]iface.ObjectChange, len(out.Changes)) 71 for i, ch := range out.Changes { 72 res[i] = iface.ObjectChange{ 73 Type: ch.Type, 74 Path: ch.Path, 75 } 76 if ch.Before != cid.Undef { 77 res[i].Before = path.FromCid(ch.Before) 78 } 79 if ch.After != cid.Undef { 80 res[i].After = path.FromCid(ch.After) 81 } 82 } 83 return res, nil 84 } 85 86 func (api *ObjectAPI) core() *HttpApi { 87 return (*HttpApi)(api) 88 }