forked from filedrive-team/filehelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filebuild.go
96 lines (87 loc) · 2.11 KB
/
filebuild.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
60
61
62
63
64
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
package filehelper
import (
"io"
"os"
"github.com/ipfs/go-cid"
chunker "github.com/ipfs/go-ipfs-chunker"
"github.com/ipfs/go-merkledag"
"github.com/ipfs/go-unixfs/importer/balanced"
ihelper "github.com/ipfs/go-unixfs/importer/helpers"
ipld "github.com/ipfs/go-ipld-format"
)
const UnixfsLinksPerLevel = 1 << 10
const UnixfsChunkSize uint64 = 1 << 20
func BuildFileNode(item Finfo, bufDs ipld.DAGService, cidBuilder cid.Builder) (node ipld.Node, err error) {
var r io.Reader
f, err := os.Open(item.Path)
if err != nil {
return nil, err
}
r = f
// read all data of item
if item.SeekStart > 0 || item.SeekEnd > 0 {
r = &FileSlice{
r: f,
start: item.SeekStart,
end: item.SeekEnd,
fileSize: item.Info.Size(),
}
}
params := ihelper.DagBuilderParams{
Maxlinks: UnixfsLinksPerLevel,
RawLeaves: false,
CidBuilder: cidBuilder,
Dagserv: bufDs,
NoCopy: false,
}
db, err := params.New(chunker.NewSizeSplitter(r, int64(UnixfsChunkSize)))
if err != nil {
return nil, err
}
node, err = balanced.Layout(db)
if err != nil {
return nil, err
}
return
}
// BuildFileNodeV0 - build ipld with cid v0
func BuildFileNodeV0(f *os.File, bufDs ipld.DAGService) (node ipld.Node, err error) {
cidBuilder, err := merkledag.PrefixForCidVersion(0)
if err != nil {
return
}
params := ihelper.DagBuilderParams{
Maxlinks: UnixfsLinksPerLevel,
RawLeaves: false,
CidBuilder: cidBuilder,
Dagserv: bufDs,
NoCopy: false,
}
db, err := params.New(chunker.NewSizeSplitter(f, int64(UnixfsChunkSize)))
if err != nil {
return nil, err
}
node, err = balanced.Layout(db)
if err != nil {
return nil, err
}
return
}
func BalanceNode(f io.Reader, bufDs ipld.DAGService, cidBuilder cid.Builder) (node ipld.Node, err error) {
params := ihelper.DagBuilderParams{
Maxlinks: UnixfsLinksPerLevel,
RawLeaves: false,
CidBuilder: cidBuilder,
Dagserv: bufDs,
NoCopy: false,
}
db, err := params.New(chunker.NewSizeSplitter(f, int64(UnixfsChunkSize)))
if err != nil {
return nil, err
}
node, err = balanced.Layout(db)
if err != nil {
return nil, err
}
return
}