| 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 | Option("allow-non-unixfs", options.SkipUnixFSValidation). |
| 28 | Exec(ctx, &out) |
| 29 | if err != nil { |
| 30 | return path.ImmutablePath{}, err |
| 31 | } |
| 32 | |
| 33 | c, err := cid.Parse(out.Hash) |
| 34 | if err != nil { |
| 35 | return path.ImmutablePath{}, err |
| 36 | } |
| 37 | |
| 38 | return path.FromCid(c), nil |
| 39 | } |
| 40 | |
| 41 | func (api *ObjectAPI) RmLink(ctx context.Context, base path.Path, link string, opts ...caopts.ObjectRmLinkOption) (path.ImmutablePath, error) { |
| 42 | options, err := caopts.ObjectRmLinkOptions(opts...) |
| 43 | if err != nil { |
| 44 | return path.ImmutablePath{}, err |
| 45 | } |
| 46 | |
| 47 | var out objectOut |
| 48 | err = api.core().Request("object/patch/rm-link", base.String(), link). |
| 49 | Option("allow-non-unixfs", options.SkipUnixFSValidation). |
| 50 | Exec(ctx, &out) |
| 51 | if err != nil { |
| 52 | return path.ImmutablePath{}, err |
| 53 | } |
| 54 | |
| 55 | c, err := cid.Parse(out.Hash) |
| 56 | if err != nil { |
| 57 | return path.ImmutablePath{}, err |
| 58 | } |
| 59 | |
| 60 | return path.FromCid(c), nil |
| 61 | } |
| 62 | |
| 63 | type change struct { |
| 64 | Type iface.ChangeType |
| 65 | Path string |
| 66 | Before cid.Cid |
| 67 | After cid.Cid |
| 68 | } |
| 69 | |
| 70 | func (api *ObjectAPI) Diff(ctx context.Context, a path.Path, b path.Path) ([]iface.ObjectChange, error) { |
| 71 | var out struct { |
| 72 | Changes []change |
| 73 | } |
| 74 | if err := api.core().Request("object/diff", a.String(), b.String()).Exec(ctx, &out); err != nil { |
| 75 | return nil, err |
| 76 | } |
| 77 | res := make([]iface.ObjectChange, len(out.Changes)) |
| 78 | for i, ch := range out.Changes { |
| 79 | res[i] = iface.ObjectChange{ |
| 80 | Type: ch.Type, |
| 81 | Path: ch.Path, |
| 82 | } |
| 83 | if ch.Before != cid.Undef { |
| 84 | res[i].Before = path.FromCid(ch.Before) |
| 85 | } |
| 86 | if ch.After != cid.Undef { |
| 87 | res[i].After = path.FromCid(ch.After) |
| 88 | } |
| 89 | } |
| 90 | return res, nil |
| 91 | } |
| 92 | |
| 93 | func (api *ObjectAPI) core() *HttpApi { |
| 94 | return (*HttpApi)(api) |
| 95 | } |