-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
199 lines (164 loc) · 4.4 KB
/
app.go
File metadata and controls
199 lines (164 loc) · 4.4 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
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"sort"
"time"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// App struct
type App struct {
ctx context.Context
notesDir string
}
// Note represents a note document
type Note struct {
ID string `json:"id"`
Title string `json:"title"`
Content string `json:"content"`
Pinned bool `json:"pinned"`
CreatedAt int64 `json:"createdAt"`
UpdatedAt int64 `json:"updatedAt"`
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
// Get user data directory
homeDir, err := os.UserHomeDir()
if err != nil {
homeDir = "."
}
a.notesDir = filepath.Join(homeDir, ".notebloom-wails", "notes")
a.ensureNotesDir()
}
// ensureNotesDir creates the notes directory if it doesn't exist
func (a *App) ensureNotesDir() error {
return os.MkdirAll(a.notesDir, 0755)
}
// getNoteFilePath returns the file path for a note
func (a *App) getNoteFilePath(id string) string {
return filepath.Join(a.notesDir, fmt.Sprintf("%s.json", id))
}
// writeFileAtomic writes data to disk using a temp file in the target directory,
// then replaces the destination in one step.
func writeFileAtomic(filePath string, data []byte, perm os.FileMode) error {
dir := filepath.Dir(filePath)
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
tmpFile, err := os.CreateTemp(dir, "."+filepath.Base(filePath)+".*.tmp")
if err != nil {
return err
}
tmpPath := tmpFile.Name()
cleanup := func() {
_ = tmpFile.Close()
_ = os.Remove(tmpPath)
}
defer cleanup()
if _, err := tmpFile.Write(data); err != nil {
return err
}
if err := tmpFile.Sync(); err != nil {
return err
}
if err := tmpFile.Close(); err != nil {
return err
}
if err := os.Rename(tmpPath, filePath); err != nil {
if removeErr := os.Remove(filePath); removeErr != nil && !os.IsNotExist(removeErr) {
return fmt.Errorf("replace destination: %w", removeErr)
}
if renameErr := os.Rename(tmpPath, filePath); renameErr != nil {
return renameErr
}
}
return os.Chmod(filePath, perm)
}
// LoadNotes loads all notes from disk
func (a *App) LoadNotes() ([]Note, error) {
a.ensureNotesDir()
files, err := os.ReadDir(a.notesDir)
if err != nil {
return []Note{}, nil
}
notes := []Note{}
for _, file := range files {
if filepath.Ext(file.Name()) != ".json" {
continue
}
data, err := os.ReadFile(filepath.Join(a.notesDir, file.Name()))
if err != nil {
continue
}
var note Note
if err := json.Unmarshal(data, ¬e); err != nil {
continue
}
notes = append(notes, note)
}
// Sort notes: pinned first, then by updatedAt
sort.Slice(notes, func(i, j int) bool {
if notes[i].Pinned && !notes[j].Pinned {
return true
}
if !notes[i].Pinned && notes[j].Pinned {
return false
}
return notes[i].UpdatedAt > notes[j].UpdatedAt
})
return notes, nil
}
// SaveNote saves a note to disk
func (a *App) SaveNote(note Note) error {
a.ensureNotesDir()
data, err := json.MarshalIndent(note, "", " ")
if err != nil {
return err
}
return writeFileAtomic(a.getNoteFilePath(note.ID), data, 0644)
}
// DeleteNote deletes a note from disk
func (a *App) DeleteNote(id string) error {
filePath := a.getNoteFilePath(id)
if _, err := os.Stat(filePath); os.IsNotExist(err) {
return nil
}
return os.Remove(filePath)
}
// SaveAsNote exports a note to a user-selected location
func (a *App) SaveAsNote(title, content string) (map[string]interface{}, error) {
if title == "" {
title = "Untitled"
}
defaultPath := fmt.Sprintf("%s.txt", title)
filePath, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
Title: "Save Note As",
DefaultFilename: defaultPath,
Filters: []runtime.FileFilter{
{DisplayName: "Text Files (*.txt)", Pattern: "*.txt"},
{DisplayName: "All Files (*.*)", Pattern: "*.*"},
},
})
if err != nil || filePath == "" {
return map[string]interface{}{"success": false}, nil
}
if err := writeFileAtomic(filePath, []byte(content), 0644); err != nil {
return map[string]interface{}{"success": false}, err
}
return map[string]interface{}{
"success": true,
"filePath": filePath,
}, nil
}
// GetCurrentTime returns the current Unix timestamp in milliseconds
func (a *App) GetCurrentTime() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}