-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
199 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package utils | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/gogf/gf/v2/errors/gerror" | ||
"github.com/gogf/gf/v2/os/glog" | ||
"io" | ||
"net/http" | ||
"os" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
// HTTPDownloadFileWithPercent downloads target url file to local path with percent process printing. | ||
func HTTPDownloadFileWithPercent(url string, localSaveFilePath string) error { | ||
start := time.Now() | ||
out, err := os.Create(localSaveFilePath) | ||
if err != nil { | ||
return gerror.Wrapf(err, `download "%s" to "%s" failed`, url, localSaveFilePath) | ||
} | ||
defer func(out *os.File) { | ||
err := out.Close() | ||
if err != nil { | ||
glog.Warning(context.Background(), err) | ||
} | ||
}(out) | ||
|
||
headResp, err := http.Head(url) | ||
if err != nil { | ||
return gerror.Wrapf(err, `download "%s" to "%s" failed`, url, localSaveFilePath) | ||
} | ||
defer func(Body io.ReadCloser) { | ||
err := Body.Close() | ||
if err != nil { | ||
glog.Warning(context.Background(), err) | ||
} | ||
}(headResp.Body) | ||
|
||
size, err := strconv.Atoi(headResp.Header.Get("Content-Length")) | ||
if err != nil { | ||
return gerror.Wrap(err, "retrieve Content-Length failed") | ||
} | ||
doneCh := make(chan int64) | ||
|
||
go doPrintDownloadPercent(doneCh, localSaveFilePath, int64(size)) | ||
|
||
resp, err := http.Get(url) | ||
if err != nil { | ||
return gerror.Wrapf(err, `download "%s" to "%s" failed`, url, localSaveFilePath) | ||
} | ||
defer func(Body io.ReadCloser) { | ||
err := Body.Close() | ||
if err != nil { | ||
glog.Warning(context.Background(), err) | ||
} | ||
}(resp.Body) | ||
|
||
wroteBytesCount, err := io.Copy(out, resp.Body) | ||
if err != nil { | ||
return gerror.Wrapf(err, `download "%s" to "%s" failed`, url, localSaveFilePath) | ||
} | ||
|
||
doneCh <- wroteBytesCount | ||
elapsed := time.Since(start) | ||
if elapsed > time.Minute { | ||
glog.Printf(context.Background(), `download completed in %.0fm`, float64(elapsed)/float64(time.Minute)) | ||
} else { | ||
glog.Printf(context.Background(), `download completed in %.0fs`, elapsed.Seconds()) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func doPrintDownloadPercent(doneCh chan int64, localSaveFilePath string, total int64) { | ||
var ( | ||
stop = false | ||
lastPercentFmt string | ||
) | ||
for { | ||
select { | ||
case <-doneCh: | ||
stop = true | ||
|
||
default: | ||
file, err := os.Open(localSaveFilePath) | ||
if err != nil { | ||
glog.Fatal(context.Background(), err) | ||
time.Sleep(5 * time.Second) | ||
} | ||
fi, err := file.Stat() | ||
if err != nil { | ||
glog.Fatal(context.Background(), err) | ||
time.Sleep(5 * time.Second) | ||
} | ||
size := fi.Size() | ||
if size == 0 { | ||
size = 1 | ||
} | ||
var ( | ||
percent = float64(size) / float64(total) * 100 | ||
percentFmt = fmt.Sprintf(`%.0f`, percent) + "%" | ||
) | ||
if lastPercentFmt != percentFmt { | ||
lastPercentFmt = percentFmt | ||
glog.Debug(context.Background(), "下载最新二进制文件进度: "+percentFmt) | ||
} | ||
} | ||
|
||
if stop { | ||
break | ||
} | ||
time.Sleep(200 * time.Millisecond) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package utils | ||
|
||
import ( | ||
"context" | ||
"github.com/gogf/gf/v2/encoding/gjson" | ||
"github.com/gogf/gf/v2/frame/g" | ||
"github.com/gogf/gf/v2/net/gclient" | ||
"github.com/gogf/gf/v2/os/glog" | ||
) | ||
|
||
// GetLatestVersionInfo 获取github最新版本 | ||
func GetLatestVersionInfo() (version string, downloadUrl string, downloadStatus bool) { | ||
backendURL := "http://120.24.211.49:10441/GetLatestVersion" | ||
response, err := g.Client().Get(context.TODO(), backendURL) | ||
if err != nil { | ||
glog.Warning(context.TODO(), "请求github最新版本失败,原因:", err.Error()) | ||
return "", "", false | ||
} | ||
defer func(response *gclient.Response) { | ||
err := response.Close() | ||
if err != nil { | ||
glog.Warning(context.TODO(), "关闭response失败,原因:", err.Error()) | ||
} | ||
}(response) | ||
githubResJson, err := gjson.DecodeToJson(response.ReadAllString()) | ||
if err != nil { | ||
glog.Warning(context.TODO(), "解析response失败,原因:", err.Error()) | ||
return "", "", false | ||
} | ||
|
||
// 判断GitHub Release可更新二进制文件是否存在 | ||
if len(githubResJson.Get("data.github_res.assets").Array()) == 0 { | ||
glog.Warning(context.TODO(), "解析response失败,原因:", "github_res.assets为空") | ||
return "", "", false | ||
} | ||
version = githubResJson.Get("data.github_res.tag_name").String() | ||
|
||
// 获取下载文件名是否正确 | ||
downloadFileName := githubResJson.Get("data.github_res.assets.0.name").String() | ||
if downloadFileName != "speed_cron_windows_amd64.exe" { | ||
glog.Warning(context.TODO(), "解析response失败,原因:", "downloadFileName不正确") | ||
return "", "", false | ||
} | ||
|
||
// 获取下载地址 | ||
downloadUrl = githubResJson.Get("data.github_res.assets.0.browser_download_url").String() | ||
if version == "" || downloadUrl == "" { | ||
glog.Warning(context.TODO(), "解析response失败,原因:", "version或downloadUrl为空") | ||
return "", "", false | ||
} | ||
return version, downloadUrl, true | ||
} |