forked from libsgh/chrome_updater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
324 lines (299 loc) · 6.45 KB
/
common.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package main
import (
"crypto/sha1"
"encoding/hex"
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/dialog"
"github.com/bodgit/sevenzip"
"io"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strconv"
"strings"
"syscall"
)
// url转换
func parseURL(urlStr string) *url.URL {
link, err := url.Parse(urlStr)
if err != nil {
fyne.LogError("Could not parse URL", err)
}
return link
}
// 路径是否合法
func isValidPath(path string) bool {
absPath, err := filepath.Abs(path)
if err != nil {
return false
}
cleanAbsPath := filepath.Clean(absPath)
cleanPath := filepath.Clean(path)
return cleanAbsPath == cleanPath && dirExist(path)
}
// 字符串是否是数字
func isNumeric(str string) bool {
_, err := strconv.ParseFloat(str, 64)
return err == nil
}
// 格式化文件大小
func formatFileSize(size int64) string {
const (
KB = 1 << 10
MB = 1 << 20
GB = 1 << 30
)
switch {
case size >= GB:
return fmt.Sprintf("%.2f GB", float64(size)/GB)
case size >= MB:
return fmt.Sprintf("%.2f MB", float64(size)/MB)
case size >= KB:
return fmt.Sprintf("%.2f KB", float64(size)/KB)
default:
return fmt.Sprintf("%d Bytes", size)
}
}
// 获取文件大小
func getFileSize(url string) (int64, error) {
resp, err := http.Head(url)
if err != nil {
return 0, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return 0, fmt.Errorf("服务器返回错误: %v", resp.Status)
}
return resp.ContentLength, nil
}
// 下载文件块
func downloadChunk(url string, start, end int64) (string, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", start, end))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusPartialContent {
return "", fmt.Errorf("服务器不支持分块下载:%v", resp.Status)
}
// 创建一个临时文件用于保存下载的文件块
chunkPath := fmt.Sprintf("chunk_%d_%d.tmp", start, end)
chunkFile, err := os.Create(chunkPath)
if err != nil {
return "", err
}
defer chunkFile.Close()
_, err = io.Copy(chunkFile, resp.Body)
if err != nil {
return "", err
}
return chunkPath, nil
}
// 合并文件块
func mergeChunk(chunkPath string, output *os.File) error {
chunkFile, err := os.Open(chunkPath)
if err != nil {
return err
}
defer chunkFile.Close()
_, err = io.Copy(output, chunkFile)
if err != nil {
return err
}
return nil
}
// 获取文件名
func getFileName(fileURL string) string {
parsedURL, err := url.Parse(fileURL)
if err != nil {
fmt.Println("Failed to parse URL:", err)
return ""
}
filename := path.Base(parsedURL.Path)
return filename
}
// 7z解压缩
func UnCompress7z(filePath, targetDir string) {
r, err := sevenzip.OpenReader(filePath)
if err != nil {
log.Fatal(err)
}
defer r.Close()
for _, file := range r.File {
rc, err := file.Open()
if err != nil {
log.Fatal(err)
}
defer rc.Close()
fp := path.Join(targetDir, file.Name)
if file.FileInfo().IsDir() {
os.MkdirAll(fp, os.ModePerm)
} else {
outputFile, err := os.Create(fp)
if err != nil {
log.Fatal(err)
}
defer outputFile.Close()
_, err = io.Copy(outputFile, rc)
}
}
}
func UnCompress7zFilter(filePath, targetDir, filterName string) {
r, err := sevenzip.OpenReader(filePath)
if err != nil {
log.Fatal(err)
}
defer r.Close()
for _, file := range r.File {
if strings.HasPrefix(file.Name, filterName) {
rc, err := file.Open()
if err != nil {
log.Fatal(err)
}
defer rc.Close()
fp := path.Join(targetDir, file.Name)
if file.FileInfo().IsDir() {
os.MkdirAll(fp, os.ModePerm)
} else {
outputFile, err := os.Create(fp)
if err != nil {
log.Fatal(err)
}
defer outputFile.Close()
_, err = io.Copy(outputFile, rc)
}
}
}
}
// 计算文件SHA1
func sumFileSHA1(filePath string) string {
file, err := os.Open(filePath)
if err != nil {
fmt.Println("无法打开文件:", err)
return ""
}
defer file.Close()
hash := sha1.New()
_, err = io.Copy(hash, file)
if err != nil {
fmt.Println("读取文件错误:", err)
return ""
}
hashValue := hash.Sum(nil)
hashString := hex.EncodeToString(hashValue)
return strings.ToUpper(hashString)
}
// 检查文件是否存在
func fileExist(filePath string) bool {
_, err := os.Stat(filePath)
if err != nil {
if os.IsNotExist(err) {
return false
} else {
fmt.Println("发生错误:", err)
}
return false
}
return true
}
func getString(v binding.String) string {
gv, _ := v.Get()
return gv
}
func getBool(v binding.Bool) bool {
gv, _ := v.Get()
return gv
}
func getStringList(v binding.StringList) []string {
gv, _ := v.Get()
return gv
}
func isProcessExist(appName string) bool {
appary := make(map[string]int)
cmd := exec.Command("cmd", "/C", "tasklist")
cmd.SysProcAttr = &syscall.SysProcAttr{
HideWindow: true,
}
output, _ := cmd.Output()
n := strings.Index(string(output), "System")
if n == -1 {
fmt.Println("no find")
os.Exit(1)
}
data := string(output)[n:]
fields := strings.Fields(data)
for k, v := range fields {
if v == appName {
appary[appName], _ = strconv.Atoi(fields[k+1])
return true
}
}
return false
}
func alertInfo(message string, win fyne.Window) {
cnf := dialog.NewInformation("提示", message, win)
cnf.SetDismissText("关闭")
cnf.Show()
}
func alertConfirm(message string, callback func(bool), win fyne.Window) {
cnf := dialog.NewConfirm("确认", message, func(b bool) {
callback(b)
}, win)
cnf.SetDismissText("关闭")
cnf.SetConfirmText("确认")
cnf.Show()
}
func dirExist(dir string) bool {
// 打开当前目录
dirHandle, err := os.Open(dir)
if err != nil {
return false
}
defer dirHandle.Close()
return true
}
// 获取系统信息
func getInfo() SysInfo {
goarch := "x64"
goos := "win"
if runtime.GOARCH == "amd64" {
goarch = "x64"
} else if runtime.GOARCH == "386" {
goarch = "x86"
} else if runtime.GOARCH == "arm64" {
goarch = "arm64"
}
if runtime.GOOS == "darwin" {
goos = "mac"
} else if runtime.GOOS == "windows" {
goos = "win"
}
return SysInfo{goarch, goos}
}
func getMapKeys(m map[string]GithubRelease) []string {
keys := make([]string, len(m))
i := 0
for key := range m {
keys[i] = key
i++
}
return keys
}
func pathJoin(baseURL, subPath string) string {
if baseURL == "" {
return subPath
}
return strings.TrimSuffix(baseURL, "/") + "/" + subPath
}