E5BotForSQLite/util/util.go

88 lines
1.5 KiB
Go
Raw Normal View History

2021-03-11 12:57:30 +08:00
package util
2020-03-27 11:31:19 +08:00
import (
"crypto/md5"
"encoding/hex"
2020-04-05 21:01:26 +08:00
"io/ioutil"
2020-03-27 18:14:19 +08:00
"net/url"
2020-03-27 11:31:19 +08:00
"os"
2020-04-05 21:01:26 +08:00
"time"
2020-03-27 18:14:19 +08:00
)
2021-06-14 22:12:18 +08:00
// Min true=>no error
2020-04-05 21:01:26 +08:00
func Min(x, y int) int {
if x < y {
return x
}
return y
}
2020-04-03 13:19:53 +08:00
func PathExists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
2020-03-27 11:31:19 +08:00
}
2020-04-03 13:19:53 +08:00
if os.IsNotExist(err) {
return false
}
return false
2020-03-27 11:31:19 +08:00
}
2020-03-27 18:14:19 +08:00
func GetURLValue(Url, key string) string {
u, _ := url.Parse(Url)
query := u.Query()
2020-03-27 18:25:31 +08:00
//fmt.Println(query.Get(key))
return query.Get(key)
2020-03-27 18:14:19 +08:00
}
2020-03-28 10:21:01 +08:00
func GetMD5Encode(data string) string {
h := md5.New()
h.Write([]byte(data))
return hex.EncodeToString(h.Sum(nil))
}
func Get16MD5Encode(data string) string {
return GetMD5Encode(data)[8:24]
}
2020-04-05 21:01:26 +08:00
2021-06-14 22:12:18 +08:00
// GetPathFiles only return file name
2020-04-05 21:01:26 +08:00
func GetPathFiles(path string) []string {
files, _ := ioutil.ReadDir(path)
var t []string
for _, file := range files {
if file.IsDir() {
continue
} else {
t = append(t, file.Name())
}
}
return t
}
func GetRecentLogs(path string, n int) []string {
var paths []string
if !PathExists(path) {
return paths
}
2021-06-18 19:36:13 +08:00
2020-04-05 21:01:26 +08:00
if path[len(path)-1:] != "/" {
path += "/"
}
data := time.Now()
d, _ := time.ParseDuration("-24h")
//不到n个返回所有
nt := Min(n, len(GetPathFiles(path)))
for i := 1; i <= nt; {
if PathExists(path + data.Format("2006-01-02") + ".log") {
2020-04-07 10:57:03 +08:00
paths = append(paths, data.Format("2006-01-02")+".log")
2020-04-05 21:01:26 +08:00
i++
}
data = data.Add(d)
}
return paths
}
2021-06-14 22:12:18 +08:00
func IF(f bool, a interface{}, b interface{}) interface{} {
if f {
return a
}
return b
}