-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.go
More file actions
281 lines (233 loc) · 6.71 KB
/
Copy pathapp.go
File metadata and controls
281 lines (233 loc) · 6.71 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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"time"
)
type Note struct {
ID string `json:"id"`
Title string `json:"title"`
Content string `json:"content"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
Tags []string `json:"tags,omitempty"`
}
type App struct {
ctx context.Context
notesDir string
appDataDir string
logger *log.Logger
}
func NewApp() *App {
return &App{}
}
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
logFile, err := os.OpenFile(filepath.Join(os.TempDir(), "voidnotes.log"), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err == nil {
a.logger = log.New(logFile, "", log.LstdFlags)
} else {
a.logger = log.New(os.Stderr, "", log.LstdFlags)
a.logger.Printf("Failed to open log file: %v", err)
}
a.logger.Println("App starting up...")
if err := a.initAppDataDir(); err != nil {
a.logger.Printf("Failed to initialize app data directory: %v", err)
} else {
a.logger.Printf("App data directory initialized: %s", a.appDataDir)
a.logger.Printf("Notes directory: %s", a.notesDir)
}
}
func (a *App) initAppDataDir() error {
var appDataPath string
if runtime.GOOS == "windows" {
appDataPath = os.Getenv("LOCALAPPDATA")
if appDataPath == "" {
appDataPath = filepath.Join(os.Getenv("USERPROFILE"), "AppData", "Local")
}
a.appDataDir = filepath.Join(appDataPath, "VoidNotes")
} else if runtime.GOOS == "darwin" {
homeDir, err := os.UserHomeDir()
if err != nil {
return err
}
a.appDataDir = filepath.Join(homeDir, "Library", "Application Support", "VoidNotes")
} else {
homeDir, err := os.UserHomeDir()
if err != nil {
return err
}
a.appDataDir = filepath.Join(homeDir, ".voidnotes")
}
if err := os.MkdirAll(a.appDataDir, 0755); err != nil {
return err
}
a.notesDir = filepath.Join(a.appDataDir, "notes")
if err := os.MkdirAll(a.notesDir, 0755); err != nil {
return err
}
return nil
}
func (a *App) domReady(ctx context.Context) {
a.logger.Println("DOM ready")
}
func (a *App) beforeClose(ctx context.Context) (prevent bool) {
a.logger.Println("Application is closing")
return false
}
func (a *App) shutdown(ctx context.Context) {
a.logger.Println("Application shutting down")
}
func (a *App) Greet(name string) string {
return fmt.Sprintf("Hello %s, It's show timee!", name)
}
func (a *App) GetAppDataDir() string {
return a.appDataDir
}
func (a *App) GetNotesDir() string {
return a.notesDir
}
func (a *App) SaveNote(noteData string) error {
startTime := time.Now()
if a.logger != nil {
a.logger.Println("SaveNote called with data length:", len(noteData))
}
var note Note
if err := json.Unmarshal([]byte(noteData), ¬e); err != nil {
if a.logger != nil {
a.logger.Printf("Failed to unmarshal note: %v", err)
}
return fmt.Errorf("failed to unmarshal note: %w", err)
}
filename := filepath.Join(a.notesDir, note.ID+".json")
if err := os.MkdirAll(filepath.Dir(filename), 0755); err != nil {
if a.logger != nil {
a.logger.Printf("Failed to create directory for note: %v", err)
}
return fmt.Errorf("failed to create directory: %w", err)
}
if a.logger != nil {
a.logger.Printf("Saving note ID: %s, Title: %s, Content length: %d",
note.ID, note.Title, len(note.Content))
a.logger.Printf("Writing to file: %s", filename)
}
tempFilename := filename + ".tmp"
if err := os.WriteFile(tempFilename, []byte(noteData), 0644); err != nil {
if a.logger != nil {
a.logger.Printf("Failed to write temporary note file: %v", err)
}
return fmt.Errorf("failed to write temporary note file: %w", err)
}
data, err := os.ReadFile(tempFilename)
if err != nil {
if a.logger != nil {
a.logger.Printf("Failed to read back temporary file: %v", err)
}
return fmt.Errorf("failed to verify temporary file: %w", err)
}
if len(data) != len(noteData) {
if a.logger != nil {
a.logger.Printf("Verification failed: file size mismatch. Expected %d, got %d",
len(noteData), len(data))
}
return fmt.Errorf("file size mismatch after writing")
}
if err := os.Rename(tempFilename, filename); err != nil {
if a.logger != nil {
a.logger.Printf("Failed to rename temporary file: %v", err)
}
return fmt.Errorf("failed to finalize note file: %w", err)
}
if a.logger != nil {
a.logger.Printf("Note saved successfully. ID: %s, Time taken: %v",
note.ID, time.Since(startTime))
}
fi, err := os.Stat(filename)
if err != nil {
if a.logger != nil {
a.logger.Printf("Failed to stat the saved file: %v", err)
}
return fmt.Errorf("saved file cannot be accessed: %w", err)
}
if fi.Size() != int64(len(noteData)) {
if a.logger != nil {
a.logger.Printf("Final verification failed: file size mismatch. Expected %d, got %d",
len(noteData), fi.Size())
}
return fmt.Errorf("final file size mismatch")
}
return nil
}
func (a *App) LoadNotes() (string, error) {
startTime := time.Now()
if a.logger != nil {
a.logger.Println("LoadNotes called")
}
files, err := os.ReadDir(a.notesDir)
if err != nil {
if a.logger != nil {
a.logger.Printf("Failed to read notes directory: %v", err)
}
return "", fmt.Errorf("failed to read notes directory: %w", err)
}
notes := []Note{}
if a.logger != nil {
a.logger.Printf("Found %d files in notes directory", len(files))
}
for _, file := range files {
if file.IsDir() || filepath.Ext(file.Name()) != ".json" {
continue
}
path := filepath.Join(a.notesDir, file.Name())
content, err := os.ReadFile(path)
if err != nil {
if a.logger != nil {
a.logger.Printf("Failed to read file %s: %v", file.Name(), err)
}
continue
}
var note Note
if err := json.Unmarshal(content, ¬e); err != nil {
if a.logger != nil {
a.logger.Printf("Failed to parse note file %s: %v", file.Name(), err)
}
continue
}
if a.logger != nil {
a.logger.Printf("Loaded note ID: %s, Title: %s", note.ID, note.Title)
}
notes = append(notes, note)
}
notesJSON, err := json.Marshal(notes)
if err != nil {
if a.logger != nil {
a.logger.Printf("Failed to marshal notes: %v", err)
}
return "", fmt.Errorf("failed to marshal notes: %w", err)
}
if a.logger != nil {
a.logger.Printf("Loaded %d notes. Time taken: %v", len(notes), time.Since(startTime))
}
return string(notesJSON), nil
}
func (a *App) DeleteNote(noteID string) error {
if a.logger != nil {
a.logger.Printf("DeleteNote called for ID: %s", noteID)
}
filename := filepath.Join(a.notesDir, noteID+".json")
if err := os.Remove(filename); err != nil {
if a.logger != nil {
a.logger.Printf("Failed to delete note %s: %v", noteID, err)
}
return fmt.Errorf("failed to delete note: %w", err)
}
if a.logger != nil {
a.logger.Printf("Note %s deleted successfully", noteID)
}
return nil
}