Skip to content

Commit

Permalink
core/commands!: remove deprecated object APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Mar 19, 2024
1 parent 21728eb commit cf686db
Show file tree
Hide file tree
Showing 20 changed files with 37 additions and 2,388 deletions.
24 changes: 6 additions & 18 deletions assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import (
"github.com/ipfs/kubo/core/coreapi"

"github.com/ipfs/boxo/files"
"github.com/ipfs/boxo/path"
cid "github.com/ipfs/go-cid"
options "github.com/ipfs/kubo/core/coreiface/options"
)

//go:embed init-doc
Expand Down Expand Up @@ -39,30 +37,20 @@ func addAssetList(nd *core.IpfsNode, l []string) (cid.Cid, error) {
return cid.Cid{}, err
}

dirb, err := api.Object().New(nd.Context(), options.Object.Type("unixfs-dir"))
if err != nil {
return cid.Cid{}, err
}

basePath := path.FromCid(dirb.Cid())
dirMap := map[string]files.Node{}

for _, p := range l {
d, err := Asset.ReadFile(p)
if err != nil {
return cid.Cid{}, fmt.Errorf("assets: could load Asset '%s': %s", p, err)
}

fp, err := api.Unixfs().Add(nd.Context(), files.NewBytesFile(d))
if err != nil {
return cid.Cid{}, err
}

fname := gopath.Base(p)
dirMap[gopath.Base(p)] = files.NewBytesFile(d)
}

basePath, err = api.Object().AddLink(nd.Context(), basePath, fname, fp)
if err != nil {
return cid.Cid{}, err
}
basePath, err := api.Unixfs().Add(nd.Context(), files.NewMapDirectory(dirMap))
if err != nil {
return cid.Cid{}, err
}

if err := api.Pin().Add(nd.Context(), basePath); err != nil {
Expand Down
215 changes: 0 additions & 215 deletions client/rpc/object.go
Original file line number Diff line number Diff line change
@@ -1,230 +1,15 @@
package rpc

import (
"bytes"
"context"
"fmt"
"io"

"github.com/ipfs/boxo/ipld/merkledag"
ft "github.com/ipfs/boxo/ipld/unixfs"
"github.com/ipfs/boxo/path"
"github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
iface "github.com/ipfs/kubo/core/coreiface"
caopts "github.com/ipfs/kubo/core/coreiface/options"
)

type ObjectAPI HttpApi

type objectOut struct {
Hash string
}

func (api *ObjectAPI) New(ctx context.Context, opts ...caopts.ObjectNewOption) (ipld.Node, error) {
options, err := caopts.ObjectNewOptions(opts...)
if err != nil {
return nil, err
}

var n ipld.Node
switch options.Type {
case "empty":
n = new(merkledag.ProtoNode)
case "unixfs-dir":
n = ft.EmptyDirNode()
default:
return nil, fmt.Errorf("unknown object type: %s", options.Type)
}

return n, nil
}

func (api *ObjectAPI) Put(ctx context.Context, r io.Reader, opts ...caopts.ObjectPutOption) (path.ImmutablePath, error) {
options, err := caopts.ObjectPutOptions(opts...)
if err != nil {
return path.ImmutablePath{}, err
}

var out objectOut
err = api.core().Request("object/put").
Option("inputenc", options.InputEnc).
Option("datafieldenc", options.DataType).
Option("pin", options.Pin).
FileBody(r).
Exec(ctx, &out)
if err != nil {
return path.ImmutablePath{}, err
}

c, err := cid.Parse(out.Hash)
if err != nil {
return path.ImmutablePath{}, err
}

return path.FromCid(c), nil
}

func (api *ObjectAPI) Get(ctx context.Context, p path.Path) (ipld.Node, error) {
r, err := api.core().Block().Get(ctx, p)
if err != nil {
return nil, err
}
b, err := io.ReadAll(r)
if err != nil {
return nil, err
}

return merkledag.DecodeProtobuf(b)
}

func (api *ObjectAPI) Data(ctx context.Context, p path.Path) (io.Reader, error) {
resp, err := api.core().Request("object/data", p.String()).Send(ctx)
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, resp.Error
}

// TODO: make Data return ReadCloser to avoid copying
defer resp.Close()
b := new(bytes.Buffer)
if _, err := io.Copy(b, resp.Output); err != nil {
return nil, err
}

return b, nil
}

