-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsizelimit.go
76 lines (61 loc) · 1.52 KB
/
sizelimit.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
package sizelimit
import (
"bytes"
"encoding/json"
"regexp"
"strconv"
"github.com/luraproject/lura/v2/config"
)
const Namespace = "kivra/sizelimit"
type Config struct {
MaxSize string `json:"max_size"`
}
type ConfigInternal struct {
MaxSize int64 `json:"max_size"`
}
func ConfigGetter(e config.ExtraConfig) (*ConfigInternal, bool) {
tmp, ok := e[Namespace]
if !ok {
return new(ConfigInternal), false
}
cfg := new(Config)
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(tmp); err != nil {
panic("sizelimit: Error: failed to parse config")
}
if err := json.NewDecoder(buf).Decode(cfg); err != nil {
panic("sizelimit: Error: failed to parse config")
}
cfgInternal := new(ConfigInternal)
cfgInternal.MaxSize = ParseMaxSize(cfg)
return cfgInternal, true
}
func ParseMaxSize(cfg *Config) int64 {
rex := regexp.MustCompile(`^([\d.]+)([a-zA-Z]*)$`)
matches := rex.FindStringSubmatch(cfg.MaxSize)
if matches == nil || len(matches) != 3 || matches[0] != cfg.MaxSize {
panic("sizelimit: Error: invalid value for 'max_size'")
}
value, err := strconv.ParseFloat(matches[1], 64)
if err != nil {
panic("sizelimit: Error: invalid value for 'max_size'")
}
var multiplier float64
switch matches[2] {
case "":
multiplier = 1
case "B":
multiplier = 1
case "kB":
multiplier = 1000
case "MB":
multiplier = 1000000
case "GB":
multiplier = 1000000000
case "TB":
multiplier = 1000000000000
default:
panic("sizelimit: Error: unknown unit: " + cfg.MaxSize)
}
return int64(value * multiplier)
}