mirror of
https://github.com/Sagit-chu/flvx.git
synced 2026-09-28 07:36:38 +08:00
29 lines
775 B
Go
29 lines
775 B
Go
package handler
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"strconv"
|
|
)
|
|
|
|
// flowMiB is optional so older clients can keep sending the GB-based flow field.
|
|
// A positive value takes precedence and preserves sub-GB limits exactly.
|
|
func parseTrafficLimit(req map[string]interface{}, defaultGB int64) (flowGB, flowMiB int64, err error) {
|
|
flowGB = asInt64(req["flow"], defaultGB)
|
|
if flowGB < 0 {
|
|
return 0, 0, fmt.Errorf("流量限制不能小于0")
|
|
}
|
|
raw, present := req["flowMiB"]
|
|
if !present {
|
|
return flowGB, 0, nil
|
|
}
|
|
flowMiB, err = strconv.ParseInt(asString(raw), 10, 64)
|
|
if err != nil || flowMiB < 0 || flowMiB > math.MaxInt64/bytesPerMiB {
|
|
return 0, 0, fmt.Errorf("流量限制超出范围")
|
|
}
|
|
if flowMiB > 0 {
|
|
flowGB = (flowMiB-1)/1024 + 1
|
|
}
|
|
return flowGB, flowMiB, nil
|
|
}
|