func (api *ObjectAPI) Links(ctx context.Context, p path.Path) ([]*ipld.Link, error) {
var out struct {
Links []struct {
Name string
Hash string
Size uint64
}
}
if err := api.core().Request("object/links", p.String()).Exec(ctx, &out); err != nil {
return nil, err
}
res := make([]*ipld.Link, len(out.Links))
for i, l := range out.Links {
c, err := cid.Parse(l.Hash)
if err != nil {
return nil, err
}

res[i] = &ipld.Link{
Cid: c,
Name: l.Name,
Size: l.Size,
}
}

return res, nil
}

func (api *ObjectAPI) Stat(ctx context.Context, p path.Path) (*iface.ObjectStat, error) {
var out struct {
Hash string
NumLinks int
BlockSize int
LinksSize int
DataSize int
CumulativeSize int
}
if err := api.core().Request("object/stat", p.String()).Exec(ctx, &out); err != nil {
return nil, err
}

c, err := cid.Parse(out.Hash)
if err != nil {
return nil, err
}

return &iface.ObjectStat{
Cid: c,
NumLinks: out.NumLinks,
BlockSize: out.BlockSize,
LinksSize: out.LinksSize,
DataSize: out.DataSize,
CumulativeSize: out.CumulativeSize,
}, nil
}

func (api *ObjectAPI) AddLink(ctx context.Context, base path.Path, name string, child path.Path, opts ...caopts.ObjectAddLinkOption) (path.ImmutablePath, error) {
options, err := caopts.ObjectAddLinkOptions(opts...)
if err != nil {
return path.ImmutablePath{}, err
}

var out objectOut
err = api.core().Request("object/patch/add-link", base.String(), name, child.String()).
Option("create", options.Create).
Exec(ctx, &out)
if err != nil {
return path.ImmutablePath{}, err
}

c, err := cid.Parse(out.Hash)
if err != nil {
return path.ImmutablePath{}, err
}

return path.FromCid(c), nil
}

func (api *ObjectAPI) RmLink(ctx context.Context, base path.Path, link string) (path.ImmutablePath, error) {
var out objectOut
err := api.core().Request("object/patch/rm-link", base.String(), link).
Exec(ctx, &out)
if err != nil {
return path.ImmutablePath{}, err
}

c, err := cid.Parse(out.Hash)
if err != nil {
return path.ImmutablePath{}, err
}

return path.FromCid(c), nil
}

func (api *ObjectAPI) AppendData(ctx context.Context, p path.Path, r io.Reader) (path.ImmutablePath, error) {
var out objectOut
err := api.core().Request("object/patch/append-data", p.String()).
FileBody(r).
Exec(ctx, &out)
if err != nil {
return path.ImmutablePath{}, err
}

c, err := cid.Parse(out.Hash)
if err != nil {
return path.ImmutablePath{}, err
}

return path.FromCid(c), nil
}

func (api *ObjectAPI) SetData(ctx context.Context, p path.Path, r io.Reader) (path.ImmutablePath, error) {
var out objectOut
err := api.core().Request("object/patch/set-data", p.String()).
FileBody(r).
Exec(ctx, &out)
if err != nil {
return path.ImmutablePath{}, err
}

c, err := cid.Parse(out.Hash)
if err != nil {
return path.ImmutablePath{}, err
}

return path.FromCid(c), nil
}

type change struct {
Type iface.ChangeType
Path string
Expand Down
11 changes: 0 additions & 11 deletions core/commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,7 @@ func TestCommands(t *testing.T) {
"/name/pubsub/subs",
"/name/resolve",
"/object",
"/object/data",
"/object/diff",
"/object/get",
"/object/links",
"/object/new",
"/object/patch",
"/object/patch/add-link",
"/object/patch/append-data",
"/object/patch/rm-link",
"/object/patch/set-data",
"/object/put",
"/object/stat",
"/p2p",
"/p2p/close",
"/p2p/forward",
Expand Down
Loading

0 comments on commit cf686db

Please sign in to comment.