github.com/gogf/gf/v2@v2.7.4/os/gfile/gfile_size.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gfile 8 9 import ( 10 "fmt" 11 "os" 12 "strconv" 13 "strings" 14 ) 15 16 // Size returns the size of file specified by `path` in byte. 17 func Size(path string) int64 { 18 s, e := os.Stat(path) 19 if e != nil { 20 return 0 21 } 22 return s.Size() 23 } 24 25 // SizeFormat returns the size of file specified by `path` in format string. 26 func SizeFormat(path string) string { 27 return FormatSize(Size(path)) 28 } 29 30 // ReadableSize formats size of file given by `path`, for more human readable. 31 func ReadableSize(path string) string { 32 return FormatSize(Size(path)) 33 } 34 35 // StrToSize converts formatted size string to its size in bytes. 36 func StrToSize(sizeStr string) int64 { 37 i := 0 38 for ; i < len(sizeStr); i++ { 39 if sizeStr[i] == '.' || (sizeStr[i] >= '0' && sizeStr[i] <= '9') { 40 continue 41 } else { 42 break 43 } 44 } 45 var ( 46 unit = sizeStr[i:] 47 number, _ = strconv.ParseFloat(sizeStr[:i], 64) 48 ) 49 if unit == "" { 50 return int64(number) 51 } 52 switch strings.ToLower(unit) { 53 case "b", "bytes": 54 return int64(number) 55 case "k", "kb", "ki", "kib", "kilobyte": 56 return int64(number * 1024) 57 case "m", "mb", "mi", "mib", "mebibyte": 58 return int64(number * 1024 * 1024) 59 case "g", "gb", "gi", "gib", "gigabyte": 60 return int64(number * 1024 * 1024 * 1024) 61 case "t", "tb", "ti", "tib", "terabyte": 62 return int64(number * 1024 * 1024 * 1024 * 1024) 63 case "p", "pb", "pi", "pib", "petabyte": 64 return int64(number * 1024 * 1024 * 1024 * 1024 * 1024) 65 case "e", "eb", "ei", "eib", "exabyte": 66 return int64(number * 1024 * 1024 * 1024 * 1024 * 1024 * 1024) 67 case "z", "zb", "zi", "zib", "zettabyte": 68 return int64(number * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024) 69 case "y", "yb", "yi", "yib", "yottabyte": 70 return int64(number * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024) 71 case "bb", "brontobyte": 72 return int64(number * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024) 73 } 74 return -1 75 } 76 77 // FormatSize formats size `raw` for more manually readable. 78 func FormatSize(raw int64) string { 79 var r float64 = float64(raw) 80 var t float64 = 1024 81 var d float64 = 1 82 if r < t { 83 return fmt.Sprintf("%.2fB", r/d) 84 } 85 d *= 1024 86 t *= 1024 87 if r < t { 88 return fmt.Sprintf("%.2fK", r/d) 89 } 90 d *= 1024 91 t *= 1024 92 if r < t { 93 return fmt.Sprintf("%.2fM", r/d) 94 } 95 d *= 1024 96 t *= 1024 97 if r < t { 98 return fmt.Sprintf("%.2fG", r/d) 99 } 100 d *= 1024 101 t *= 1024 102 if r < t { 103 return fmt.Sprintf("%.2fT", r/d) 104 } 105 d *= 1024 106 t *= 1024 107 if r < t { 108 return fmt.Sprintf("%.2fP", r/d) 109 } 110 d *= 1024 111 t *= 1024 112 if r < t { 113 return fmt.Sprintf("%.2fE", r/d) 114 } 115 d *= 1024 116 t *= 1024 117 if r < t { 118 return fmt.Sprintf("%.2fZ", r/d) 119 } 120 d *= 1024 121 t *= 1024 122 if r < t { 123 return fmt.Sprintf("%.2fY", r/d) 124 } 125 d *= 1024 126 t *= 1024 127 if r < t { 128 return fmt.Sprintf("%.2fBB", r/d) 129 } 130 return "TooLarge" 131 }