@cryptotaxi247 / kubo / commits / fdcebc5a2

importer

Juan Batiz-Benet committed Jul 5, 2014 at 15:03 UTC fdcebc5a2001bc8d6709bc325e17c76ece334251
1 file changed +40
importer/importer.go new
+40
@@ -0,0 +1,40 @@
1 +package importer
2 +
3 +import (
4 + "fmt"
5 + dag "github.com/jbenet/go-ipfs/merkledag"
6 + "io"
7 + "io/ioutil"
8 +)
9 +
10 +var BlockSizeLimit = int64(1048576) // 1 MB
11 +var SizeLimitExceeded = fmt.Errorf("object size limit exceeded")
12 +
13 +// todo: incremental construction with an ipfs node. dumping constructed
14 +// objects into the datastore, to avoid buffering all in memory
15 +
16 +// size required for block construction
17 +func NewDagFromReader(r io.Reader, size int64) (*dag.Node, error) {
18 + // todo: block-splitting based on rabin fingerprinting
19 + // todo: block-splitting with user-defined function
20 + // todo: block-splitting at all. :P
21 +
22 + // totally just trusts the reported size. fix later.
23 + if size > BlockSizeLimit { // 1 MB limit for now.
24 + return nil, SizeLimitExceeded
25 + }
26 +
27 + // we're doing it live!
28 + buf, err := ioutil.ReadAll(r)
29 + if err != nil {
30 + return nil, err
31 + }
32 +
33 + if int64(len(buf)) > BlockSizeLimit {
34 + return nil, SizeLimitExceeded // lying punk.
35 + }
36 +
37 + root := &dag.Node{Data: buf}
38 + // no children for now because not block splitting yet
39 + return root, nil
40 +}