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