package utils import ( "fmt" "io/fs" "path/filepath" "strconv" "strings" ) var sizeSuffixes = [...]string{"B", "KiB", "MiB", "GiB", "TiB"} // ConvertFileSize converts size in bytes down to biggest units it represents. // Returns converted size, unit and, a concatenation of size and unit func ConvertFileSize(size int64) (float64, string, string) { var idx int var fSize float64 = float64(size) for idx = 0; fSize >= 1024; fSize /= 1024 { idx++ } fSizeStr := strconv.FormatFloat(fSize, 'f', 3, 64) fSizeStr = strings.TrimRight(fSizeStr, "0") fSizeStr = strings.TrimSuffix(fSizeStr, ".") return fSize, sizeSuffixes[idx], fmt.Sprint(fSizeStr, sizeSuffixes[idx]) } func DirectorySize(path string) (dirSz int64, err error) { err = filepath.Walk(path, func(_ string, info fs.FileInfo, err error) error { if err != nil { return err } dirSz += info.Size() return nil }) if err != nil { return dirSz, err } return dirSz, nil }