-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
util.go
57 lines (49 loc) · 1.18 KB
/
util.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
package pget
import (
"fmt"
"os"
"path/filepath"
)
func getPartialDirname(targetDir, filename string, procs int) string {
if targetDir == "" {
return fmt.Sprintf("_%s.%d", filename, procs)
}
return filepath.Join(targetDir, fmt.Sprintf("_%s.%d", filename, procs))
}
// getPartialFilePath returns the path of the partial file
func getPartialFilePath(targetDir, filename string, id, procs int) string {
return filepath.Join(
targetDir,
fmt.Sprintf("%s.%d.%d", filename, procs, id),
)
}
// checkProgress In order to confirm the degree of progress
func checkProgress(dirname string) (int64, error) {
return subDirsize(dirname)
}
func subDirsize(dirname string) (int64, error) {
var size int64
err := filepath.Walk(dirname, func(_ string, info os.FileInfo, err error) error {
if !info.IsDir() {
size += info.Size()
}
return err
})
return size, err
}
func makeRange(i, procs int, rangeSize, contentLength int64) Range {
low := rangeSize * int64(i)
if i == procs-1 {
return Range{
low: low,
high: contentLength,
}
}
return Range{
low: low,
high: low + rangeSize - 1,
}
}
func (r Range) BytesRange() string {
return fmt.Sprintf("bytes=%d-%d", r.low, r.high)
}