downloads page

This commit is contained in:
josc146
2023-05-20 13:46:33 +08:00
parent 0761df8df5
commit 0f0281dd63
9 changed files with 231 additions and 88 deletions

View File

@@ -25,27 +25,84 @@ type DownloadStatus struct {
Size int64 `json:"size"`
Speed float64 `json:"speed"`
Progress float64 `json:"progress"`
Downloading bool `json:"downloading"`
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)
func existsInDownloadList(url string) bool {
for _, ds := range downloadList {
if ds.Url == url {
return true
}
}
return false
}
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) PauseDownload(url string) {
for i, ds := range downloadList {
if ds.Url == url {
ds.resp.Cancel()
downloadList[i] = DownloadStatus{
resp: ds.resp,
Name: ds.Name,
Path: ds.Path,
Url: ds.Url,
Transferred: ds.Transferred,
Size: ds.Size,
Speed: 0,
Progress: ds.Progress,
Downloading: false,
Done: ds.Done,
}
}
}
}
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,
Transferred: ds.Transferred,
Size: ds.Size,
Speed: ds.Speed,
Progress: ds.Progress,
Downloading: true,
Done: ds.Done,
}
}
}
}
func (a *App) AddToDownloadList(path string, url string) {
if !existsInDownloadList(url) {
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,
Downloading: true,
Done: false,
})
}
}
func (a *App) downloadLoop() {
@@ -53,17 +110,18 @@ func (a *App) downloadLoop() {
go func() {
for {
<-ticker.C
for i, downloadStatus := range downloadList {
for i, ds := 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(),
resp: ds.resp,
Name: ds.Name,
Path: ds.Path,
Url: ds.Url,
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,
}
}
runtime.EventsEmit(a.ctx, "downloadList", downloadList)