forked from libsgh/chrome_updater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
317 lines (307 loc) · 9.36 KB
/
base.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
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
jsoniter "github.com/json-iterator/go"
"io"
"log"
"net/http"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"sync"
)
func baseScreen(win fyne.Window, data *SettingsData) fyne.CanvasObject {
// 获取系统信息
sysInfo := getInfo()
installPathHandle(data)
folderEntry := widget.NewEntryWithData(data.installPath)
folderEntry.OnChanged = func(path string) {
installPathHandle(data)
}
showFolderPicker := func() {
onChosen := func(f fyne.ListableURI, err error) {
if err != nil {
fmt.Println(err)
return
}
if f == nil {
return
}
_ = data.installPath.Set(f.Path())
}
dialog.ShowFolderOpen(onChosen, win)
}
folderBtn := widget.NewButtonWithIcon("", theme.FolderOpenIcon(), showFolderPicker)
data.folderEntryStatus.AddListener(binding.NewDataListener(func() {
if b, _ := data.folderEntryStatus.Get(); b {
folderEntry.Disable()
folderBtn.Disable()
} else {
folderEntry.Enable()
folderBtn.Enable()
}
}))
checkBtn := widget.NewButtonWithIcon(LoadString("CheckBtnLabel"), theme.SearchIcon(), func() {
chromeInfo := getLocalChromeInfo(getVk(data.branch, sysInfo))
data.curVer.Set(chromeInfo.Version)
data.fileSize.Set(formatFileSize(chromeInfo.Size))
data.urlList.Set(chromeInfo.Urls)
data.SHA1.Set(chromeInfo.Sha1)
data.SHA256.Set(chromeInfo.Sha256)
data.downBtnStatus.Set(false)
})
downloadBtn := widget.NewButtonWithIcon(LoadString("InstallBtnLabel"), theme.DownloadIcon(), func() {
ov, _ := data.oldVer.Get()
cv, _ := data.curVer.Get()
if cv == ov {
alertInfo(LoadString("NoNeedUpdateMsg"), win)
} else {
chromeInUse := isProcessExist("chrome.exe")
if chromeInUse {
alertInfo(LoadString("ChromeRunningMsg"), win)
} else {
if getString(data.oldVer) == "-" {
alertConfirm(LoadString("FirstInstallMsg"), func(b bool) {
execDownAndUnzip(data, downloadProgress, 0)
}, win)
} else {
execDownAndUnzip(data, downloadProgress, 1)
}
}
}
})
data.downBtnStatus.AddListener(binding.NewDataListener(func() {
if b, _ := data.downBtnStatus.Get(); b {
downloadBtn.Disable()
} else {
downloadBtn.Enable()
}
}))
data.checkBtnStatus.AddListener(binding.NewDataListener(func() {
if b, _ := data.checkBtnStatus.Get(); b {
checkBtn.Disable()
} else {
checkBtn.Enable()
}
}))
versionRadio := widget.NewRadioGroup(LoadStringList("StableVerOption", "BetaVerOption", "DevVerOption", "CanaryVerOption"), func(value string) {
if value == LoadString("StableVerOption") {
data.branch.Set("stable")
} else if value == LoadString("BetaVerOption") {
data.branch.Set("beta")
} else if value == LoadString("DevVerOption") {
data.branch.Set("dev")
} else if value == LoadString("CanaryVerOption") {
data.branch.Set("canary")
} else {
data.branch.Set("stable")
}
})
versionRadio.Horizontal = true
if getString(data.branch) == "stable" {
versionRadio.Selected = LoadString("StableVerOption")
} else if getString(data.branch) == "beta" {
versionRadio.Selected = LoadString("BetaVerOption")
} else if getString(data.branch) == "dev" {
versionRadio.Selected = LoadString("DevVerOption")
} else if getString(data.branch) == "canary" {
versionRadio.Selected = LoadString("CanaryVerOption")
} else {
versionRadio.Selected = LoadString("StableVerOption")
}
urlsRadio := widget.NewRadioGroup([]string{
"edgedl.me.gvt1",
"dl.google.com",
"www.google.com"}, func(value string) {
if value == "" {
data.urlKey.Set("edgedl.me.gvt1")
} else {
data.urlKey.Set(value)
}
})
urlsRadio.Selected = getString(data.urlKey)
urlsRadio.Horizontal = true
buttons := container.NewHBox(folderBtn)
bar := container.NewBorder(nil, nil, buttons, nil, folderEntry)
curVerLabel := widget.NewLabelWithData(data.curVer)
curVerLabel.TextStyle.Bold = true
form := widget.NewForm(
&widget.FormItem{Text: LoadString("InstallLabel"), Widget: bar},
&widget.FormItem{Text: LoadString("BranchLabel"), Widget: versionRadio},
&widget.FormItem{Text: LoadString("NowVerLabel"), Widget: widget.NewLabelWithData(data.oldVer)},
&widget.FormItem{Text: LoadString("LatestVerLabel"), Widget: curVerLabel},
&widget.FormItem{Text: LoadString("FileSizeLabel"), Widget: widget.NewLabelWithData(data.fileSize)},
&widget.FormItem{Text: "SHA1", Widget: widget.NewLabelWithData(data.SHA1)},
&widget.FormItem{Text: "SHA256", Widget: widget.NewLabelWithData(data.SHA256)},
&widget.FormItem{Text: LoadString("DownLoadChannelLabel"), Widget: urlsRadio},
)
downloadProgress = widget.NewProgressBar()
downloadProgress.TextFormatter = func() string {
fs, _ := data.fileSize.Get()
if downloadProgress.Max*0.9 == downloadProgress.Value {
return fmt.Sprintf(LoadString("DownLoadedProcessMsg"), fs)
} else if downloadProgress.Max == downloadProgress.Value {
return LoadString("InstalledMsg")
} else if downloadProgress.Value == -1 {
return LoadString("DownloadFailedMsg")
} else if downloadProgress.Value == 0.95 {
return LoadString("Download95Msg")
}
fsFloatStr := strings.Split(fs, " ")[0]
fsFloat, err := strconv.ParseFloat(fsFloatStr, 64)
if err != nil {
return LoadString("DownloadNotStartedMsg")
}
return fmt.Sprintf(LoadString("DownloadingMsg"), fsFloat*downloadProgress.Value, fs)
}
data.processStatus.AddListener(binding.NewDataListener(func() {
if b, _ := data.processStatus.Get(); b {
downloadProgress.Show()
} else {
downloadProgress.Hide()
}
}))
return container.New(&buttonLayout{}, form, container.NewVBox(downloadProgress, container.NewGridWithColumns(2, checkBtn, downloadBtn)))
}
func execDownAndUnzip(data *SettingsData, downloadProgress *widget.ProgressBar, installType int) {
data.checkBtnStatus.Set(true)
data.folderEntryStatus.Set(true)
data.processStatus.Set(true)
if installType == 0 {
initInstallDirs(data)
}
url := getDownloadUrl(data.urlList, data.urlKey)
parentPath, _ := data.installPath.Get()
downloadProgress.SetValue(0)
fileName := getFileName(url)
fileName = filepath.Join(parentPath, fileName)
fileSize, _ := getFileSize(url)
var wg = &sync.WaitGroup{}
GoroutineDownload(url, fileName, 4, 5*1024*1024, 30, fileSize, downloadProgress, wg)
sha1 := sumFileSHA1(fileName)
if v, _ := data.SHA1.Get(); v != sha1 {
downloadProgress.SetValue(-1)
} else {
downloadProgress.SetValue(0.95)
//解压覆盖
UnCompress7z(fileName, parentPath)
UnCompress7z(path.Join(parentPath, "chrome.7z"), parentPath)
p := path.Join(parentPath, "Chrome-bin")
targetDir := filepath.Dir(p)
files, _ := os.ReadDir(p)
for _, f := range files {
os.Rename(filepath.Join(p, f.Name()), path.Join(targetDir, f.Name()))
}
//清理文件
_ = os.Remove(p)
_ = os.Remove(filepath.Join(parentPath, "chrome.7z"))
if !getBool(data.remainInstallFileSettings) {
os.Remove(fileName)
}
if !getBool(data.remainHistoryFileSettings) {
os.RemoveAll(filepath.Join(parentPath, getString(data.oldVer)))
}
downloadProgress.SetValue(1)
data.oldVer.Set(getString(data.curVer))
defer data.checkBtnStatus.Set(false)
defer data.folderEntryStatus.Set(false)
}
}
// 初始化安装目录
func initInstallDirs(data *SettingsData) {
//创建App、Cache、Data目录
os.Mkdir(filepath.Join(getString(data.installPath), "App"), os.ModePerm)
os.Mkdir(filepath.Join(getString(data.installPath), "Cache"), os.ModePerm)
os.Mkdir(filepath.Join(getString(data.installPath), "Data"), os.ModePerm)
//更改安装目录
data.installPath.Set(filepath.Join(getString(data.installPath), "App"))
}
var (
downloadProgress *widget.ProgressBar
)
// 获取下载地址
func getDownloadUrl(list binding.StringList, urlKey binding.String) string {
urls, _ := list.Get()
uk, _ := urlKey.Get()
for _, url := range urls {
if strings.HasPrefix(url, "https://"+uk) {
return url
}
}
return urls[0]
}
// 获取版本分支KEY
func getVk(branch binding.String, sysInfo SysInfo) string {
b, _ := branch.Get()
return sysInfo.goos + "_" + b + "_" + sysInfo.goarch
}
// 获取Chrome版本信息
func getChromeInfo(key string) ChromeInfo {
// 发送 HTTP 请求获取 JSON 数据
response, err := http.Get("https://chrome.noki.eu.org/api/c/info")
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
data, err := io.ReadAll(response.Body)
chromeInfo := ChromeInfo{}
// 解码 JSON 数据到结构体
infoStr := jsoniter.Get(data, key).ToString()
jsoniter.UnmarshalFromString(infoStr, &chromeInfo)
if err != nil {
log.Fatal(err)
}
return chromeInfo
}
// 处理Chrome安装路径
func installPathHandle(data *SettingsData) {
//读取当前程序所在目录
p, _ := data.installPath.Get()
dir, err := os.Getwd()
if isValidPath(p) {
dir = p
} else {
data.installPath.Set(dir)
}
if err != nil {
log.Fatal(err)
}
// 打开当前目录
dirHandle, err := os.Open(dir)
if err != nil {
log.Fatal()
}
defer dirHandle.Close()
fileInfos, err := dirHandle.Readdir(-1)
if err != nil {
log.Fatal(err)
}
result := false
v := ""
for _, fileInfo := range fileInfos {
name := fileInfo.Name()
if name == "chrome.exe" {
result = true
}
if fileInfo.IsDir() && isNumeric(strings.ReplaceAll(name, ".", "")) {
v = fileInfo.Name()
}
}
if result {
data.installPath.Set(dir)
data.oldVer.Set(v)
} else {
data.oldVer.Set("-")
}
if getBool(data.downBtnStatus) {
data.checkBtnStatus.Set(false)
}
}