|
|
|
@ -1,7 +1,9 @@
|
|
|
|
|
package gofiles
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
"math"
|
|
|
|
|
"os"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
@ -99,3 +101,28 @@ func CreateDir(path string) error {
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func logn(n, b float64) float64 {
|
|
|
|
|
return math.Log(n) / math.Log(b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func humanateBytes(s uint64, base float64, sizes []string) string {
|
|
|
|
|
if s < 10 {
|
|
|
|
|
return fmt.Sprintf("%dB", s)
|
|
|
|
|
}
|
|
|
|
|
e := math.Floor(logn(float64(s), base))
|
|
|
|
|
suffix := sizes[int(e)]
|
|
|
|
|
val := float64(s) / math.Pow(base, math.Floor(e))
|
|
|
|
|
f := "%.0f"
|
|
|
|
|
if val < 10 {
|
|
|
|
|
f = "%.1f"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fmt.Sprintf(f+"%s", val, suffix)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FileSize 计算文件大小并生成用户友好的字符串。
|
|
|
|
|
func FileSize(s uint64) string {
|
|
|
|
|
sizes := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"}
|
|
|
|
|
return humanateBytes(uint64(s), 1024, sizes)
|
|
|
|
|
}
|
|
|
|
|