@cryptotaxi247 / kubo / commits / c68ab5620

coreapi unixfs: cid prefix options

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed Sep 20, 2018 at 15:59 UTC c68ab5620a8a1026c2f817c5831ca671b24ca70e
3 files changed +56 -5
core/coreapi/interface/options/unixfs.go
+6 -2
@@ -8,7 +8,9 @@ type UnixfsAddSettings struct {
8 CidVersion int
9 MhType uint64
10
11 - InlineLimit int
11 + InlineLimit int
12 + RawLeaves bool
13 + RawLeavesSet bool
14 }
15
16 type UnixfsAddOption func(*UnixfsAddSettings) error
@@ -18,7 +20,9 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, error) {
20 CidVersion: -1,
21 MhType: mh.SHA2_256,
22
21 - InlineLimit: 0,
23 + InlineLimit: 0,
24 + RawLeaves: false,
25 + RawLeavesSet: false,
26 }
27
28 for _, opt := range opts {
core/coreapi/interface/unixfs.go
+1
@@ -10,6 +10,7 @@ import (
10 )
11
12 // UnixfsAPI is the basic interface to immutable files in IPFS
13 +// NOTE: This API is heavily WIP, things are guaranteed to break frequently
14 type UnixfsAPI interface {
15 // Add imports the data from the reader into merkledag file
16 Add(context.Context, io.ReadCloser, ...options.UnixfsAddOption) (ResolvedPath, error)
core/coreapi/unixfs.go
+49 -3
@@ -2,15 +2,19 @@ package coreapi
2
3 import (
4 "context"
5 + "errors"
6 + "fmt"
7 "io"
8
9 coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
8 - options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
9 - coreunix "github.com/ipfs/go-ipfs/core/coreunix"
10 + "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
11 + "github.com/ipfs/go-ipfs/core/coreunix"
12
13 cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
14 + mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
15 files "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit/files"
16 uio "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/io"
17 + dag "gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag"
18 ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format"
19 )
20
@@ -19,11 +23,42 @@ type UnixfsAPI CoreAPI
23 // Add builds a merkledag node from a reader, adds it to the blockstore,
24 // and returns the key representing that node.
25 func (api *UnixfsAPI) Add(ctx context.Context, r io.ReadCloser, opts ...options.UnixfsAddOption) (coreiface.ResolvedPath, error) {
22 - _, err := options.UnixfsAddOptions(opts...)
26 + settings, err := options.UnixfsAddOptions(opts...)
27 if err != nil {
28 return nil, err
29 }
30
31 + // TODO: move to options
32 + // (hash != "sha2-256") -> CIDv1
33 + if settings.MhType != mh.SHA2_256 {
34 + switch settings.CidVersion {
35 + case 0:
36 + return nil, errors.New("CIDv0 only supports sha2-256")
37 + case 1, -1:
38 + settings.CidVersion = 1
39 + default:
40 + return nil, fmt.Errorf("unknown CID version: %d", settings.CidVersion)
41 + }
42 + } else {
43 + if settings.CidVersion < 0 {
44 + // Default to CIDv0
45 + settings.CidVersion = 0
46 + }
47 + }
48 +
49 + // cidV1 -> raw blocks (by default)
50 + if settings.CidVersion > 0 && !settings.RawLeavesSet {
51 + settings.RawLeaves = true
52 + }
53 +
54 + prefix, err := dag.PrefixForCidVersion(settings.CidVersion)
55 + if err != nil {
56 + return nil, err
57 + }
58 +
59 + prefix.MhType = settings.MhType
60 + prefix.MhLength = -1
61 +
62 outChan := make(chan interface{}, 1)
63
64 fileAdder, err := coreunix.NewAdder(ctx, api.node.Pinning, api.node.Blockstore, api.node.DAG)
@@ -32,6 +67,17 @@ func (api *UnixfsAPI) Add(ctx context.Context, r io.ReadCloser, opts ...options.
67 }
68
69 fileAdder.Out = outChan
70 + //fileAdder.Chunker = chunker
71 + //fileAdder.Progress = progress
72 + //fileAdder.Hidden = hidden
73 + //fileAdder.Trickle = trickle
74 + //fileAdder.Wrap = wrap
75 + //fileAdder.Pin = dopin
76 + fileAdder.Silent = false
77 + fileAdder.RawLeaves = settings.RawLeaves
78 + //fileAdder.NoCopy = nocopy
79 + //fileAdder.Name = pathName
80 + fileAdder.CidBuilder = prefix
81
82 err = fileAdder.AddFile(files.NewReaderFile("", "", r, nil))
83 if err != nil {