download list

This commit is contained in:
josc146
2023-05-20 13:00:08 +08:00
parent 4f8e35ce62
commit 0761df8df5
8 changed files with 104 additions and 12 deletions

View File

@@ -24,6 +24,8 @@ func NewApp() *App {
// so we can call the runtime methods
func (a *App) OnStartup(ctx context.Context) {
a.ctx = ctx
a.downloadLoop()
}
func (a *App) UpdateApp(url string) (broken bool, err error) {

View File

@@ -0,0 +1,72 @@
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 {
_, err := grab.Get(path, url)
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"`
Done bool `json:"done"`
}
var downloadList []DownloadStatus
func (a *App) AddToDownloadList(path string, url string) {
client := grab.NewClient()
req, _ := grab.NewRequest(path, url)
resp := client.Do(req)
downloadList = append(downloadList, DownloadStatus{
resp: resp,
Name: filepath.Base(path),
Path: path,
Url: url,
Transferred: 0,
Size: 0,
Speed: 0,
Progress: 0,
Done: false,
})
}
func (a *App) downloadLoop() {
ticker := time.NewTicker(500 * time.Millisecond)
go func() {
for {
<-ticker.C
for i, downloadStatus := range downloadList {
downloadList[i] = DownloadStatus{
resp: downloadStatus.resp,
Name: downloadStatus.Name,
Path: downloadStatus.Path,
Url: downloadStatus.Url,
Transferred: downloadStatus.resp.BytesComplete(),
Size: downloadStatus.resp.Size(),
Speed: downloadStatus.resp.BytesPerSecond(),
Progress: 100 * downloadStatus.resp.Progress(),
Done: downloadStatus.resp.IsComplete(),
}
}
runtime.EventsEmit(a.ctx, "downloadList", downloadList)
}
}()
}

View File

@@ -8,8 +8,6 @@ import (
"path/filepath"
"runtime"
"time"
"github.com/cavaliergopher/grab/v3"
)
func (a *App) SaveJson(fileName string, jsonData any) error {
@@ -86,14 +84,6 @@ func (a *App) ListDirFiles(dirPath string) ([]FileInfo, error) {
return filesInfo, nil
}
func (a *App) DownloadFile(path string, url string) error {
_, err := grab.Get(path, url)
if err != nil {
return err
}
return nil
}
func (a *App) DeleteFile(path string) error {
err := os.Remove(path)
if err != nil {