-
Notifications
You must be signed in to change notification settings - Fork 66
/
unixfs.go
59 lines (51 loc) · 1.32 KB
/
unixfs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package util
import (
"errors"
"fmt"
"io"
chunker "github.com/ipfs/go-ipfs-chunker"
ipld "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-merkledag"
"github.com/ipfs/go-unixfs"
"github.com/ipfs/go-unixfs/importer/balanced"
ihelper "github.com/ipfs/go-unixfs/importer/helpers"
mh "github.com/multiformats/go-multihash"
)
var DefaultHashFunction = uint64(mh.SHA2_256)
func ImportFile(dserv ipld.DAGService, fi io.Reader) (ipld.Node, error) {
prefix, err := merkledag.PrefixForCidVersion(1)
if err != nil {
return nil, err
}
prefix.MhType = DefaultHashFunction
prefix.MhLength = -1
spl := chunker.NewSizeSplitter(fi, 1024*1024)
dbp := ihelper.DagBuilderParams{
Dagserv: dserv,
Maxlinks: ihelper.DefaultLinksPerBlock,
RawLeaves: true,
CidBuilder: &prefix,
}
db, err := dbp.New(spl)
if err != nil {
return nil, err
}
return balanced.Layout(db)
}
func TryExtractFSNode(nd ipld.Node) (*unixfs.FSNode, error) {
switch nd := nd.(type) {
case *merkledag.ProtoNode:
n, err := unixfs.FSNodeFromBytes(nd.Data())
if err != nil {
return nil, err
}
if n.Type() == unixfs.TSymlink {
return nil, fmt.Errorf("symlinks not supported")
}
return n, nil // success!
case *merkledag.RawNode:
default:
return nil, errors.New("unknown node type")
}
return nil, errors.New("unknown node type")
}