object.go 6.8 KB
Newer Older
1 2 3 4 5 6 7 8
package coreapi

import (
	"bytes"
	"context"
	"io"
	"io/ioutil"

Łukasz Magiera's avatar
Łukasz Magiera committed
9 10
	caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
	dagutils "github.com/ipfs/go-ipfs/merkledag/utils"
11 12 13 14

	coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
	dag "github.com/ipfs/go-ipfs/merkledag"
	ft "github.com/ipfs/go-ipfs/unixfs"
Łukasz Magiera's avatar
Łukasz Magiera committed
15

Łukasz Magiera's avatar
Łukasz Magiera committed
16 17 18 19 20
	"encoding/base64"
	"encoding/json"
	"encoding/xml"
	"errors"
	"fmt"
Łukasz Magiera's avatar
Łukasz Magiera committed
21
	node "gx/ipfs/QmNwUEK7QbwSqyKBu3mMtToo8SUc6wQJ7gdZq4gGGJqfnf/go-ipld-format"
22
	cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid"
23 24
)

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

Łukasz Magiera's avatar
Łukasz Magiera committed
27 28 29 30
type ObjectAPI struct {
	*CoreAPI
	*caopts.ObjectOptions
}
31

Łukasz Magiera's avatar
Łukasz Magiera committed
32 33 34 35 36 37 38 39 40 41
type Link struct {
	Name, Hash string
	Size       uint64
}

type Node struct {
	Links []Link
	Data  string
}

Łukasz Magiera's avatar
Łukasz Magiera committed
42 43 44 45 46
func (api *ObjectAPI) New(ctx context.Context, opts ...caopts.ObjectNewOption) (coreiface.Node, error) {
	options, err := caopts.ObjectNewOptions(opts...)
	if err != nil {
		return nil, err
	}
47

Łukasz Magiera's avatar
Łukasz Magiera committed
48 49 50 51 52 53 54 55 56
	var n node.Node
	switch options.Type {
	case "empty":
		n = new(dag.ProtoNode)
	case "unixfs-dir":
		n = ft.EmptyDirNode()
	}

	_, err = api.node.DAG.Add(n)
57 58 59
	if err != nil {
		return nil, err
	}
Łukasz Magiera's avatar
Łukasz Magiera committed
60
	return n, nil
61 62
}

63 64 65 66 67 68
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
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 119 120 121 122 123 124 125 126 127 128
	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
	}

	_, err = api.node.DAG.Add(dagnode)
	if err != nil {
		return nil, err
	}

	return ParseCid(dagnode.Cid()), nil
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 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 183 184 185 186
}

func (api *ObjectAPI) Get(ctx context.Context, path coreiface.Path) (coreiface.Node, error) {
	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
}

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

	links := nd.Links()
	out := make([]*coreiface.Link, len(links))
	for n, l := range links {
		out[n] = (*coreiface.Link)(l)
	}

	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
187
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
188 189 190 191 192 193
	options, err := caopts.ObjectAddLinkOptions(opts...)
	if err != nil {
		return nil, err
	}

	baseNd, err := api.core().ResolveNode(ctx, base)
194 195 196 197 198 199 200 201 202
	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
203
	basePb, ok := baseNd.(*dag.ProtoNode)
204 205 206 207 208
	if !ok {
		return nil, dag.ErrNotProtobuf
	}

	var createfunc func() *dag.ProtoNode
Łukasz Magiera's avatar
Łukasz Magiera committed
209
	if options.Create {
210 211 212
		createfunc = ft.EmptyDirNode
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
213
	e := dagutils.NewDagEditor(basePb, api.node.DAG)
214 215 216 217 218 219 220 221 222 223 224

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

	nnode, err := e.Finalize(api.node.DAG)
	if err != nil {
		return nil, err
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
225
	return ParseCid(nnode.Cid()), nil
226 227
}

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

Łukasz Magiera's avatar
Łukasz Magiera committed
234
	basePb, ok := baseNd.(*dag.ProtoNode)
235 236 237 238
	if !ok {
		return nil, dag.ErrNotProtobuf
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
239
	e := dagutils.NewDagEditor(basePb, api.node.DAG)
240 241 242 243 244 245 246 247 248 249 250

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

	nnode, err := e.Finalize(api.node.DAG)
	if err != nil {
		return nil, err
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
251
	return ParseCid(nnode.Cid()), nil
252 253
}

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

Łukasz Magiera's avatar
Łukasz Magiera committed
258
func (api *ObjectAPI) SetData(ctx context.Context, path coreiface.Path, r io.Reader) (coreiface.Path, error) {
259 260 261
	return api.patchData(ctx, path, r, false)
}

Łukasz Magiera's avatar
Łukasz Magiera committed
262
func (api *ObjectAPI) patchData(ctx context.Context, path coreiface.Path, r io.Reader, appendData bool) (coreiface.Path, error) {
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
	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)

	_, err = api.node.DAG.Add(pbnd)
	if err != nil {
		return nil, err
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
288
	return ParseCid(pbnd.Cid()), nil
289 290 291
}

func (api *ObjectAPI) core() coreiface.CoreAPI {
Łukasz Magiera's avatar
Łukasz Magiera committed
292
	return api.CoreAPI
293
}
Łukasz Magiera's avatar
Łukasz Magiera committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325

func deserializeNode(nd *Node, dataFieldEncoding string) (*dag.ProtoNode, error) {
	dagnode := new(dag.ProtoNode)
	switch dataFieldEncoding {
	case "text":
		dagnode.SetData([]byte(nd.Data))
	case "base64":
		data, _ := base64.StdEncoding.DecodeString(nd.Data)
		dagnode.SetData(data)
	default:
		return nil, fmt.Errorf("Unkown data field encoding")
	}

	dagnode.SetLinks(make([]*node.Link, len(nd.Links)))
	for i, link := range nd.Links {
		c, err := cid.Decode(link.Hash)
		if err != nil {
			return nil, err
		}
		dagnode.Links()[i] = &node.Link{
			Name: link.Name,
			Size: link.Size,
			Cid:  c,
		}
	}

	return dagnode, nil
}

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