/
utils.go
92 lines (75 loc) · 1.54 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
package utils
import (
"crypto/md5"
"encoding/base64"
"encoding/hex"
"io/ioutil"
"os"
"strings"
"time"
"github.com/google/uuid"
)
func Hmac(data string) string {
h := md5.New()
h.Write([]byte(data))
return hex.EncodeToString(h.Sum(nil))
}
func IsStringEmpty(str string) bool {
return strings.Trim(str, " ") == ""
}
func GetUUID() string {
u := uuid.New()
return strings.ReplaceAll(u.String(), "-", "")
}
func PathExists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return false
}
func Base64ToImage(imageBase64 string) ([]byte, error) {
image, err := base64.StdEncoding.DecodeString(imageBase64)
if err != nil {
return nil, err
}
return image, nil
}
func GetDirFiles(dir string) ([]string, error) {
dirList, err := ioutil.ReadDir(dir)
if err != nil {
return nil, err
}
filesRet := make([]string, 0)
for _, file := range dirList {
if file.IsDir() {
files, err := GetDirFiles(dir + string(os.PathSeparator) + file.Name())
if err != nil {
return nil, err
}
filesRet = append(filesRet, files...)
} else {
filesRet = append(filesRet, dir+string(os.PathSeparator)+file.Name())
}
}
return filesRet, nil
}
func GetCurrentTimeStamp() int64 {
return time.Now().UnixNano() / 1e6
}
//slice去重
func RemoveRepByMap(slc []string) []string {
result := []string{}
tempMap := map[string]byte{}
for _, e := range slc {
l := len(tempMap)
tempMap[e] = 0
if len(tempMap) != l {
result = append(result, e)
}
}
return result
}