-
Notifications
You must be signed in to change notification settings - Fork 276
/
byte.go
173 lines (149 loc) · 3.25 KB
/
byte.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Copyright 2020 The Dragonfly Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package unit
import (
"encoding/json"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
"d7y.io/dragonfly/v2/pkg/util/stringutils"
)
type Bytes int64
const (
B Bytes = 1
KB = 1024 * B
MB = 1024 * KB
GB = 1024 * MB
TB = 1024 * GB
PB = 1024 * TB
EB = 1024 * PB
)
func (f Bytes) ToNumber() int64 {
return int64(f)
}
func ToBytes(size int64) Bytes {
return Bytes(size)
}
// Set is used for command flag var
func (f *Bytes) Set(s string) (err error) {
if stringutils.IsBlank(s) {
*f = 0
} else {
*f, err = parseSize(s)
}
return
}
func (f Bytes) Type() string {
return "bytes"
}
func (f Bytes) String() string {
var (
symbol string
unit Bytes
)
if f >= PB {
symbol = "PB"
unit = PB
} else if f >= TB {
symbol = "TB"
unit = TB
} else if f >= GB {
symbol = "GB"
unit = GB
} else if f >= MB {
symbol = "MB"
unit = MB
} else if f >= KB {
symbol = "KB"
unit = KB
} else {
symbol = "B"
unit = B
}
return fmt.Sprintf("%.1f%s", float64(f)/float64(unit), symbol)
}
var sizeRegexp = regexp.MustCompile(`^([0-9]+)(\.0*)?([MmKkGgTtPpEe])?[iI]?[bB]?$`)
func parseSize(fsize string) (Bytes, error) {
fsize = strings.TrimSpace(fsize)
if stringutils.IsBlank(fsize) {
return 0, nil
}
matches := sizeRegexp.FindStringSubmatch(fsize)
if len(matches) == 0 {
return 0, errors.Errorf("parse size %s: invalid format", fsize)
}
var unit Bytes
switch matches[3] {
case "k", "K":
unit = KB
case "m", "M":
unit = MB
case "g", "G":
unit = GB
case "t", "T":
unit = TB
case "p", "P":
unit = PB
case "e", "E":
unit = EB
default:
unit = B
}
num, err := strconv.ParseInt(matches[1], 0, 64)
if err != nil {
return 0, errors.Wrapf(err, "failed to parse size: %s", fsize)
}
return ToBytes(num) * unit, nil
}
func (f Bytes) MarshalYAML() (interface{}, error) {
result := f.String()
return result, nil
}
func (f *Bytes) UnmarshalJSON(b []byte) error {
return f.unmarshal(json.Unmarshal, b)
}
func (f *Bytes) UnmarshalYAML(node *yaml.Node) error {
return f.unmarshal(yaml.Unmarshal, []byte(node.Value))
}
func (f *Bytes) unmarshal(unmarshal func(in []byte, out interface{}) (err error), b []byte) error {
var v interface{}
if err := unmarshal(b, &v); err != nil {
return err
}
switch value := v.(type) {
case float64:
*f = Bytes(int64(value))
return nil
case int:
*f = Bytes(int64(value))
return nil
case int64:
*f = Bytes(value)
return nil
case string:
size, err := parseSize(value)
if err != nil {
return errors.WithMessage(err, "invalid byte size")
}
*f = size
return nil
default:
return errors.New("invalid byte size")
}
}