용량 컨버전 로직 추가
This commit is contained in:
parent
d9d96073e8
commit
f673c30fb4
|
@ -2,9 +2,11 @@ package misc
|
|||
|
||||
import (
|
||||
"strconv"
|
||||
"fmt"
|
||||
"math"
|
||||
"image"
|
||||
)
|
||||
|
||||
|
||||
func mustParseInt(value string, bits int) int64 {
|
||||
if parsed, err := strconv.ParseInt(value, 10, bits); err != nil {
|
||||
panic(err)
|
||||
|
@ -124,3 +126,65 @@ func FormatInt16(value int16) string { return strconv.FormatInt(int64(value), 10
|
|||
func FormatInt32(value int32) string { return strconv.FormatInt(int64(value), 10) }
|
||||
func FormatInt64(value int64) string { return strconv.FormatInt(value, 10) }
|
||||
func FormatInt(value int) string { return strconv.FormatInt(int64(value), 10) }
|
||||
|
||||
const (
|
||||
Byte = 1 << (10 * iota)
|
||||
KiByte
|
||||
MiByte
|
||||
GiByte
|
||||
TiByte
|
||||
PiByte
|
||||
EiByte
|
||||
ZiByte
|
||||
YiByte
|
||||
)
|
||||
|
||||
var bytesSizeTable = map[string]uint64{
|
||||
"B": Byte,
|
||||
"KiB": KiByte,
|
||||
"MiB": MiByte,
|
||||
"GiB": GiByte,
|
||||
"TiB": TiByte,
|
||||
"PiB": PiByte,
|
||||
"EiB": EiByte,
|
||||
"ZiB": ZiByte,
|
||||
"YiB": YiByte,
|
||||
}
|
||||
|
||||
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 calculates the file size and generate user-friendly string.
|
||||
func FileSize(s uint64) string {
|
||||
sizes := []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"}
|
||||
return humanateBytes(s, 1024, sizes)
|
||||
}
|
||||
|
||||
func AspectRatio(srcRect image.Point, toResize uint64) image.Point {
|
||||
w, h := int(toResize), getRatioSize(int(toResize), srcRect.Y, srcRect.X)
|
||||
if srcRect.X < srcRect.Y {
|
||||
w, h = getRatioSize(int(toResize), srcRect.X, srcRect.Y), int(toResize)
|
||||
}
|
||||
return image.Point{w, h}
|
||||
}
|
||||
|
||||
func getRatioSize(a, b, c int) int {
|
||||
d := a * b / c
|
||||
return (d + 1) & -1
|
||||
}
|
||||
|
|
Reference in New Issue