2022-03-17 00:33:59 +00:00
|
|
|
package api
|
2020-07-23 01:56:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2022-03-17 00:33:59 +00:00
|
|
|
type byteRange struct {
|
2020-07-23 01:56:08 +00:00
|
|
|
Start int64
|
|
|
|
End *int64
|
|
|
|
RawString string
|
|
|
|
}
|
|
|
|
|
2022-03-17 00:33:59 +00:00
|
|
|
func createByteRange(s string) byteRange {
|
2020-07-23 01:56:08 +00:00
|
|
|
// strip bytes=
|
|
|
|
r := strings.TrimPrefix(s, "bytes=")
|
|
|
|
e := strings.Split(r, "-")
|
|
|
|
|
2022-03-17 00:33:59 +00:00
|
|
|
ret := byteRange{
|
2020-07-23 01:56:08 +00:00
|
|
|
RawString: s,
|
|
|
|
}
|
|
|
|
if len(e) > 0 {
|
|
|
|
ret.Start, _ = strconv.ParseInt(e[0], 10, 64)
|
|
|
|
}
|
|
|
|
if len(e) > 1 && e[1] != "" {
|
|
|
|
end, _ := strconv.ParseInt(e[1], 10, 64)
|
|
|
|
ret.End = &end
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2022-03-17 00:33:59 +00:00
|
|
|
func (r byteRange) toHeaderValue(fileLength int64) string {
|
2020-07-23 01:56:08 +00:00
|
|
|
if r.End == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
end := *r.End
|
|
|
|
return "bytes " + strconv.FormatInt(r.Start, 10) + "-" + strconv.FormatInt(end, 10) + "/" + strconv.FormatInt(fileLength, 10)
|
|
|
|
}
|
|
|
|
|
2022-03-17 00:33:59 +00:00
|
|
|
func (r byteRange) apply(bytes []byte) []byte {
|
2020-07-23 01:56:08 +00:00
|
|
|
if r.End == nil {
|
|
|
|
return bytes[r.Start:]
|
|
|
|
}
|
|
|
|
|
|
|
|
end := *r.End + 1
|
|
|
|
if int(end) > len(bytes) {
|
|
|
|
end = int64(len(bytes))
|
|
|
|
}
|
|
|
|
return bytes[r.Start:end]
|
|
|
|
}
|