-
Notifications
You must be signed in to change notification settings - Fork 1
/
chunker.go
63 lines (52 loc) · 1.46 KB
/
chunker.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
package libs
import (
"io"
"path/filepath"
chunker "github.com/ipfs/go-ipfs-chunker"
)
const UnixfsChunkSize uint64 = 1 << 20 //Deafault chunksize 2M
// Reading data while obtaining source data information, including the source data file path, offset, and size:
type SliceMeta struct {
Path string
Offset uint64
Size uint64
}
// Defining the EnhancedSplitter interface as an extension of the Splitter interface,
// which reads data while also returning source data path, offset, and size
type EnhancedSplitter interface {
chunker.Splitter
NextBytesWithMeta() ([]byte, *SliceMeta, error)
}
type sliceSplitter struct {
chunker.Splitter
// source data's path
srcPath string
offset uint64
chunkStart uint64
}
func NewSplitter(r io.Reader, size int64, srcPath string, parentPath string, chunkStart uint64) (EnhancedSplitter, error) {
path, err := filepath.Rel(filepath.Clean(parentPath), filepath.Clean(srcPath))
if err != nil {
return nil, err
}
spl := chunker.NewSizeSplitter(r, size)
return &sliceSplitter{
Splitter: spl,
srcPath: path,
offset: 0,
chunkStart: chunkStart,
}, nil
}
func (ss *sliceSplitter) NextBytesWithMeta() ([]byte, *SliceMeta, error) {
buf, err := ss.NextBytes()
size := len(buf)
//Constructing a source data SliceMeta
m := &SliceMeta{
Path: ss.srcPath,
Offset: ss.offset + ss.chunkStart,
Size: uint64(size),
}
ss.offset += uint64(size)
return buf, m, err
}
var _ EnhancedSplitter = &sliceSplitter{}