This repository has been archived by the owner on Nov 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathapp.go
588 lines (523 loc) · 17 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
package main
import (
"context"
"errors"
"fmt"
"io"
"math"
"os"
"os/exec"
"path/filepath"
goruntime "runtime"
"strings"
"time"
"riftshare/internal/settings"
"riftshare/internal/transport"
"riftshare/internal/update"
"github.com/gen2brain/beeep"
"github.com/psanford/wormhole-william/wormhole"
"github.com/wailsapp/wails/v2/pkg/runtime"
"github.com/klauspost/compress/zip"
)
// App application struct
type App struct {
ctx context.Context
c *transport.Client
selectedFiles []string
receivedFile string
wormholeCtx *context.Context
wormholeCancel *context.CancelFunc
LogPath string
UserPrefs settings.UserSettings
IsTransferring bool
NotificationIconPath string
}
// NewApp creates a new App application struct
func NewApp(logPath string) *App {
return &App{c: transport.NewClient(), LogPath: logPath}
}
// startup is called at application startup
func (b *App) startup(ctx context.Context) {
// Perform your setup here
b.ctx = ctx
runtime.LogInfo(b.ctx, "Fetching User Preferences")
setting, err := settings.GetUserSettings()
if err != nil {
runtime.LogError(b.ctx, err.Error())
settings.SaveUserSettings(setting)
}
b.UserPrefs = setting
b.UserPrefs.Version = "v" + b.GetCurrentVersion()
b.c.Notifications = setting.Notifications
b.c.OverwriteExisting = setting.Overwrite
b.c.DownloadPath = setting.DownloadsDirectory
}
// domReady is called after the front-end dom has been loaded
func (b *App) domReady(ctx context.Context) {
// Add your action here
if b.UserPrefs.SelfUpdate && !b.AppInstalledFromPackageManager() {
runtime.LogInfo(b.ctx, "Checking For Updates")
b.UpdateCheckUI()
} else {
runtime.LogInfo(b.ctx, "Skipping Update Check")
}
b.VerifyNotificationIcon()
// Mac App Store file permissions issue, cannot retain default download directory
if b.AppInstalledFromPackageManager() && goruntime.GOOS == "darwin" {
defaultDownloadPath := transport.UserDownloadsFolder()
defaultDownloadPath = strings.TrimSuffix(defaultDownloadPath, "/Library/Containers/app.riftshare/Data/Downloads") + "/Downloads"
if b.UserPrefs.DownloadsDirectory != defaultDownloadPath {
runtime.LogInfo(b.ctx, "App is installed from Mac App Store, reset download directory")
b.UserPrefs.DownloadsDirectory = defaultDownloadPath
b.c.DownloadPath = defaultDownloadPath
b.PersistUserSettings()
}
}
}
// shutdown is called at application termination
func (b *App) shutdown(ctx context.Context) {
// Perform your teardown here
}
// Greet returns a greeting for the given name
func (b *App) OpenDirectoryDialog() ([]string, error) {
opts := runtime.OpenDialogOptions{Title: "Select Directory", DefaultDirectory: b.UserPrefs.DownloadsDirectory}
selection, err := runtime.OpenDirectoryDialog(b.ctx, opts)
if err != nil {
runtime.LogError(b.ctx, "Error opening dialog")
b.ShowErrorDialog(err.Error())
return b.selectedFiles, errors.New("system error opening dialog")
}
runtime.LogInfo(b.ctx, "File Selected:"+selection)
if selection == "" {
runtime.LogError(b.ctx, "No files selected")
return b.selectedFiles, errors.New("invalid selection")
}
b.selectedFiles = []string{selection}
dirSize, err := DirSize(selection)
if err != nil {
runtime.LogError(b.ctx, "Could not get directory size info")
return b.selectedFiles, nil
} else {
runtime.LogInfof(b.ctx, "Directory Size, %v \n", dirSize)
if dirSize > 1000000000 {
buttons := []string{"Ok"}
dialogMessage := fmt.Sprintf("You are attempting to send a large directory ( %v+ GB ). This may take time to compress before sending. Please be patient. ", dirSize/1000000000)
dialogOpts := runtime.MessageDialogOptions{Title: "Large Directory Warning", Message: dialogMessage, Type: runtime.InfoDialog, Buttons: buttons, DefaultButton: "Ok"}
_, _ = runtime.MessageDialog(b.ctx, dialogOpts)
}
}
return b.selectedFiles, nil
}
func (b *App) OpenFilesDialog() ([]string, error) {
opts := runtime.OpenDialogOptions{Title: "Select File", DefaultDirectory: b.UserPrefs.DownloadsDirectory}
selection, err := runtime.OpenMultipleFilesDialog(b.ctx, opts)
if err != nil {
runtime.LogError(b.ctx, "Error opening dialog")
b.ShowErrorDialog(err.Error())
return b.selectedFiles, errors.New("system error opening dialog")
}
runtime.LogInfo(b.ctx, "File Selected:"+fmt.Sprint(selection))
if len(selection) == 0 {
runtime.LogError(b.ctx, "No files selected")
return b.selectedFiles, errors.New("invalid selection")
}
b.selectedFiles = selection
return b.selectedFiles, nil
}
func (b *App) SendFile(filePath string) {
runtime.LogInfo(b.ctx, "Sending File: "+filePath)
runtime.EventsEmit(b.ctx, "send:status", "retrieving code")
go func() {
ctx := *b.wormholeCtx
code, status, err := b.c.NewFileSend(ctx, filePath, wormhole.WithProgress(b.UpdateSendProgress))
if err != nil {
runtime.LogError(b.ctx, "Send Failed")
runtime.EventsEmit(b.ctx, "send:status", "failed")
b.ShowErrorDialog(err.Error())
}
runtime.EventsEmit(b.ctx, "send:started", code)
runtime.EventsEmit(b.ctx, "send:status", "waiting for receiver")
select {
case s := <-status:
if s.Error != nil {
runtime.LogError(b.ctx, "Send Failed")
runtime.EventsEmit(b.ctx, "send:status", "failed")
b.ShowErrorDialog(s.Error.Error())
} else if s.OK {
runtime.LogInfo(b.ctx, "Send Success")
runtime.EventsEmit(b.ctx, "send:status", "completed")
if b.c.Notifications {
beeep.Notify("RiftShare", "Send Complete", b.NotificationIconPath)
}
}
// Remove temporary zip file upon completion
if filepath.Ext(filePath) == ".zip" && strings.Contains(filePath, "riftshare-") {
os.Remove(filePath)
}
case <-ctx.Done():
// If the request gets cancelled, log it
runtime.LogInfo(b.ctx, "Request cancelled, removing zip file")
// Remove temporary zip file upon cancelation
if filepath.Ext(filePath) == ".zip" && strings.Contains(filePath, "riftshare-") {
os.Remove(filePath)
}
return
}
}()
}
func (b *App) SendDirectory(dirPath string) {
runtime.LogInfo(b.ctx, "Sending Directory: "+dirPath)
runtime.EventsEmit(b.ctx, "send:status", "retrieving code")
go func() {
code, status, err := b.c.NewDirSend(*b.wormholeCtx, dirPath, wormhole.WithProgress(b.UpdateSendProgress))
if err != nil {
runtime.LogError(b.ctx, "Send Failed")
runtime.EventsEmit(b.ctx, "send:status", "failed")
b.ShowErrorDialog(err.Error())
}
runtime.EventsEmit(b.ctx, "send:started", code)
runtime.EventsEmit(b.ctx, "send:status", "waiting for receiver")
s := <-status
if s.Error != nil {
runtime.LogError(b.ctx, "Send Failed")
runtime.EventsEmit(b.ctx, "send:status", "failed")
b.ShowErrorDialog(s.Error.Error())
} else if s.OK {
runtime.LogInfo(b.ctx, "Send Success")
runtime.EventsEmit(b.ctx, "send:status", "completed")
}
}()
}
func (b *App) ReceiveFile(code string) {
runtime.LogInfo(b.ctx, "Receiving File...")
runtime.EventsEmit(b.ctx, "receive:status", "receiving")
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
b.wormholeCtx = &ctx
b.wormholeCancel = &cancel
pathname := make(chan string, 1)
progress := make(chan float64)
runtime.EventsEmit(b.ctx, "receive:started")
go func() {
path := <-pathname
runtime.LogInfo(b.ctx, path)
runtime.EventsEmit(b.ctx, "receive:path", path)
runtime.EventsEmit(b.ctx, "receive:status", "receiving")
b.receivedFile = path
}()
go func() {
for percent := range progress {
runtime.EventsEmit(b.ctx, "receive:updated", percent)
}
}()
go func() {
err := b.c.NewReceive(*b.wormholeCtx, code, pathname, progress)
if err != nil {
runtime.LogError(b.ctx, "Receive Failed")
runtime.EventsEmit(b.ctx, "receive:status", "failed")
b.ShowErrorDialog(err.Error())
return
}
// If zip received from another riftshare client, unzip archive
if filepath.Ext(b.receivedFile) == ".zip" && strings.Contains(b.receivedFile, "riftshare-") {
runtime.LogInfo(b.ctx, "Riftshare zip found, unzipping..")
runtime.EventsEmit(b.ctx, "receive:status", "decompressing files")
dir, err := unzipFile(b.receivedFile)
if err != nil {
runtime.LogError(b.ctx, "Error during unzip"+err.Error())
runtime.EventsEmit(b.ctx, "receive:status", "failed")
b.ShowErrorDialog(err.Error())
return
}
runtime.EventsEmit(b.ctx, "receive:path", dir)
os.Remove(b.receivedFile)
b.receivedFile = dir
}
runtime.EventsEmit(b.ctx, "receive:status", "completed")
if b.c.Notifications {
beeep.Notify("RiftShare", "Receive Complete", b.NotificationIconPath)
}
}()
}
func (b *App) UpdateSendProgress(sentBytes int64, totalBytes int64) {
percentage := math.Round(float64(sentBytes) / float64(totalBytes) * 100)
// runtime.LogInfo(b.ctx, "Progress"+strconv.Itoa(int(percentage)))
runtime.EventsEmit(b.ctx, "send:updated", percentage)
}
func (b *App) OpenFile(path string) {
var err error
switch goruntime.GOOS {
case "linux":
err = exec.Command("xdg-open", path).Run()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", path).Run()
case "darwin":
err = exec.Command("open", path).Run()
default:
err = fmt.Errorf("unsupported platform")
}
if err != nil {
runtime.LogError(b.ctx, err.Error())
// If filehandler isn't found retry with directory
dir, _ := filepath.Split(path)
b.OpenFile(dir)
}
}
func (b *App) SelectedFilesSend() {
// Create a new context
ctx := context.Background()
// Create a new context, with its cancellation function
// from the original context
ctx, cancel := context.WithCancel(ctx)
b.wormholeCtx = &ctx
b.wormholeCancel = &cancel
if len(b.selectedFiles) == 1 {
fileInfo, err := os.Stat(b.selectedFiles[0])
if err != nil {
runtime.LogError(b.ctx, "Could not check send file info")
}
if fileInfo.IsDir() {
b.SendDirectory(b.selectedFiles[0])
} else {
b.SendFile(b.selectedFiles[0])
}
} else {
archivePath := b.zipFiles(b.selectedFiles)
b.SendFile(archivePath)
}
}
func (b *App) zipFiles(pathNames []string) string {
homeDir, err := os.UserHomeDir()
if err != nil {
runtime.LogError(b.ctx, "Could not find home directory")
}
runtime.LogInfo(b.ctx, "creating zip archive...")
runtime.EventsEmit(b.ctx, "send:status", "compressing Files")
timeString := time.Now().Format("2006-01-02-15-04-05")
archiveName := fmt.Sprintf("riftshare-%s.zip", timeString)
archivePath := filepath.Join(homeDir, archiveName)
archive, err := os.Create(archivePath)
if err != nil {
runtime.LogFatal(b.ctx, "Error creating archive")
}
defer archive.Close()
w := zip.NewWriter(archive)
defer w.Close()
for _, filePath := range pathNames {
file, err := os.Open(filePath)
if err != nil {
runtime.LogError(b.ctx, err.Error())
b.ShowErrorDialog(err.Error())
}
defer file.Close()
f, err := w.Create(filepath.Base(file.Name()))
if err != nil {
runtime.LogError(b.ctx, err.Error())
b.ShowErrorDialog(err.Error())
}
_, err = io.Copy(f, file)
if err != nil {
runtime.LogError(b.ctx, err.Error())
b.ShowErrorDialog(err.Error())
}
}
runtime.EventsEmit(b.ctx, "send:status", "zip Complete")
return archivePath
}
func (b *App) CancelWormholeRequest() {
runtime.LogInfo(b.ctx, "Cancelled wormhole request. ")
cancel := *b.wormholeCancel
cancel()
}
func (b *App) UpdateCheckUI() {
shouldUpdate, latestVersion := update.CheckForUpdate()
if shouldUpdate {
updateMessage := fmt.Sprintf("New Version Available, would you like to update to v%s", latestVersion)
buttons := []string{"Yes", "No"}
dialogOpts := runtime.MessageDialogOptions{Title: "Update Available", Message: updateMessage, Type: runtime.QuestionDialog, Buttons: buttons, DefaultButton: "Yes", CancelButton: "No"}
action, err := runtime.MessageDialog(b.ctx, dialogOpts)
if err != nil {
runtime.LogError(b.ctx, "Error in update dialog. ")
}
runtime.LogInfo(b.ctx, action)
if action == "Yes" {
runtime.LogInfo(b.ctx, "Update clicked")
var updated bool
if goruntime.GOOS == "darwin" {
updated = update.DoSelfUpdateMac()
} else {
updated = update.DoSelfUpdate()
}
if updated {
buttons = []string{"Ok"}
dialogOpts = runtime.MessageDialogOptions{Title: "Update Succeeded", Message: "Update Successfull. Please restart this app to take effect. ", Type: runtime.InfoDialog, Buttons: buttons, DefaultButton: "Ok"}
runtime.MessageDialog(b.ctx, dialogOpts)
} else {
buttons = []string{"Ok"}
dialogOpts = runtime.MessageDialogOptions{Title: "Update Error", Message: "Update failed, please manually update from GitHub Releases. ", Type: runtime.InfoDialog, Buttons: buttons, DefaultButton: "Ok"}
runtime.MessageDialog(b.ctx, dialogOpts)
}
}
}
}
func (b *App) GetCurrentVersion() string {
return update.Version
}
func (b *App) GetLogPath() string {
return b.LogPath
}
func (b *App) SetDownloadsFolder() string {
// directory := b.UserPrefs.DownloadsDirectory
// if goruntime.GOOS == "darwin" && strings.Contains(directory, "Containers") {
// runtime.LogInfof(b.ctx, "Default directory is %s", directory)
// directory = "/Users/aman/Downloads/"
// }
opts := runtime.OpenDialogOptions{Title: "Select Directory", DefaultDirectory: b.UserPrefs.DownloadsDirectory}
selection, err := runtime.OpenDirectoryDialog(b.ctx, opts)
if err != nil {
runtime.LogInfo(b.ctx, "Error opening dialog")
b.ShowErrorDialog(err.Error())
}
// Check for empty / canceled string
if selection != "" {
b.c.DownloadPath = selection
b.UserPrefs.DownloadsDirectory = selection
b.PersistUserSettings()
}
return b.UserPrefs.DownloadsDirectory
}
func (b *App) SetOverwriteParam(val bool) bool {
b.c.OverwriteExisting = val
b.UserPrefs.Overwrite = val
b.PersistUserSettings()
return b.c.OverwriteExisting
}
func (b *App) SetNotificationsParam(val bool) bool {
b.c.Notifications = val
b.UserPrefs.Notifications = val
b.PersistUserSettings()
if val {
b.VerifyNotificationIcon()
}
return b.c.Notifications
}
func (b *App) GetSelectedFiles() []string {
return b.selectedFiles
}
func (b *App) ClearSelectedFiles() {
b.selectedFiles = []string{}
}
func (b *App) GetReceivedFile() string {
return b.receivedFile
}
func (b *App) GetUserPrefs() settings.UserSettings {
return b.UserPrefs
}
func (b *App) SetSelfUpdateParam(val bool) bool {
b.UserPrefs.SelfUpdate = val
b.PersistUserSettings()
return b.UserPrefs.SelfUpdate
}
func (b *App) PersistUserSettings() {
err := settings.SaveUserSettings(b.UserPrefs)
if err != nil {
runtime.LogError(b.ctx, err.Error())
}
}
func (b *App) ShowErrorDialog(message string) {
buttons := []string{"Ok"}
opts := runtime.MessageDialogOptions{Title: "Error Occured", Message: message, Buttons: buttons, Type: runtime.ErrorDialog, DefaultButton: "Ok"}
runtime.MessageDialog(b.ctx, opts)
}
func (b *App) AppInstalledFromPackageManager() bool {
switch goruntime.GOOS {
case "windows":
cmdPath, _ := os.Executable()
return strings.Contains(cmdPath, "WindowsApps")
case "darwin":
homePath, _ := os.UserHomeDir()
return strings.Contains(homePath, "Containers")
case "linux":
return true
default:
return false
}
}
func (b *App) VerifyNotificationIcon() string {
return "appicon.png"
}
// func (b *App) VerifyNotificationIcon() string {
// if goruntime.GOOS == "windows" {
// if b.UserPrefs.Notifications {
// settingsDir, ferr := settings.GetSettingsDirectory()
// if ferr != nil {
// runtime.LogError(b.ctx, "Could not open settings directory")
// return ""
// }
// notificationIconPath := filepath.Join(settingsDir, "notificationIcon.png")
// if _, err := os.Stat(notificationIconPath); os.IsNotExist(err) {
// runtime.LogInfo(b.ctx, "No notification icon found, creating..")
// err = os.WriteFile(notificationIconPath, notificationIcon, 0666)
// if err != nil {
// runtime.LogError(b.ctx, "Could not write icon file")
// return ""
// }
// return notificationIconPath
// } else {
// runtime.LogInfo(b.ctx, "Notification icon found, skipping")
// return notificationIconPath
// }
// }
// }
// return ""
// }
func unzipFile(path string) (string, error) {
// Open a zip archive for reading.
r, err := zip.OpenReader(path)
if err != nil {
return "", err
}
defer r.Close()
// Destination Folder
dst := strings.Trim(path, ".zip")
for _, f := range r.File {
filePath := filepath.Join(dst, f.Name)
if !strings.HasPrefix(filePath, filepath.Clean(dst)+string(os.PathSeparator)) {
return "", errors.New("invalid file path")
}
if f.FileInfo().IsDir() {
os.MkdirAll(filePath, os.ModePerm)
continue
}
if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
return "", err
}
dstFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return "", err
}
fileInArchive, err := f.Open()
if err != nil {
return "", err
}
if _, err := io.Copy(dstFile, fileInArchive); err != nil {
return "", err
}
dstFile.Close()
fileInArchive.Close()
}
return dst, nil
}
func DirSize(path string) (int64, error) {
var size int64
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
size += info.Size()
}
return err
})
return size, err
}