-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.go
More file actions
154 lines (132 loc) · 3.68 KB
/
app.go
File metadata and controls
154 lines (132 loc) · 3.68 KB
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
package main
import (
"io"
"net"
"os"
"path/filepath"
"time"
"github.com/google/uuid"
"github.com/wailsapp/wails/v3/pkg/application"
"github.com/wailsapp/wails/v3/pkg/events"
)
// App struct
type App struct {
app *application.App
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
func (a *App) setApp(app *application.App) {
a.app = app
}
func (a *App) GetValidDevices() []Device {
return GetValidDevices()
}
func (a *App) openTransferWindow(uuid string) {
w := a.app.Window.NewWithOptions(application.WebviewWindowOptions{
Title: "XPSync",
DisableResize: true,
Width: 378,
Height: 250,
Frameless: true,
DevToolsEnabled: true,
Mac: application.MacWindow{
InvisibleTitleBarHeight: 50,
Backdrop: application.MacBackdropTranslucent,
TitleBar: application.MacTitleBarHiddenInset,
},
BackgroundColour: application.NewRGB(236, 233, 216),
URL: "/transfer.html",
})
w.OnWindowEvent(events.Common.WindowRuntimeReady, func(event *application.WindowEvent) {
w.ExecJS("let transferUUID = '" + uuid + "';")
})
}
func (a *App) CloseTransfer(uuid string) {
for _, dev := range devices {
if dev.PendingAction.UUID == uuid && dev.PendingAction.ProgressedWriter != nil {
dev.PendingAction.ProgressedWriter.Close()
}
dev.PendingAction.UploadPreWriterStatus = "Canceled"
}
}
func (a *App) GetTransferStatus(uuid string) (bytesCopied int64, totalBytes int64, progress float64, message string, fileName string, device string, complete bool, bytesPerSecond float64) {
bytesCopied = 0
totalBytes = 0
progress = float64(1)
message = "Transfer complete"
fileName = ""
device = ""
bytesPerSecond = 0
complete = true
for _, dev := range devices {
if dev.PendingAction.UUID == uuid {
pw := dev.PendingAction.ProgressedWriter
device = dev.Ip
if pw == nil && dev.PendingAction.UploadPreWriterStatus == "RequestReceived" {
message = "Preparing"
progress = -1
fileName = "<Unknown>"
} else if pw == nil {
message = "Waiting for device"
progress = 0
fileName = "<Unknown>"
} else {
message = "Transferring"
fileName = dev.PendingAction.FileName
totalBytes = pw.Size
bytesCopied = pw.BytesWritten()
progress = float64(bytesCopied) / float64(totalBytes)
bytesPerSecond = float64(bytesCopied) / time.Since(dev.PendingAction.BegunTime).Seconds()
}
complete = false
}
}
return
}
func (a *App) SendFile(deviceIp string) {
dialog := application.OpenFileDialog()
dialog.SetTitle("Choose a file to transfer")
dialog.CanChooseDirectories(false)
fpath, err := dialog.PromptForSingleSelection()
if err != nil || fpath == "" {return}
file, err := os.Open(fpath)
if err != nil {return}
uuid := uuid.New().String()
SetDevicePendingAction(deviceIp, Action{
Type: "ToDevice",
FileName: filepath.Base(fpath),
UploadReader: file,
UUID: uuid,
}, "")
a.openTransferWindow(uuid)
}
func (a *App) RecvFile(deviceIp string) {
dcb := func (name string) io.WriteCloser {
dialog := application.SaveFileDialog()
dialog.SetMessage("Choose a place to save the received file.")
dialog.SetFilename(name)
fpath, err := dialog.PromptForSingleSelection()
if err != nil {return nil}
file, err := os.Create(fpath)
if err != nil {return nil}
return file
}
uuid := uuid.New().String()
SetDevicePendingAction(deviceIp, Action{
Type: "FromDevice",
DownloadBegins: &dcb,
UUID: uuid,
}, "")
a.openTransferWindow(uuid)
}
func (a *App) GetLocalIp() string {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
return "IP"
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP.String()
}