/
splitter.go
111 lines (99 loc) · 2.17 KB
/
splitter.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package core
// function to split a file into chunks
import (
"context"
"fmt"
"github.com/ipfs/go-merkledag"
"io"
"os"
)
var defaultChuckSize int64 = 1024 * 1024
type UploadSplits struct {
Cid string `json:"cid"`
Index int `json:"index"`
ContentId int64 `json:"contentId"`
}
type FileSplitter struct {
SplitterParam
}
type SplitterParam struct {
ChuckSize int64
LightNode *LightNode
}
type SplitChunk struct {
Cid string
Chunk []byte `json:"Chunk,omitempty"`
Size int
Index int
}
func NewFileSplitter(param SplitterParam) FileSplitter {
if param.ChuckSize == 0 {
param.ChuckSize = defaultChuckSize
}
return FileSplitter{
SplitterParam: param,
}
}
func (c FileSplitter) SplitFileFromReaderIntoBlockstore(fileFromReader io.Reader) ([]SplitChunk, error) {
// Read the file into a buffer
buf := make([]byte, c.ChuckSize)
//var chunks [][]byte
var splitChunks []SplitChunk
var i = 0
for {
n, err := fileFromReader.Read(buf)
if n == 0 {
break
}
if err != nil {
return nil, fmt.Errorf("Error reading file: %v", err)
}
rawNode := merkledag.NewRawNode(buf[:n])
c.LightNode.Node.Add(context.Background(), rawNode)
splitChunks = append(splitChunks, SplitChunk{
//Chunk: buf[:n],
Index: i,
Size: n,
Cid: rawNode.Cid().String(),
})
i++
}
return splitChunks, nil
}
func (c FileSplitter) SplitFileFromReader(fileFromReader io.Reader) ([][]byte, error) {
// Read the file into a buffer
buf := make([]byte, c.ChuckSize)
var chunks [][]byte
for {
n, err := fileFromReader.Read(buf)
if n == 0 {
break
}
if err != nil {
return nil, fmt.Errorf("Error reading file: %v", err)
}
chunks = append(chunks, buf[:n])
}
return chunks, nil
}
func (c FileSplitter) SplitFile(filePath string) ([][]byte, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("Error opening file: %v", err)
}
defer file.Close()
// Read the file into a buffer
buf := make([]byte, c.ChuckSize)
var chunks [][]byte
for {
n, err := file.Read(buf)
if n == 0 {
break
}
if err != nil {
return nil, fmt.Errorf("Error reading file: %v", err)
}
chunks = append(chunks, buf[:n])
}
return chunks, nil
}