RWKV-Runner/backend-golang/download.go

127 lines
2.9 KiB
Go
Raw Permalink Normal View History

2023-05-20 13:00:08 +08:00
package backend_golang
import (
"context"
2023-05-20 13:00:08 +08:00
"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
cancel context.CancelFunc
2023-05-20 13:00:08 +08:00
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:00:08 +08:00
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 _, ds := range downloadList {
2023-05-20 13:46:33 +08:00
if ds.Url == url {
if ds.cancel != nil {
ds.cancel()
2023-05-20 13:46:33 +08:00
}
ds.resp = nil
ds.Downloading = false
ds.Speed = 0
break
2023-05-20 13:46:33 +08:00
}
}
}
func (a *App) ContinueDownload(url string) {
for _, ds := range downloadList {
2023-05-20 13:46:33 +08:00
if ds.Url == url {
if !ds.Downloading && ds.resp == nil && !ds.Done {
ds.Downloading = true
2023-05-20 13:46:33 +08:00
req, err := grab.NewRequest(ds.Path, ds.Url)
if err != nil {
ds.Downloading = false
break
}
// if PauseDownload() is called before the request finished, ds.Downloading will be false
// if the user keeps clicking pause and resume, it may result in multiple requests being successfully downloaded at the same time
// so we have to create a context and cancel it when PauseDownload() is called
ctx, cancel := context.WithCancel(context.Background())
ds.cancel = cancel
req = req.WithContext(ctx)
resp := grab.DefaultClient.Do(req)
if resp != nil && resp.HTTPResponse != nil &&
resp.HTTPResponse.StatusCode >= 200 && resp.HTTPResponse.StatusCode < 300 {
ds.resp = resp
} else {
ds.Downloading = false
}
2023-05-20 13:46:33 +08:00
}
break
2023-05-20 13:46:33 +08:00
}
}
}
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: false,
2023-05-20 13:46:33 +08:00
})
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
for _, ds := range downloadList {
2023-05-21 12:07:53 +08:00
if ds.resp != nil {
ds.Transferred = ds.resp.BytesComplete()
ds.Size = ds.resp.Size()
ds.Speed = ds.resp.BytesPerSecond()
ds.Progress = 100 * ds.resp.Progress()
ds.Downloading = !ds.resp.IsComplete()
ds.Done = ds.resp.Progress() == 1
if !ds.Downloading {
ds.resp = nil
}
2023-05-20 13:00:08 +08:00
}
}
runtime.EventsEmit(a.ctx, "downloadList", downloadList)
}
}()
}