2023-05-20 13:00:08 +08:00
|
|
|
package backend_golang
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/cavaliergopher/grab/v3"
|
|
|
|
"github.com/wailsapp/wails/v2/pkg/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (a *App) DownloadFile(path string, url string) error {
|
2023-06-01 16:54:21 +08:00
|
|
|
_, err := grab.Get(a.exDir+path, url)
|
2023-05-20 13:00:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type DownloadStatus struct {
|
|
|
|
resp *grab.Response
|
|
|
|
Name string `json:"name"`
|
|
|
|
Path string `json:"path"`
|
|
|
|
Url string `json:"url"`
|
|
|
|
Transferred int64 `json:"transferred"`
|
|
|
|
Size int64 `json:"size"`
|
|
|
|
Speed float64 `json:"speed"`
|
|
|
|
Progress float64 `json:"progress"`
|
2023-05-20 13:46:33 +08:00
|
|
|
Downloading bool `json:"downloading"`
|
2023-05-20 13:00:08 +08:00
|
|
|
Done bool `json:"done"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var downloadList []DownloadStatus
|
|
|
|
|
2023-05-20 13:46:33 +08:00
|
|
|
func existsInDownloadList(url string) bool {
|
|
|
|
for _, ds := range downloadList {
|
|
|
|
if ds.Url == url {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) PauseDownload(url string) {
|
|
|
|
for i, ds := range downloadList {
|
|
|
|
if ds.Url == url {
|
2023-05-21 12:07:53 +08:00
|
|
|
if ds.resp != nil {
|
|
|
|
ds.resp.Cancel()
|
|
|
|
}
|
2023-05-20 13:46:33 +08:00
|
|
|
|
|
|
|
downloadList[i] = DownloadStatus{
|
2023-05-21 12:14:36 +08:00
|
|
|
resp: ds.resp,
|
2023-05-20 13:46:33 +08:00
|
|
|
Name: ds.Name,
|
|
|
|
Path: ds.Path,
|
|
|
|
Url: ds.Url,
|
|
|
|
Downloading: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) ContinueDownload(url string) {
|
|
|
|
for i, ds := range downloadList {
|
|
|
|
if ds.Url == url {
|
|
|
|
client := grab.NewClient()
|
|
|
|
req, _ := grab.NewRequest(ds.Path, ds.Url)
|
|
|
|
resp := client.Do(req)
|
|
|
|
|
|
|
|
downloadList[i] = DownloadStatus{
|
|
|
|
resp: resp,
|
|
|
|
Name: ds.Name,
|
|
|
|
Path: ds.Path,
|
|
|
|
Url: ds.Url,
|
|
|
|
Downloading: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-20 13:00:08 +08:00
|
|
|
func (a *App) AddToDownloadList(path string, url string) {
|
2023-05-20 13:46:33 +08:00
|
|
|
if !existsInDownloadList(url) {
|
|
|
|
downloadList = append(downloadList, DownloadStatus{
|
2023-05-21 10:55:23 +08:00
|
|
|
resp: nil,
|
2023-05-20 13:46:33 +08:00
|
|
|
Name: filepath.Base(path),
|
2023-06-01 16:54:21 +08:00
|
|
|
Path: a.exDir + path,
|
2023-05-20 13:46:33 +08:00
|
|
|
Url: url,
|
|
|
|
Downloading: true,
|
|
|
|
})
|
2023-05-21 10:55:23 +08:00
|
|
|
a.ContinueDownload(url)
|
2023-05-20 14:53:52 +08:00
|
|
|
} else {
|
|
|
|
a.ContinueDownload(url)
|
2023-05-20 13:46:33 +08:00
|
|
|
}
|
2023-05-20 13:00:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) downloadLoop() {
|
|
|
|
ticker := time.NewTicker(500 * time.Millisecond)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
<-ticker.C
|
2023-05-20 13:46:33 +08:00
|
|
|
for i, ds := range downloadList {
|
2023-05-21 12:07:53 +08:00
|
|
|
transferred := int64(0)
|
|
|
|
size := int64(0)
|
|
|
|
speed := float64(0)
|
|
|
|
progress := float64(0)
|
|
|
|
downloading := ds.Downloading
|
|
|
|
done := false
|
|
|
|
if ds.resp != nil {
|
|
|
|
transferred = ds.resp.BytesComplete()
|
|
|
|
size = ds.resp.Size()
|
|
|
|
speed = ds.resp.BytesPerSecond()
|
|
|
|
progress = 100 * ds.resp.Progress()
|
|
|
|
downloading = !ds.resp.IsComplete()
|
|
|
|
done = ds.resp.Progress() == 1
|
|
|
|
}
|
2023-05-20 13:00:08 +08:00
|
|
|
downloadList[i] = DownloadStatus{
|
2023-05-20 13:46:33 +08:00
|
|
|
resp: ds.resp,
|
|
|
|
Name: ds.Name,
|
|
|
|
Path: ds.Path,
|
|
|
|
Url: ds.Url,
|
2023-05-21 12:07:53 +08:00
|
|
|
Transferred: transferred,
|
|
|
|
Size: size,
|
|
|
|
Speed: speed,
|
|
|
|
Progress: progress,
|
|
|
|
Downloading: downloading,
|
|
|
|
Done: done,
|
2023-05-20 13:00:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
runtime.EventsEmit(a.ctx, "downloadList", downloadList)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|