-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
230 lines (190 loc) · 5.47 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"context"
"encoding/json"
"io"
"io/fs"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"sync"
"github.com/dropbox/dropbox-sdk-go-unofficial/v6/dropbox"
"github.com/dropbox/dropbox-sdk-go-unofficial/v6/dropbox/files"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
var downloadsFolder string
var launcher *Launcher
var downloadPercent int
// Launcher struct
type Launcher struct {
ctx context.Context
}
// NewApp creates a new App application struct
func NewApp() *Launcher {
launcher = &Launcher{}
return launcher
}
// startup is called at application startup
func (b *Launcher) startup(ctx context.Context) {
b.ctx = ctx
Appdata, _ := os.UserConfigDir()
downloadsFolder = path.Join(Appdata, "IrishBruse", "Launcher")
os.MkdirAll(downloadsFolder, 0666)
}
// domReady is called after the front-end dom has been loaded
func (b *Launcher) domReady(ctx context.Context) {
}
// shutdown is called at application termination
func (b *Launcher) shutdown(ctx context.Context) {
}
// PassThru test
type PassThru struct {
io.Reader
total int64 // Total # of bytes transferred
length int64 // Expected length
progress float64
}
// Read 'overrides' the underlying io.Reader's Read method.
// This is the one that will be called by io.Copy(). We simply
// use it to keep track of byte counts and then forward the call.
func (pt *PassThru) Read(p []byte) (int, error) {
n, err := pt.Reader.Read(p)
if n > 0 {
pt.total += int64(n)
percentage := float64(pt.total) / float64(pt.length) * float64(95)
downloadPercent = int(percentage)
runtime.EventsEmit(launcher.ctx, "downloadProgress", downloadPercent)
}
return n, err
}
// Play version
func (b *Launcher) Play(folder string) {
runtime.WindowMinimise(b.ctx)
pattern := path.Join(downloadsFolder, folder, "/*.exe")
executables, err := filepath.Glob(pattern)
if err != nil {
runtime.MessageDialog(b.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Download (dbx.Download) Error!",
Message: err.Error(),
})
runtime.LogError(b.ctx, err.Error())
return
}
app := exec.Command(executables[0])
app.Dir = path.Join(downloadsFolder, folder)
app.Run()
runtime.WindowUnminimise(b.ctx)
}
// Delete version
func (b *Launcher) Delete(folder string) {
path := path.Join(downloadsFolder, folder)
os.RemoveAll(path)
}
// Download url
func (b *Launcher) Download(file string) {
runtime.LogInfo(b.ctx, file)
config := dropbox.Config{
Token: dropboxToken,
LogLevel: dropbox.LogDebug,
}
dbx := files.New(config)
downloadArg := files.NewDownloadArg(file)
res, reader, err := dbx.Download(downloadArg)
if err != nil {
runtime.MessageDialog(b.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Download (dbx.Download) Error!",
Message: err.Error(),
})
runtime.LogError(b.ctx, err.Error())
return
}
defer reader.Close()
os.MkdirAll(downloadsFolder+path.Dir(file), fs.ModeDir)
readerpt := &PassThru{Reader: reader, length: int64(res.Size)}
data, err := ioutil.ReadAll(readerpt)
if err != nil {
runtime.MessageDialog(b.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Download (ioutil.ReadAll) Error!",
Message: err.Error(),
})
runtime.LogError(b.ctx, err.Error())
return
}
os.WriteFile(downloadsFolder+file, data, 0644)
versionFolder := path.Join(downloadsFolder, strings.Replace(file, ".zip", "", 1))
Unzip(downloadsFolder+file, versionFolder)
os.Remove(downloadsFolder + file)
runtime.EventsEmit(b.ctx, "downloadProgress", 100)
}
// GetApps returns an array of icon urls from dropbox
func (b *Launcher) GetApps() string {
apps := make([]ListItem, 0, 10)
var wg sync.WaitGroup
dbxinit()
appNames := dropboxGetApps(b.ctx)
for i := 0; i < len(appNames); i++ {
apps = append(apps, ListItem{Name: appNames[i]})
apps[i].Downloaded = make([]string, 0)
}
wg.Add(1)
go func() {
dropboxFetchIcons(b.ctx, apps)
wg.Done()
}()
wg.Add(1)
go func() {
dropboxFetchVersions(b.ctx, apps)
wg.Done()
}()
wg.Wait()
downloads, err := os.ReadDir(downloadsFolder)
if err != nil {
runtime.MessageDialog(b.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Download (os.ReadDir(downloadsFolder)) Error!",
Message: err.Error(),
})
runtime.LogError(b.ctx, err.Error())
return ""
}
for _, d := range downloads {
if d.IsDir() {
versions, err := os.ReadDir(path.Join(downloadsFolder, d.Name()))
if err != nil {
runtime.MessageDialog(b.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Download (os.ReadDir(path.Join(downloadsFolder, app.Name()))) Error!",
Message: err.Error(),
})
runtime.LogError(b.ctx, err.Error())
return ""
}
for _, version := range versions {
for i := 0; i < len(apps); i++ {
if apps[i].Name == d.Name() {
apps[i].Downloaded = append(apps[i].Downloaded, version.Name())
break
}
}
}
}
}
b2, err := json.Marshal(apps)
if err != nil {
runtime.MessageDialog(b.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Dropbox (json.Marshal(apps)) Error!",
Message: err.Error(),
})
runtime.LogError(b.ctx, err.Error())
return ""
}
// runtime.LogInfo(b.ctx, string(b2))
return string(b2)
}