/
limits.go
122 lines (98 loc) · 2.8 KB
/
limits.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
package git
import (
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/ARM-software/golang-utils/utils/config"
"github.com/ARM-software/golang-utils/utils/units/multiplication"
"github.com/ARM-software/golang-utils/utils/units/size"
)
type noLimits struct {
}
func (n *noLimits) Apply() bool {
return false
}
func (n *noLimits) GetMaxFileSize() int64 {
return 0
}
func (n *noLimits) GetMaxTotalSize() int64 {
return 0
}
func (n *noLimits) GetMaxFileCount() int64 {
return 0
}
func (n *noLimits) GetMaxTreeDepth() int64 {
return 0
}
func (n *noLimits) GetMaxEntries() int64 {
return 0
}
func (n *noLimits) GetMaxTrueSize() int64 {
return 0
}
func (n *noLimits) Validate() error {
return nil
}
// Limits defines file system limits
type Limits struct {
MaxFileSize int64 `mapstructure:"max_file_size"`
MaxTotalSize int64 `mapstructure:"max_total_size"`
MaxFileCount int64 `mapstructure:"max_file_count"`
MaxTreeDepth int64 `mapstructure:"max_tree_depth"`
MaxEntries int64 `mapstructure:"max_entries"`
MaxTrueSize int64 `mapstructure:"max_true_size"`
}
func (l *Limits) Apply() bool {
return true
}
func (l *Limits) GetMaxFileSize() int64 {
return l.MaxFileSize
}
func (l *Limits) GetMaxTotalSize() int64 {
return l.MaxTotalSize
}
func (l *Limits) GetMaxFileCount() int64 {
return l.MaxFileCount
}
func (l *Limits) GetMaxTreeDepth() int64 {
return l.MaxTreeDepth
}
func (l *Limits) GetMaxEntries() int64 {
return l.MaxEntries
}
func (l *Limits) GetMaxTrueSize() int64 {
return l.MaxTrueSize
}
func (l *Limits) Validate() error {
validation.ErrorTag = "mapstructure"
// Validate Embedded Structs
err := config.ValidateEmbedded(l)
if err != nil {
return err
}
return validation.ValidateStruct(l,
validation.Field(&l.MaxFileSize, validation.Required.When(l.Apply())),
validation.Field(&l.MaxTotalSize, validation.Required.When(l.Apply())),
validation.Field(&l.MaxFileCount, validation.Required.When(l.Apply())),
validation.Field(&l.MaxTreeDepth, validation.Required.When(l.Apply())),
validation.Field(&l.MaxEntries, validation.Required.When(l.Apply())),
validation.Field(&l.MaxTrueSize, validation.Required.When(l.Apply())),
)
}
// NoLimits defines no file system FileSystemLimits
func NoLimits() ILimits {
return &noLimits{}
}
// NewLimits defines file system FileSystemLimits.
func NewLimits(maxFileSize, maxTotalSize, maxFileCount, maxTreeDepth, maxEntries, maxTrueSize int64) ILimits {
return &Limits{
MaxFileSize: maxFileSize,
MaxTotalSize: maxTotalSize,
MaxFileCount: maxFileCount,
MaxTreeDepth: maxTreeDepth,
MaxEntries: maxEntries,
MaxTrueSize: maxTrueSize,
}
}
// DefaultLimits defines default file system FileSystemLimits
func DefaultLimits() ILimits {
return NewLimits(100*size.MB, 10*size.GB, multiplication.Mega, 12, 100*multiplication.Mega, size.GB)
}