-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.go
93 lines (75 loc) · 1.85 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
package main
import (
"context"
"encoding/base64"
"os"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
type App struct {
ctx context.Context
args []string
}
func NewApp(args []string) *App {
return &App{
args: args,
}
}
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
func (a *App) SaveFileBkp(suggestedFilename string, base64Data string) (bool, error) {
filename, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
Title: "Save File",
Filters: []runtime.FileFilter{{DisplayName: "All Files(*)", Pattern: "*.bak;*.dat;*.vult"}},
DefaultFilename: suggestedFilename,
})
if err != nil {
return false, err
}
if filename == "" {
// User canceled the dialog
return false, nil
}
err = os.WriteFile(filename, []byte(base64Data), 0644)
if err != nil {
return false, err
}
return true, nil
}
func (a *App) GetOSArgs() []string {
return a.args
}
func (a *App) ReadTextFile(filename string) (string, error) {
data, err := os.ReadFile(filename)
if err != nil {
return "", err
}
return string(data), nil
}
func (a *App) SaveFile(suggestedFilename string, base64Data string) (string, error) {
filename, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
Title: "Save File",
Filters: []runtime.FileFilter{
{DisplayName: "All Supported Files (*.png;*.bak;*.dat;*.vult)", Pattern: "*.png;*.bak;*.dat;*.vult"},
},
DefaultFilename: suggestedFilename,
})
if err != nil {
return "", err
}
if filename == "" {
// User canceled the dialog
return "", nil
}
// Decode base64 data
data, err := base64.StdEncoding.DecodeString(base64Data)
if err != nil {
return "", err
}
// Write the data to the file
err = os.WriteFile(filename, data, 0644)
if err != nil {
return "", err
}
return filename, nil
}