-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
284 lines (256 loc) · 7 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
package main
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"time"
"github.com/fsnotify/fsnotify"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
type App struct {
getters *Getters
ctx context.Context
directories []string
hotFiler bool
done chan bool
watcher *fsnotify.Watcher
cwd string
fileTaskSize int64
lastFilePath string
}
func NewApp() *App {
return &App{
getters: &Getters{},
directories: []string{"VAULT", "M-BOX"},
hotFiler: false,
}
}
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
cwd, err := os.Getwd()
if err != nil {
runtime.LogFatal(a.ctx, fmt.Sprintf("Failed to get CWD: %v", err))
}
a.cwd = cwd
runtime.LogInfo(a.ctx, "CWD "+a.cwd)
}
func (a *App) InitializeRootFolder() error {
executablePath, err := os.Executable()
if err != nil {
return err
}
executableDir := filepath.Dir(executablePath) // Directory of the executable
newFolderPath := filepath.Join(executableDir, strings.ToLower(rootFolder))
if _, err := os.Stat(newFolderPath); os.IsNotExist(err) {
err = os.MkdirAll(newFolderPath, os.ModePerm)
if err != nil {
return err
}
} else if err != nil {
return err
}
newFilePath := filepath.Join(newFolderPath, filepath.Base(executablePath))
if _, err := os.Stat(newFilePath); err == nil {
return nil // or return an appropriate message/error if needed
} else if !os.IsNotExist(err) {
return err
}
return nil
}
func (a *App) InterruptFileTask() error {
if a.lastFilePath != "" {
file, err := os.OpenFile(a.lastFilePath, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
runtime.LogWarning(a.ctx, fmt.Sprintf("Last file null or error: %v", err))
// Don't return here, just log the error
} else {
// If opening succeeds, close the file immediately
file.Close()
runtime.LogError(a.ctx, "interrupted file task: "+filepath.Base(a.lastFilePath))
// Attempt to remove the file
if err := os.Remove(a.lastFilePath); err != nil {
runtime.LogError(a.ctx, fmt.Sprintf("Failed to remove last file: %v", err))
}
}
a.lastFilePath = ""
}
select {
case <-interrupt:
// Channel already closed or has been read from
return nil
default:
close(interrupt)
}
return nil
}
func (a *App) Login(username, password string) (int, error) {
if username == "" || password == "" {
return -1, nil
}
filePath := filepath.Join(a.cwd, keyFileName)
var loginStat int
_hashedUsername, err := hashString(username)
if err != nil {
runtime.LogError(a.ctx, fmt.Sprintf("Failed to hash username: %v", err))
return -1, err
}
hashedUsername = _hashedUsername
var shuffledCredentials = shuffleStrings(_hashedUsername, password)
_hashedCredentials, err := hashString(shuffledCredentials)
hashedCredentials = []byte(_hashedCredentials)
loginStat = checkCredentials(_hashedCredentials)
if err != nil {
runtime.LogFatal(a.ctx, fmt.Sprintf("Failed to hash credentials: %v", err))
}
switch loginStat {
case 0: //Key file does not exist
file, err := os.Create(filePath)
if err != nil {
runtime.LogFatal(a.ctx, fmt.Sprintf("Failed to create file: %v", err))
}
defer file.Close()
saveHashedCredentials(_hashedCredentials)
a.grantAccessToApp()
case 1: //File exists but is empty
break
case 2: //credentials match file hash
a.grantAccessToApp()
}
return loginStat, nil
}
func (a *App) grantAccessToApp() {
for i, dir := range a.directories {
a.directories[i] = a.cwd + string(os.PathSeparator) + dir
}
err := createDirectories(a.directories...)
if err != nil {
log.Fatal(err)
}
fileTree, err := a.BuildDirectoryFileTree(0)
if err != nil {
runtime.LogError(a.ctx, fmt.Sprintf("Failed to build fileTree: %v", err))
}
printFileTree(fileTree, false)
}
func (a *App) ResizeWindow(width int, height int) {
if a.ctx != nil {
runtime.WindowSetSize(a.ctx, width, height)
}
}
func (a *App) MinimizeApp() {
runtime.WindowMinimise(a.ctx)
}
func (a *App) CloseApp() {
if a.lastFilePath != "" {
a.InterruptFileTask()
}
os.Exit(0)
}
func (a *App) SetHotFiler(b bool) {
a.hotFiler = b
}
func (a *App) SetIsInFileTask(b bool) bool {
isInFileTask = b
if isInFileTask {
a.closeDirectoryWatcher()
} else {
a.DirectoryWatcher(lastDirIndex)
}
return isInFileTask
}
func MoveFiles(srcPaths []string, destDir string) error {
for _, srcPath := range srcPaths {
filename := filepath.Base(srcPath)
destPath := filepath.Join(destDir, filename)
// Perform the move operation
err := os.Rename(srcPath, destPath)
if err != nil {
return err
}
}
return nil
}
func (a *App) closeDirectoryWatcher() {
if a.watcher != nil {
// Remove the current directory from being watched if applicable
if lastDirIndex >= 0 {
err := a.watcher.Remove(a.directories[lastDirIndex])
if err != nil {
fmt.Println("\033[31m", "Failed to remove directory from watcher:", "\033[0m")
}
}
runtime.LogWarning(a.ctx, "stopped watching "+filepath.Base(a.directories[lastDirIndex]))
a.watcher.Close() // Close the current watcher to clean up resources
}
}
func (a *App) resetDirectoryWatcher(dirIndex int) {
var err error
a.watcher, err = fsnotify.NewWatcher()
if err != nil {
runtime.LogError(a.ctx, fmt.Sprintf("Failed to create watcher: %v", err))
return
}
a.done = make(chan bool)
lastDirIndex = dirIndex // Initialize with an invalid index
}
func (a *App) DirectoryWatcher(dirIndex int) {
a.closeDirectoryWatcher()
a.resetDirectoryWatcher(dirIndex)
delayDuration := time.Second
debounceTimer := time.NewTimer(delayDuration)
if !debounceTimer.Stop() {
<-debounceTimer.C // Drain the timer if it fired before being stopped
}
go func() {
for {
select {
case event, ok := <-a.watcher.Events:
if !ok {
return
}
fmt.Printf("DirectoryWatcherEvent: %s\n", event)
switch {
case event.Op&fsnotify.Write == fsnotify.Write:
runtime.EventsEmit(a.ctx, refreshDirSize)
case event.Op&fsnotify.Remove == fsnotify.Remove:
runtime.EventsEmit(a.ctx, refreshDirSize)
}
if !debounceTimer.Stop() {
select {
case <-debounceTimer.C: // Try to drain the channel
default:
}
}
debounceTimer.Reset(delayDuration)
case <-debounceTimer.C:
runtime.LogDebug(a.ctx, "Handling event after debounce")
if !isInFileTask {
runtime.LogInfo(a.ctx, "Emitting rebuildFileTree event")
runtime.EventsEmit(a.ctx, rebuildFileTree)
if a.hotFiler {
filepaths, err := a.getters.GetFilesByType(0, true)
if err != nil {
runtime.LogError(a.ctx, "error with event debounce "+err.Error())
}
a.EncryptFiles(filepaths)
}
}
case err, ok := <-a.watcher.Errors:
if !ok {
return
}
runtime.LogError(a.ctx, fmt.Sprintf("Watcher error: %v", err))
}
}
}()
err := a.watcher.Add(a.directories[dirIndex])
if err != nil {
runtime.LogError(a.ctx, "error with watcher Add() "+err.Error())
}
runtime.LogDebug(a.ctx, "began watching "+filepath.Base(a.directories[lastDirIndex]))
lastDirIndex = dirIndex // Update the current index
<-a.done // Keep the watcher goroutine alive
}