object.go 6.83 KB
Newer Older
1 2 3 4 5
package coreapi

import (
	"bytes"
	"context"
6 7 8 9 10
	"encoding/base64"
	"encoding/json"
	"encoding/xml"
	"errors"
	"fmt"
11 12 13 14
	"io"
	"io/ioutil"

	coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
15
	caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
16
	dag "github.com/ipfs/go-ipfs/merkledag"
17
	dagutils "github.com/ipfs/go-ipfs/merkledag/utils"
18
	ft "github.com/ipfs/go-ipfs/unixfs"
Łukasz Magiera's avatar
Łukasz Magiera committed
19

Steven Allen's avatar
Steven Allen committed
20 21
	cid "gx/ipfs/QmYVNvtQkeZ6AKSwDrjQTs432QtL6umrrK41EBq3cu7iSP/go-cid"
	ipld "gx/ipfs/QmZtNq8dArGfnpCZfx2pUNY7UcjGhVp5qqwQ4hH6mpTMRQ/go-ipld-format"
22 23
)

Łukasz Magiera's avatar
Łukasz Magiera committed
24 25
const inputLimit = 2 << 20

26
type ObjectAPI CoreAPI
27

Łukasz Magiera's avatar
Łukasz Magiera committed
28 29 30 31 32 33 34 35 36 37
type Link struct {
	Name, Hash string
	Size       uint64
}

type Node struct {
	Links []Link
	Data  string
}

38
func (api *ObjectAPI) New(ctx context.Context, opts ...caopts.ObjectNewOption) (ipld.Node, error) {
Łukasz Magiera's avatar
Łukasz Magiera committed
39 40 41 42
	options, err := caopts.ObjectNewOptions(opts...)
	if err != nil {
		return nil, err
	}
43

44
	var n ipld.Node
Łukasz Magiera's avatar
Łukasz Magiera committed
45 46 47 48 49 50 51
	switch options.Type {
	case "empty":
		n = new(dag.ProtoNode)
	case "unixfs-dir":
		n = ft.EmptyDirNode()
	}

Steven Allen's avatar
Steven Allen committed
52
	err = api.node.DAG.Add(ctx, n)
53 54 55
	if err != nil {
		return nil, err
	}
Łukasz Magiera's avatar
Łukasz Magiera committed
56
	return n, nil
57 58
}

