github.com/laof/lite-speed-test@v0.0.0-20230930011949-1f39b7037845/download/range.go (about) 1 package download 2 3 import "fmt" 4 5 type Range struct { 6 Url string 7 RangeFrom int64 8 RangeTo int64 9 } 10 11 func (rng Range) toHeader(contentLength int64) string { 12 var ranges string 13 if rng.RangeTo != int64(contentLength) { 14 ranges = fmt.Sprintf("bytes=%d-%d", rng.RangeFrom, rng.RangeTo) 15 } else { 16 ranges = fmt.Sprintf("bytes=%d-", rng.RangeFrom) //get all 17 } 18 return ranges 19 } 20 21 func calcRange(part int64, len int64, url string) []Range { 22 ret := []Range{} 23 for j := int64(0); j < part; j++ { 24 from := (len / part) * j 25 var to int64 26 if j < part-1 { 27 to = (len/part)*(j+1) - 1 28 } else { 29 to = len 30 } 31 32 ret = append(ret, Range{Url: url, RangeFrom: from, RangeTo: to}) 33 } 34 return ret 35 }