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 {

View File

@ -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<ModelSourceItem>[] = [
{item.downloadUrl && !item.isLocal &&
<ToolTipButton desc={t('Download')} icon={<ArrowDownload20Regular/>} onClick={() => {
toast(`${t('Downloading')} ${item.name}`, {type: 'info'});
DownloadFile(`./${manifest.localModelDir}/${item.name}`, item.downloadUrl!);
AddToDownloadList(`./${manifest.localModelDir}/${item.name}`, item.downloadUrl!);
}}/>}
{item.url && <ToolTipButton desc={t('Open Url')} icon={<Open20Regular/>} onClick={() => {
BrowserOpenURL(item.url!);

View File

@ -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() {

View File

@ -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();

View File

@ -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<void>;
export function ConvertModel(arg1:string,arg2:string,arg3:string):Promise<string>;
export function DeleteFile(arg1:string):Promise<void>;

View File

@ -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);
}