2023-05-20 13:00:08 +08:00
|
|
|
package backend_golang
|
|
|
|
|
|
|
|
import (
|
2023-06-05 22:54:36 +08:00
|
|
|
"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 {
|
2024-02-02 22:00:01 +08:00
|
|
|
absPath, err := a.GetAbsPath(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = grab.Get(absPath, url)
|
2023-05-20 13:00:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type DownloadStatus struct {
|
|
|
|
resp *grab.Response
|
2023-06-05 22:54:36 +08:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2023-06-05 22:54:36 +08:00
|
|
|
var downloadList []*DownloadStatus
|
2023-05-20 13:00:08 +08:00
|
|
|
|
2023-11-26 22:54:59 +08:00
|
|
|
func existsInDownloadList(path string, url string) bool {
|
2023-05-20 13:46:33 +08:00
|
|
|
for _, ds := range downloadList {
|
2023-11-26 22:54:59 +08:00
|
|
|
if ds.Path == path || ds.Url == url {
|
2023-05-20 13:46:33 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) PauseDownload(url string) {
|
2023-06-05 22:54:36 +08:00
|
|
|
for _, ds := range downloadList {
|
2023-05-20 13:46:33 +08:00
|
|
|
if ds.Url == url {
|
2023-06-05 22:54:36 +08:00
|
|
|
if ds.cancel != nil {
|
|
|
|
ds.cancel()
|
2023-05-20 13:46:33 +08:00
|
|
|
}
|
2023-06-05 22:54:36 +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) {
|
2023-06-05 22:54:36 +08:00
|
|
|
for _, ds := range downloadList {
|
2023-05-20 13:46:33 +08:00
|
|
|
if ds.Url == url {
|
2023-06-05 22:54:36 +08:00
|
|
|
if !ds.Downloading && ds.resp == nil && !ds.Done {
|
|
|
|
ds.Downloading = true
|
2023-05-20 13:46:33 +08:00
|
|
|
|
2023-06-05 22:54:36 +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
|
|
|
}
|
2023-06-05 22:54:36 +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) {
|
2024-02-02 22:00:01 +08:00
|
|
|
absPath, err := a.GetAbsPath(path)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !existsInDownloadList(absPath, url) {
|
2023-06-05 22:54:36 +08:00
|
|
|
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),
|
2024-02-02 22:00:01 +08:00
|
|
|
Path: absPath,
|
2023-05-20 13:46:33 +08:00
|
|
|
Url: url,
|
2023-06-05 22:54:36 +08:00
|
|
|
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
|
2023-06-05 22:54:36 +08:00
|
|
|
for _, ds := range downloadList {
|
2023-05-21 12:07:53 +08:00
|
|
|
if ds.resp != nil {
|
2023-06-05 22:54:36 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|