mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-16 21:33:41 +00:00
fix: ByteFmt panic on negative int64 values (#1200)
This commit is contained in:
1 parent
3abf6ed933
commit
5515a1c48c
2 files changed
+30
-2
No files matched your search
+14
-1
@@ -5,14 +5,27 @@ import (
|
||||
"math"
|
||||
)
|
||||
|
||||
const unknownSize = "unknown"
|
||||
|
||||
var unitArr = []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"}
|
||||
|
||||
func ByteFmt(size int64) string {
|
||||
if size == 0 {
|
||||
return "unknown"
|
||||
return unknownSize
|
||||
}
|
||||
// Handle negative values
|
||||
if size < 0 {
|
||||
return unknownSize
|
||||
}
|
||||
fs := float64(size)
|
||||
p := int(math.Log(fs) / math.Log(1024))
|
||||
// Ensure index is within bounds
|
||||
if p < 0 {
|
||||
p = 0
|
||||
}
|
||||
if p >= len(unitArr) {
|
||||
p = len(unitArr) - 1
|
||||
}
|
||||
val := fs / math.Pow(1024, float64(p))
|
||||
_, frac := math.Modf(val)
|
||||
if frac > 0 {
|
||||
|
||||
Reference in new issue
Block a user