Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
energye
energy
提交
8b57bd36
energy
项目概览
energye
/
energy
通知
13
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
energy
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
8b57bd36
编写于
6月 06, 2023
作者:
yanghye
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
up: check update demo
上级
c857df98
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
205 addition
and
22 deletion
+205
-22
example/browser-lib-checkupdate/main.go
example/browser-lib-checkupdate/main.go
+195
-20
example/browser-lib-checkupdate/resources/locale.en-US.json
example/browser-lib-checkupdate/resources/locale.en-US.json
+5
-1
example/browser-lib-checkupdate/resources/locale.zh-CN.json
example/browser-lib-checkupdate/resources/locale.zh-CN.json
+5
-1
未找到文件。
example/browser-lib-checkupdate/main.go
浏览文件 @
8b57bd36
...
...
@@ -3,6 +3,7 @@
package
main
import
(
"archive/zip"
"embed"
"fmt"
"github.com/energye/energy/v2/cef/autoupdate"
...
...
@@ -20,6 +21,7 @@ import (
"github.com/energye/golcl/pkgs/libname"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
"runtime"
...
...
@@ -53,15 +55,9 @@ func main() {
os
.
Mkdir
(
energyPath
,
fs
.
ModePerm
)
}
var
(
libPath
string
libPath
=
libname
.
LibPath
()
dstLibPath
=
filepath
.
Join
(
energyPath
,
libname
.
GetDLLName
())
)
if
runtime
.
GOOS
==
"darwin"
{
//MacOSX从Frameworks加载
libPath
=
"@executable_path/../Frameworks/"
+
libname
.
GetDLLName
()
}
else
{
libPath
=
libname
.
LibPath
()
}
if
tools
.
IsExist
(
dstLibPath
)
{
os
.
Remove
(
dstLibPath
)
}
...
...
@@ -241,19 +237,30 @@ func main() {
updateContentMemo
.
Lines
()
.
Add
(
downUrl
)
// 取消按钮
cancelBtn
:=
lcl
.
NewImageButton
(
m
)
cancelBtn
.
SetParent
(
m
)
cancelBtn
.
SetImageCount
(
3
)
cancelBtn
.
SetAutoSize
(
true
)
cancelBtn
.
SetCursor
(
types
.
CrHandPoint
)
cancelBtn
.
Picture
()
.
LoadFromFSFile
(
"resources/btn-cancel.png"
)
cancelBtn
.
SetLeft
(
300
)
cancelBtn
.
SetTop
(
290
)
cancelBtn
.
SetHint
(
i18n
.
Resource
(
"cancel"
))
cancelBtn
.
SetOnClick
(
func
(
lcl
.
IObject
)
{
//m.Close()
})
//
cancelBtn := lcl.NewImageButton(m)
//
cancelBtn.SetParent(m)
//
cancelBtn.SetImageCount(3)
//
cancelBtn.SetAutoSize(true)
//
cancelBtn.SetCursor(types.CrHandPoint)
//
cancelBtn.Picture().LoadFromFSFile("resources/btn-cancel.png")
//
cancelBtn.SetLeft(300)
//
cancelBtn.SetTop(290)
//
cancelBtn.SetHint(i18n.Resource("cancel"))
//
cancelBtn.SetOnClick(func(lcl.IObject) {
//
//m.Close()
//
})
// 下载
var
isNotDownload
=
true
// 更新下载进度条
updateProgressBar
:=
lcl
.
NewProgressBar
(
m
)
updateProgressBar
.
SetParent
(
m
)
updateProgressBar
.
SetTop
(
275
)
updateProgressBar
.
SetLeft
(
updateContentMemo
.
Left
())
updateProgressBar
.
SetMin
(
0
)
updateProgressBar
.
SetMax
(
100
)
updateProgressBar
.
SetWidth
(
updateContentMemo
.
Width
())
updateProgressBar
.
SetVisible
(
false
)
// 更新按钮
updateBtn
:=
lcl
.
NewImageButton
(
m
)
updateBtn
.
SetParent
(
m
)
...
...
@@ -267,7 +274,51 @@ func main() {
updateBtn
.
SetTop
(
290
)
updateBtn
.
SetHint
(
i18n
.
Resource
(
"update"
))
updateBtn
.
SetOnClick
(
func
(
lcl
.
IObject
)
{
fmt
.
Println
(
"update"
)
if
isNotDownload
{
updateProgressBar
.
SetPosition
(
0
)
updateProgressBar
.
SetVisible
(
isNotDownload
)
isNotDownload
=
false
updateBtn
.
SetEnabled
(
isNotDownload
)
var
savePath
,
_
=
filepath
.
Split
(
libPath
)
var
fileName
,
_
=
energyLiblcl
()
var
saveFilePath
=
filepath
.
Join
(
savePath
,
fileName
)
+
".zip"
// zip
updateContentMemo
.
Lines
()
.
Add
(
i18n
.
Resource
(
"beginDownload"
))
updateContentMemo
.
Lines
()
.
Add
(
" "
+
i18n
.
Resource
(
"downloadURL"
)
+
": "
+
downUrl
)
updateContentMemo
.
Lines
()
.
Add
(
" "
+
i18n
.
Resource
(
"savePath"
)
+
": "
+
saveFilePath
)
go
func
()
{
// 下载地址, 保存目录, 回调更新进程函数
downloadFile
(
downUrl
,
saveFilePath
,
func
(
totalLength
,
processLength
int64
)
{
var
process
=
int32
((
float64
(
processLength
)
/
float64
(
totalLength
))
*
100
)
lcl
.
ThreadSync
(
func
()
{
updateProgressBar
.
SetPosition
(
process
)
if
process
>=
100
{
isNotDownload
=
true
updateBtn
.
SetEnabled
(
isNotDownload
)
updateContentMemo
.
Lines
()
.
Add
(
i18n
.
Resource
(
"endDownload"
))
}
})
})
updateContentMemo
.
Lines
()
.
Add
(
i18n
.
Resource
(
"extractUnZip"
)
+
": "
+
filepath
.
Join
(
savePath
,
libname
.
GetDLLName
()))
extractUnZip
(
saveFilePath
,
savePath
,
func
(
totalLength
,
processLength
int64
)
{
var
process
=
int32
((
float64
(
processLength
)
/
float64
(
totalLength
))
*
100
)
lcl
.
ThreadSync
(
func
()
{
//updateProgressBar.SetPosition(process)
//if process >= 100 {
// isNotDownload = true
// updateBtn.SetEnabled(isNotDownload)
// updateContentMemo.Lines().Add(i18n.Resource("endDownload"))
//}
if
process
>=
100
{
updateContentMemo
.
Lines
()
.
Add
(
i18n
.
Resource
(
"extractUnZip"
)
+
" success"
)
}
})
//bar.PrintBar(int((float64(processLength) / float64(totalLength)) * 100))
},
libname
.
GetDLLName
())
}()
}
})
// 下载进度
...
...
@@ -278,3 +329,127 @@ func main() {
// 检查 liblcl 库是否有更新
autoupdate
.
CheckUpdate
()
}
// 下载文件
func
downloadFile
(
url
string
,
localPath
string
,
callback
func
(
totalLength
,
processLength
int64
))
error
{
var
(
fsize
int64
buf
=
make
([]
byte
,
1024
*
10
)
written
int64
)
tmpFilePath
:=
localPath
+
".download"
client
:=
new
(
http
.
Client
)
resp
,
err
:=
client
.
Get
(
url
)
if
err
!=
nil
{
fmt
.
Printf
(
"download-error=[%v]
\n
"
,
err
)
os
.
Exit
(
1
)
return
err
}
fsize
,
err
=
strconv
.
ParseInt
(
resp
.
Header
.
Get
(
"Content-Length"
),
10
,
32
)
if
err
!=
nil
{
fmt
.
Printf
(
"download-error=[%v]
\n
"
,
err
)
os
.
Exit
(
1
)
return
err
}
println
(
"Save path: ["
,
localPath
,
"] file size:"
,
fsize
)
file
,
err
:=
os
.
Create
(
tmpFilePath
)
if
err
!=
nil
{
fmt
.
Printf
(
"download-error=[%v]
\n
"
,
err
)
os
.
Exit
(
1
)
return
err
}
defer
file
.
Close
()
if
resp
.
Body
==
nil
{
fmt
.
Printf
(
"Download-error=[body is null]
\n
"
)
os
.
Exit
(
1
)
return
nil
}
defer
resp
.
Body
.
Close
()
for
{
nr
,
er
:=
resp
.
Body
.
Read
(
buf
)
if
nr
>
0
{
nw
,
err
:=
file
.
Write
(
buf
[
0
:
nr
])
if
nw
>
0
{
written
+=
int64
(
nw
)
}
callback
(
fsize
,
written
)
if
err
!=
nil
{
break
}
if
nr
!=
nw
{
err
=
io
.
ErrShortWrite
break
}
}
if
er
!=
nil
{
if
er
!=
io
.
EOF
{
err
=
er
}
break
}
}
if
err
==
nil
{
file
.
Sync
()
file
.
Close
()
err
=
os
.
Rename
(
tmpFilePath
,
localPath
)
if
err
!=
nil
{
return
err
}
}
return
err
}
func
writeFile
(
r
io
.
Reader
,
w
*
os
.
File
,
totalLength
int64
,
callback
func
(
totalLength
,
processLength
int64
))
{
buf
:=
make
([]
byte
,
1024
*
10
)
var
written
int64
for
{
nr
,
err
:=
r
.
Read
(
buf
)
if
nr
>
0
{
nw
,
err
:=
w
.
Write
(
buf
[
0
:
nr
])
if
nw
>
0
{
written
+=
int64
(
nw
)
}
callback
(
totalLength
,
written
)
if
err
!=
nil
{
break
}
if
nr
!=
nw
{
err
=
io
.
ErrShortWrite
break
}
}
if
err
!=
nil
{
break
}
}
}
func
extractUnZip
(
filePath
,
targetPath
string
,
callback
func
(
totalLength
,
processLength
int64
),
files
...
interface
{})
{
if
rc
,
err
:=
zip
.
OpenReader
(
filePath
);
err
==
nil
{
defer
rc
.
Close
()
for
i
:=
0
;
i
<
len
(
files
);
i
++
{
if
f
,
err
:=
rc
.
Open
(
files
[
i
]
.
(
string
));
err
==
nil
{
defer
f
.
Close
()
st
,
_
:=
f
.
Stat
()
targetFileName
:=
filepath
.
Join
(
targetPath
,
st
.
Name
())
if
st
.
IsDir
()
{
os
.
MkdirAll
(
targetFileName
,
st
.
Mode
())
continue
}
if
targetFile
,
err
:=
os
.
Create
(
targetFileName
);
err
==
nil
{
writeFile
(
f
,
targetFile
,
st
.
Size
(),
callback
)
targetFile
.
Close
()
}
}
else
{
fmt
.
Printf
(
"error: cannot open file, error=[%v]
\n
"
,
err
)
os
.
Exit
(
1
)
return
}
}
}
else
{
if
err
!=
nil
{
fmt
.
Printf
(
"error: cannot read zip file, error=[%v]
\n
"
,
err
)
os
.
Exit
(
1
)
}
}
}
example/browser-lib-checkupdate/resources/locale.en-US.json
浏览文件 @
8b57bd36
...
...
@@ -4,5 +4,9 @@
"cancel"
:
"cancel"
,
"update"
:
"update"
,
"updateContent"
:
"Update Content"
,
"downloadURL"
:
"Update URL"
"downloadURL"
:
"Update URL"
,
"beginDownload"
:
"begin download"
,
"endDownload"
:
"end download"
,
"savePath"
:
"save path"
,
"extractUnZip"
:
"extract UNZIP"
}
\ No newline at end of file
example/browser-lib-checkupdate/resources/locale.zh-CN.json
浏览文件 @
8b57bd36
...
...
@@ -4,5 +4,9 @@
"cancel"
:
"取消"
,
"update"
:
"更新"
,
"updateContent"
:
"更新内容"
,
"downloadURL"
:
"更新地址"
"downloadURL"
:
"更新地址"
,
"beginDownload"
:
"开始下载"
,
"endDownload"
:
"下载完成"
,
"savePath"
:
"保存目录"
,
"extractUnZip"
:
"释放 UNZIP"
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录