59 60 61 62 63 64
func (api *ObjectAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.ObjectPutOption) (coreiface.Path, error) {
	options, err := caopts.ObjectPutOptions(opts...)
	if err != nil {
		return nil, err
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
	data, err := ioutil.ReadAll(io.LimitReader(src, inputLimit+10))
	if err != nil {
		return nil, err
	}

	var dagnode *dag.ProtoNode
	switch options.InputEnc {
	case "json":
		node := new(Node)
		err = json.Unmarshal(data, node)
		if err != nil {
			return nil, err
		}

		// check that we have data in the Node to add
		// otherwise we will add the empty object without raising an error
		if nodeEmpty(node) {
			return nil, errors.New("no data or links in this node")
		}

		dagnode, err = deserializeNode(node, options.DataType)
		if err != nil {
			return nil, err
		}

	case "protobuf":
		dagnode, err = dag.DecodeProtobuf(data)

	case "xml":
		node := new(Node)
		err = xml.Unmarshal(data, node)
		if err != nil {
			return nil, err
		}

		// check that we have data in the Node to add
		// otherwise we will add the empty object without raising an error
		if nodeEmpty(node) {
			return nil, errors.New("no data or links in this node")
		}

		dagnode, err = deserializeNode(node, options.DataType)
		if err != nil {
			return nil, err
		}

	default:
		return nil, errors.New("unknown object encoding")
	}

	if err != nil {
		return nil, err
	}

Steven Allen's avatar
Steven Allen committed
119
	err = api.node.DAG.Add(ctx, dagnode)
Łukasz Magiera's avatar
Łukasz Magiera committed
120 121 122 123
	if err != nil {
		return nil, err
	}

124
	return api.ParseCid(dagnode.Cid()), nil
125 126
}

127
func (api *ObjectAPI) Get(ctx context.Context, path coreiface.Path) (ipld.Node, error) {
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
	return api.core().ResolveNode(ctx, path)
}

func (api *ObjectAPI) Data(ctx context.Context, path coreiface.Path) (io.Reader, error) {
	nd, err := api.core().ResolveNode(ctx, path)
	if err != nil {
		return nil, err
	}

	pbnd, ok := nd.(*dag.ProtoNode)
	if !ok {
		return nil, dag.ErrNotProtobuf
	}

	return bytes.NewReader(pbnd.Data()), nil
}

145
func (api *ObjectAPI) Links(ctx context.Context, path coreiface.Path) ([]*ipld.Link, error) {
146 147 148 149 150 151
	nd, err := api.core().ResolveNode(ctx, path)
	if err != nil {
		return nil, err
	}

	links := nd.Links()
152
	out := make([]*ipld.Link, len(links))
153
	for n, l := range links {
154
		out[n] = (*ipld.Link)(l)
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
	}

	return out, nil
}

func (api *ObjectAPI) Stat(ctx context.Context, path coreiface.Path) (*coreiface.ObjectStat, error) {
	nd, err := api.core().ResolveNode(ctx, path)
	if err != nil {
		return nil, err
	}

	stat, err := nd.Stat()
	if err != nil {
		return nil, err
	}

	out := &coreiface.ObjectStat{
		Cid:            nd.Cid(),
		NumLinks:       stat.NumLinks,
		BlockSize:      stat.BlockSize,
		LinksSize:      stat.LinksSize,
		DataSize:       stat.DataSize,
		CumulativeSize: stat.CumulativeSize,
	}

	return out, nil
}

Łukasz Magiera's avatar
Łukasz Magiera committed
183
func (api *ObjectAPI) AddLink(ctx context.Context, base coreiface.Path, name string, child coreiface.Path, opts ...caopts.ObjectAddLinkOption) (coreiface.Path, error) {
Łukasz Magiera's avatar
Łukasz Magiera committed
184 185 186 187 188 189
	options, err := caopts.ObjectAddLinkOptions(opts...)
	if err != nil {
		return nil, err
	}

	baseNd, err := api.core().ResolveNode(ctx, base)
190 191 192 193 194 195 196 197 198
	if err != nil {
		return nil, err
	}

	childNd, err := api.core().ResolveNode(ctx, child)
	if err != nil {
		return nil, err
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
199
	basePb, ok := baseNd.(*dag.ProtoNode)
200 201 202 203 204
	if !ok {
		return nil, dag.ErrNotProtobuf
	}

	var createfunc func() *dag.ProtoNode
Łukasz Magiera's avatar
Łukasz Magiera committed
205
	if options.Create {
206 207 208
		createfunc = ft.EmptyDirNode
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
209
	e := dagutils.NewDagEditor(basePb, api.node.DAG)
210 211 212 213 214 215

	err = e.InsertNodeAtPath(ctx, name, childNd, createfunc)
	if err != nil {
		return nil, err
	}

Steven Allen's avatar
Steven Allen committed
216
	nnode, err := e.Finalize(ctx, api.node.DAG)
217 218 219 220
	if err != nil {
		return nil, err
	}

221
	return api.ParseCid(nnode.Cid()), nil
222 223
}

Łukasz Magiera's avatar
Łukasz Magiera committed
224
func (api *ObjectAPI) RmLink(ctx context.Context, base coreiface.Path, link string) (coreiface.Path, error) {
Łukasz Magiera's avatar
Łukasz Magiera committed
225
	baseNd, err := api.core().ResolveNode(ctx, base)
226 227 228 229
	if err != nil {
		return nil, err
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
230
	basePb, ok := baseNd.(*dag.ProtoNode)
231 232 233 234
	if !ok {
		return nil, dag.ErrNotProtobuf
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
235
	e := dagutils.NewDagEditor(basePb, api.node.DAG)
236 237 238 239 240 241

	err = e.RmLink(ctx, link)
	if err != nil {
		return nil, err
	}

Steven Allen's avatar
Steven Allen committed
242
	nnode, err := e.Finalize(ctx, api.node.DAG)
243 244 245 246
	if err != nil {
		return nil, err
	}

247
	return api.ParseCid(nnode.Cid()), nil
248 249
}

Łukasz Magiera's avatar
Łukasz Magiera committed
250
func (api *ObjectAPI) AppendData(ctx context.Context, path coreiface.Path, r io.Reader) (coreiface.Path, error) {
251 252 253
	return api.patchData(ctx, path, r, true)
}

Łukasz Magiera's avatar
Łukasz Magiera committed
254
func (api *ObjectAPI) SetData(ctx context.Context, path coreiface.Path, r io.Reader) (coreiface.Path, error) {
255 256 257
	return api.patchData(ctx, path, r, false)
}

Łukasz Magiera's avatar
Łukasz Magiera committed
258
func (api *ObjectAPI) patchData(ctx context.Context, path coreiface.Path, r io.Reader, appendData bool) (coreiface.Path, error) {
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
	nd, err := api.core().ResolveNode(ctx, path)
	if err != nil {
		return nil, err
	}

	pbnd, ok := nd.(*dag.ProtoNode)
	if !ok {
		return nil, dag.ErrNotProtobuf
	}

	data, err := ioutil.ReadAll(r)
	if err != nil {
		return nil, err
	}

	if appendData {
		data = append(pbnd.Data(), data...)
	}
	pbnd.SetData(data)

Steven Allen's avatar
Steven Allen committed
279
	err = api.node.DAG.Add(ctx, pbnd)
280 281 282 283
	if err != nil {
		return nil, err
	}

284
	return api.ParseCid(pbnd.Cid()), nil
285 286 287
}

func (api *ObjectAPI) core() coreiface.CoreAPI {
288
	return (*CoreAPI)(api)
289
}
Łukasz Magiera's avatar
Łukasz Magiera committed
290 291 292 293 294 295 296

func deserializeNode(nd *Node, dataFieldEncoding string) (*dag.ProtoNode, error) {
	dagnode := new(dag.ProtoNode)
	switch dataFieldEncoding {
	case "text":
		dagnode.SetData([]byte(nd.Data))
	case "base64":
Steven Allen's avatar
Steven Allen committed
297 298 299 300
		data, err := base64.StdEncoding.DecodeString(nd.Data)
		if err != nil {
			return nil, err
		}
Łukasz Magiera's avatar
Łukasz Magiera committed
301 302
		dagnode.SetData(data)
	default:
Łukasz Magiera's avatar
Łukasz Magiera committed
303
		return nil, fmt.Errorf("unkown data field encoding")
Łukasz Magiera's avatar
Łukasz Magiera committed
304 305
	}

Steven Allen's avatar
Steven Allen committed
306
	links := make([]*ipld.Link, len(nd.Links))
Łukasz Magiera's avatar
Łukasz Magiera committed
307 308 309 310 311
	for i, link := range nd.Links {
		c, err := cid.Decode(link.Hash)
		if err != nil {
			return nil, err
		}
Steven Allen's avatar
Steven Allen committed
312
		links[i] = &ipld.Link{
Łukasz Magiera's avatar
Łukasz Magiera committed
313 314 315 316 317
			Name: link.Name,
			Size: link.Size,
			Cid:  c,
		}
	}
Steven Allen's avatar
Steven Allen committed
318
	dagnode.SetLinks(links)
Łukasz Magiera's avatar
Łukasz Magiera committed
319 320 321 322 323 324 325

	return dagnode, nil
}

func nodeEmpty(node *Node) bool {
	return node.Data == "" && len(node.Links) == 0
}