-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
190 lines (165 loc) · 4.02 KB
/
utils.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package sysutils
import (
"fmt"
"net/url"
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
"time"
"github.com/robfig/cron/v3"
)
var (
separator string
TimeLocationShanghai, _ = time.LoadLocation("Asia/Shanghai")
)
func init() {
if os.IsPathSeparator('\\') { //前边的判断是否是系统的分隔符
separator = "\\"
} else {
separator = "/"
}
}
// PathSeparator 返回系统路径分隔符
func PathSeparator() string {
return separator
}
// PathJoin 判断系统路径分隔符并返回对应的路径字符串拼接结果
func PathJoin(path ...string) string {
return strings.Join(path, separator)
}
// GetCurPath 获取当前文件执行的路径
func GetCurPath() string {
file, _ := exec.LookPath(os.Args[0])
path, _ := filepath.Abs(file)
rst := filepath.Dir(path)
return rst
}
// ObjectToURLValues 把json结构体转换为url.values
func ObjectToURLValues(p interface{}) url.Values {
t := reflect.TypeOf(p)
v := reflect.ValueOf(p)
params := url.Values{}
if v.Kind() != reflect.Struct {
return params
}
params = getParams(t, v)
return params
}
func getParams(t reflect.Type, v reflect.Value) url.Values {
params := url.Values{}
if v.Kind() != reflect.Struct {
return params
}
for i := 0; i < v.NumField(); i++ {
f := t.Field(i)
if f.Type.Kind() == reflect.Struct {
tParams := getParams(f.Type, v.Field(i))
for kk, vv := range tParams {
params.Set(kk, vv[0])
}
} else {
name, ok := f.Tag.Lookup("form")
if !ok {
continue
}
vv := v.FieldByName(f.Name).String()
if vv == "" {
continue
}
params.Set(name, vv)
}
}
return params
}
type Time struct {
time.Time
}
func (t Time) MarshalText() ([]byte, error) {
return []byte(t.Format("2006-01-02 15:04:05")), nil
}
func (t *Time) UnmarshalText(str []byte) error {
tt, err := time.Parse("2006-01-02 15:04:05", string(str))
if err != nil {
return err
}
*t = Time{tt}
return nil
}
func (t Time) String() string {
return t.Format("2006-01-02 15:04:05")
}
func Now() Time {
return Time{time.Now().Local().In(TimeLocationShanghai)}
}
// 字节的单位转换 保留两位小数
func FormatSize(fileSize int64) (size string) {
if fileSize < 1024 {
//return strconv.FormatInt(fileSize, 10) + "B"
return fmt.Sprintf("%.2fB", float64(fileSize)/float64(1))
} else if fileSize < (1024 * 1024) {
return fmt.Sprintf("%.2fKB", float64(fileSize)/float64(1024))
} else if fileSize < (1024 * 1024 * 1024) {
return fmt.Sprintf("%.2fMB", float64(fileSize)/float64(1024*1024))
} else if fileSize < (1024 * 1024 * 1024 * 1024) {
return fmt.Sprintf("%.2fGB", float64(fileSize)/float64(1024*1024*1024))
} else if fileSize < (1024 * 1024 * 1024 * 1024 * 1024) {
return fmt.Sprintf("%.2fTB", float64(fileSize)/float64(1024*1024*1024*1024))
} else { //if fileSize < (1024 * 1024 * 1024 * 1024 * 1024 * 1024)
return fmt.Sprintf("%.2fEB", float64(fileSize)/float64(1024*1024*1024*1024*1024))
}
}
type Expireable interface {
Key() string
IsExpired() bool
}
type ExpireableStorage struct {
data map[string]Expireable
crontab *cron.Cron
}
func (s *ExpireableStorage) Add(delta Expireable) {
s.data[delta.Key()] = delta
}
func (s *ExpireableStorage) Del(delta Expireable) {
delete(s.data, delta.Key())
}
func (s *ExpireableStorage) Get(key string) (Expireable, error) {
if v, ok := s.data[key]; ok {
return v, nil
}
return nil, fmt.Errorf("invalid key")
}
func (s *ExpireableStorage) Destroy() {
for k := range s.data {
delete(s.data, k)
}
ctx := s.crontab.Stop()
<-ctx.Done()
s = nil
}
func (s *ExpireableStorage) Run() {
count := 0
for k, v := range s.data {
if v.IsExpired() {
delete(s.data, k)
count++
}
}
}
func NewExpireableStorage(spec string) (*ExpireableStorage, error) {
es := &ExpireableStorage{
data: make(map[string]Expireable),
crontab: cron.New(cron.WithLocation(TimeLocationShanghai)),
}
_, err := cron.ParseStandard(spec)
if err != nil {
return nil, err
}
_, err = es.crontab.AddJob(spec, es)
if err != nil {
return nil, err
}
es.crontab.Start()
return es, nil
}