forked from Angey40/BaiduPCS-Go
-
Notifications
You must be signed in to change notification settings - Fork 4
/
instance_state.go
59 lines (54 loc) · 1.4 KB
/
instance_state.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 uploader
import (
"io"
)
type (
// BlockState 文件区块信息
BlockState struct {
ID int `json:"id"`
Range ReadRange `json:"range"`
CheckSum string `json:"checksum"`
}
// InstanceState 上传断点续传信息
InstanceState struct {
BlockList []*BlockState `json:"block_list"`
}
)
func workerListToInstanceState(workers workerList) *InstanceState {
blockStates := make([]*BlockState, 0, len(workers))
for _, wer := range workers {
blockStates = append(blockStates, &BlockState{
ID: wer.id,
Range: wer.splitUnit.Range(),
CheckSum: wer.checksum,
})
}
return &InstanceState{
BlockList: blockStates,
}
}
func instanceStateToWorkerList(is *InstanceState, file io.ReaderAt) workerList {
workers := make(workerList, 0, len(is.BlockList))
for _, blockState := range is.BlockList {
if blockState.CheckSum == "" {
workers = append(workers, &worker{
id: blockState.ID,
partOffset: blockState.Range.Begin,
splitUnit: NewSplitUnit(file, blockState.Range),
checksum: blockState.CheckSum,
})
} else {
workers = append(workers, &worker{
id: blockState.ID,
partOffset: blockState.Range.Begin,
splitUnit: &fileBlock{
readRange: blockState.Range,
readed: blockState.Range.End - blockState.Range.Begin,
readerAt: file,
},
checksum: blockState.CheckSum,
})
}
}
return workers
}