-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
324 lines (273 loc) · 6.82 KB
/
Copy pathapp.go
File metadata and controls
324 lines (273 loc) · 6.82 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
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
package main
import (
"bufio"
"context"
"encoding/json"
"fmt"
wruntime "github.com/wailsapp/wails/v2/pkg/runtime"
"io/ioutil"
"os"
"path/filepath"
gruntime "runtime"
)
// App struct
type App struct {
ctx context.Context
isDev bool
allowClose bool
}
func (a *App) AllowClose() {
a.allowClose = true
fmt.Println("App is now allowed to close.")
wruntime.Quit(a.ctx)
}
func (a *App) OnBeforeClose(ctx context.Context) (prevent bool) {
if a.allowClose {
return false // Allow closing
}
fmt.Println("Close prevented! Show a dialog here.")
return true // Prevent closing
}
func checkDevMode() bool {
return true
}
// NewApp creates a new App application struct
func NewApp() *App {
app := &App{
isDev: checkDevMode(), // ✅ Check if the app is in dev mode
}
return app
}
// startup is called when the app starts.
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
// LoadJSONFile opens a file dialog and returns the file contents.
func (a *App) LoadJSONFile() (string, error) {
// OpenFileDialog returns the selected file path (or an empty string if cancelled).
filePath, err := wruntime.OpenFileDialog(a.ctx, wruntime.OpenDialogOptions{
Title: "Select a File",
Filters: []wruntime.FileFilter{
{
DisplayName: "JSON Files (*.json)",
Pattern: "*.json",
},
},
})
if err != nil {
return "", err
}
// If no file was selected, just return an empty string.
if filePath == "" {
return "", nil
}
// Read the selected file.
bytes, err := ioutil.ReadFile(filePath)
if err != nil {
return "", err
}
return string(bytes), nil
}
func (a *App) SaveFile(content string) (string, error) {
// Open a "Save File" dialog
savePath, err := wruntime.SaveFileDialog(a.ctx, wruntime.SaveDialogOptions{
Title: "Save Your File",
DefaultFilename: "",
Filters: []wruntime.FileFilter{
{
DisplayName: "All Files",
Pattern: "",
},
},
})
if err != nil {
return "", err
}
// If the user cancels, savePath will be empty
if savePath == "" {
return "", nil
}
// Overwrite (or create) the file with the given content
err = os.WriteFile(savePath, []byte(content), 0644)
if err != nil {
return "", err
}
return savePath, nil
}
func (a *App) LoadConstants() (map[string]interface{}, error) {
var filePath string
if a.isDev {
filePath = filepath.Join("frontend", "public", "constants.json")
} else {
execPath, err := os.Executable()
if err != nil {
return nil, err
}
filePath = filepath.Join(filepath.Dir(execPath), "constants.json")
}
if _, err := os.Stat(filePath); os.IsNotExist(err) {
return nil, err // No file yet, return nil
}
data, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
var constants map[string]interface{}
err = json.Unmarshal(data, &constants)
if err != nil {
return nil, err
}
return constants, nil
}
func (a *App) SaveConstants(content interface{}) (string, error) {
data, err := json.MarshalIndent(content, "", " ")
if err != nil {
return "", err
}
if err != nil {
return "", err
}
var savePath string
if a.isDev {
// Development: Save to frontend/public/constants.json
savePath = filepath.Join("frontend", "public", "constants.json")
} else {
// Production: Save in the same directory as the app binary
execPath, err := os.Executable()
if err != nil {
return "", err
}
savePath = filepath.Join(filepath.Dir(execPath), "constants.json")
}
err = os.MkdirAll(filepath.Dir(savePath), 0755)
if err != nil {
return "", err
}
err = os.WriteFile(savePath, data, 0644)
if err != nil {
return "", err
}
return savePath, nil
}
func isDev() bool {
// Check if a file called "dev" exists in the current working dir
cwd, err := os.Getwd()
if err != nil {
return false
}
devFile := filepath.Join(cwd, "dev")
if _, err := os.Stat(devFile); err == nil {
return true
}
return false
}
func (a *App) ExposePath(filename string) (string, error) {
savePath, err := getSavePath(filename)
if err != nil {
return "", err
}
return savePath, nil
}
func getSavePath(filename string) (string, error) {
var basePath string
if isDev() {
// In dev mode, use current working dir (project root)
cwd, err := os.Getwd()
if err != nil {
return "", err
}
basePath = filepath.Join(cwd, "release")
} else {
// In production mode
execPath, err := os.Executable()
if err != nil {
return "", err
}
switch gruntime.GOOS {
case "windows":
basePath = filepath.Dir(execPath)
case "darwin":
appPath := filepath.Dir(execPath) // /MacOS
contentsPath := filepath.Dir(appPath) // /Contents
appBundlePath := filepath.Dir(contentsPath) // /MyApp.app
basePath = filepath.Dir(appBundlePath)
default:
// Fallback for other OSes
basePath = filepath.Dir(execPath)
}
}
return filepath.Join(basePath, filename), nil
}
func (a *App) SaveMaterialInventory(content interface{}) (string, error) {
data, err := json.MarshalIndent(content, "", " ")
if err != nil {
return "", err
}
baseDir, err := os.UserConfigDir() // or os.UserHomeDir()
if err != nil {
return "", err
}
appDir := filepath.Join(baseDir, "")
err = os.MkdirAll(appDir, 0755)
if err != nil {
return "", err
}
savePath, err := getSavePath("material_dictionary.json")
err = os.WriteFile(savePath, data, 0644)
if err != nil {
return "", err
}
return savePath, nil
}
func (a *App) LoadMaterialInventory() (map[string]interface{}, error) {
filePath, err := getSavePath("material_dictionary.json")
if _, err := os.Stat(filePath); os.IsNotExist(err) {
return nil, err // No file yet, return nil
}
data, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
var constants map[string]interface{}
err = json.Unmarshal(data, &constants)
if err != nil {
return nil, err
}
return constants, nil
}
func (a *App) GetStoredHash() (string, error) {
exePath, err := os.Executable()
if err != nil {
return "", fmt.Errorf("could not find executable path: %w", err)
}
exeDir := filepath.Dir(exePath)
passwordFilePath := filepath.Join(exeDir, "password")
file, err := os.Open(passwordFilePath)
if err != nil {
return "", fmt.Errorf("failed to open 'password' file: %w", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
if scanner.Scan() {
return scanner.Text(), nil
}
if err := scanner.Err(); err != nil {
return "", fmt.Errorf("failed to read 'password' file: %w", err)
}
return "", fmt.Errorf("password file is empty")
}
func (a *App) __GetStoredHash() (string, error) {
file, err := os.Open("password")
if err != nil {
return "", fmt.Errorf("failed to open 'password' file: %w", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
if scanner.Scan() {
return scanner.Text(), nil
}
if err := scanner.Err(); err != nil {
return "", fmt.Errorf("failed to read 'password' file: %w", err)
}
return "", fmt.Errorf("password file is empty")
}