-
Notifications
You must be signed in to change notification settings - Fork 2
/
tools.go
150 lines (148 loc) · 2.75 KB
/
tools.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
package faithdroid
import (
"crypto/md5"
"encoding/json"
"fmt"
"io"
"math/rand"
"net/http"
"os"
"strconv"
"strings"
"time"
)
func NewNumToken() string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return strconv.FormatInt(int64(r.Intn(60000)), 10)
}
func NewToken() string {
ct := time.Now().UnixNano()
h := md5.New()
io.WriteString(h, strconv.FormatInt(ct, 10))
token := fmt.Sprintf("%x", h.Sum(nil))
return token
}
func JsonArray(i interface{}) string {
b, e := json.Marshal(i)
if e != nil {
return "[]"
}
return string(b)
}
func JsonObject(i interface{}) string {
b, e := json.Marshal(i)
if e != nil {
return "{}"
}
return string(b)
}
func UnJson(str string, v interface{}) {
e := json.Unmarshal([]byte(str), v)
if e != nil {
fmt.Println("UnJson() err:", e)
}
}
func Getrpath(s string) string {
if len(s) < 1 {
return ""
}
sp := string(os.PathSeparator)
if s[len(s)-1:] == sp {
return s
}
return s + sp
}
func DownloadNetFile(url, downloadDir string, callback func(string)) {
f := Getrpath(downloadDir) + NewToken()
e := DownloadFile(url, f)
if e != nil {
fmt.Println(e)
return
}
callback(f)
}
func CacheNetFile(url, cacheDir string, callback func(string)) {
f := Getrpath(cacheDir) + Url2cachePath(url)
if _, e := os.Stat(f); e != nil {
e = DownloadFile(url, f)
if e != nil {
fmt.Println(e)
return
}
}
callback(f)
}
func EndsWith(s, suffix string) bool {
if len(suffix) > len(s) {
return false
}
if s[len(s)-len(suffix):] == suffix {
return true
}
return false
}
func Url2cachePath(url string) string {
rUrl := GetRealUrl(url)
s := strings.Replace(rUrl, "://", "/", -1)
if EndsWith(s, "/") {
return s[:len(s)-1]
}
return s
}
func GetRealUrl(url string) string {
for i := 0; i < len(url); i++ {
item := url[i : i+1]
if item == "?" || item == "#" {
return url[:i]
}
}
return url
}
func SPrintf(a ...interface{}) string {
return fmt.Sprint(a...)
}
func XPrintf(a int) string {
return fmt.Sprintf("%x", a)
}
func a2i(a string) (int, error) {
return strconv.Atoi(a)
}
func a2f(a string) float64 {
f, e := strconv.ParseFloat(a, 32)
if e != nil {
fmt.Println(e)
return 0
}
return float64(f)
}
func DownloadFile(url, fdist string) error {
rp, e := http.Get(url)
if e != nil {
return e
}
defer rp.Body.Close()
f, e := WriteFile(fdist)
if e != nil {
return e
}
defer f.Close()
_, e = io.Copy(f, rp.Body)
return e
}
func WriteFile(f string) (*os.File, error) {
e := os.MkdirAll(GetDirOfFile(f), 0755)
if e != nil {
fmt.Println(e)
return nil, e
}
file, e := os.OpenFile(f, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
return file, e
}
func GetDirOfFile(f string) string {
for i := len(f) - 1; i > -1; i-- {
if f[i:i+1] == string(os.PathSeparator) {
return f[:i]
}
}
return f
}