diff --git a/backend-golang/app.go b/backend-golang/app.go index 21a404e..4047019 100644 --- a/backend-golang/app.go +++ b/backend-golang/app.go @@ -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) { diff --git a/backend-golang/download.go b/backend-golang/download.go new file mode 100644 index 0000000..397f601 --- /dev/null +++ b/backend-golang/download.go @@ -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) + } + }() +} diff --git a/backend-golang/file.go b/backend-golang/file.go index ed14736..7e54b8b 100644 --- a/backend-golang/file.go +++ b/backend-golang/file.go @@ -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 { diff --git a/frontend/src/pages/Models.tsx b/frontend/src/pages/Models.tsx index 47edf5b..bc3828c 100644 --- a/frontend/src/pages/Models.tsx +++ b/frontend/src/pages/Models.tsx @@ -17,7 +17,7 @@ import {ArrowClockwise20Regular, ArrowDownload20Regular, Folder20Regular, Open20 import {observer} from 'mobx-react-lite'; import commonStore, {ModelSourceItem} from '../stores/commonStore'; import {BrowserOpenURL} from '../../wailsjs/runtime'; -import {DownloadFile, OpenFileFolder} from '../../wailsjs/go/backend_golang/App'; +import {AddToDownloadList, OpenFileFolder} from '../../wailsjs/go/backend_golang/App'; import manifest from '../../../manifest.json'; import {toast} from 'react-toastify'; import {Page} from '../components/Page'; @@ -135,7 +135,7 @@ const columns: TableColumnDefinition[] = [ {item.downloadUrl && !item.isLocal && } onClick={() => { toast(`${t('Downloading')} ${item.name}`, {type: 'info'}); - DownloadFile(`./${manifest.localModelDir}/${item.name}`, item.downloadUrl!); + AddToDownloadList(`./${manifest.localModelDir}/${item.name}`, item.downloadUrl!); }}/>} {item.url && } onClick={() => { BrowserOpenURL(item.url!); diff --git a/frontend/src/startup.ts b/frontend/src/startup.ts index b516f2c..7b79434 100644 --- a/frontend/src/startup.ts +++ b/frontend/src/startup.ts @@ -2,6 +2,7 @@ import commonStore, {defaultModelConfigs} from './stores/commonStore'; import {ReadJson} from '../wailsjs/go/backend_golang/App'; import {checkUpdate, downloadProgramFiles, LocalConfig, refreshModels} from './utils'; import {getStatus} from './apis'; +import {EventsOn} from '../wailsjs/runtime'; export async function startup() { downloadProgramFiles(); @@ -17,6 +18,11 @@ export async function startup() { if (status) commonStore.setModelStatus(status); }); + + EventsOn('downloadList', (data) => { + if (data) + commonStore.setDownloadList(data); + }); } async function initRemoteText() { diff --git a/frontend/src/stores/commonStore.ts b/frontend/src/stores/commonStore.ts index 66db475..f8df3bb 100644 --- a/frontend/src/stores/commonStore.ts +++ b/frontend/src/stores/commonStore.ts @@ -52,6 +52,17 @@ export type ModelConfig = { modelParameters: ModelParameters } +export type DownloadStatus = { + name: string; + path: string; + url: string; + transferred: number; + size: number; + speed: number; + progress: number; + done: boolean; +} + export const defaultModelConfigs: ModelConfig[] = [ { name: 'Default', @@ -91,6 +102,7 @@ class CommonStore { }; introduction: { [lang: string]: string } = manifest.introduction; about: { [lang: string]: string } = manifest.about; + downloadList: DownloadStatus[] = []; getCurrentModelConfig = () => { return this.modelConfigs[this.currentModelConfigIndex]; @@ -168,6 +180,10 @@ class CommonStore { setAbout = (value: { [lang: string]: string }) => { this.about = value; }; + + setDownloadList = (value: DownloadStatus[]) => { + this.downloadList = value; + }; } export default new CommonStore(); \ No newline at end of file diff --git a/frontend/wailsjs/go/backend_golang/App.d.ts b/frontend/wailsjs/go/backend_golang/App.d.ts index e293a67..f75492d 100644 --- a/frontend/wailsjs/go/backend_golang/App.d.ts +++ b/frontend/wailsjs/go/backend_golang/App.d.ts @@ -2,6 +2,8 @@ // This file is automatically generated. DO NOT EDIT import {backend_golang} from '../models'; +export function AddToDownloadList(arg1:string,arg2:string):Promise; + export function ConvertModel(arg1:string,arg2:string,arg3:string):Promise; export function DeleteFile(arg1:string):Promise; diff --git a/frontend/wailsjs/go/backend_golang/App.js b/frontend/wailsjs/go/backend_golang/App.js index 403667f..9e2a9d8 100644 --- a/frontend/wailsjs/go/backend_golang/App.js +++ b/frontend/wailsjs/go/backend_golang/App.js @@ -2,6 +2,10 @@ // Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL // This file is automatically generated. DO NOT EDIT +export function AddToDownloadList(arg1, arg2) { + return window['go']['backend_golang']['App']['AddToDownloadList'](arg1, arg2); +} + export function ConvertModel(arg1, arg2, arg3) { return window['go']['backend_golang']['App']['ConvertModel'](arg1, arg2, arg3); }