-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_instances.go
More file actions
243 lines (202 loc) · 5.35 KB
/
Copy pathsync_instances.go
File metadata and controls
243 lines (202 loc) · 5.35 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
/*
* This file is part of eLabFTW Desktop.
*
* @author Nicolas CARPi <Deltablot>
* @author Moustapha Camara <Deltablot>
* @copyright 2026 Nicolas CARPi
* @see https://www.elabftw.net Official website
* SPDX-License-Identifier: GPL-3.0-or-later
*/
/*
* This file handles the synchronization of eLabFTW instances, using their URL,
* user's api Key for read/write actions, and stored with id
*/
package main
import (
"fmt"
"strings"
)
type ElabftwInstance struct {
ID int64 `json:"id"`
SiteURL string `json:"siteUrl"`
APIKey string `json:"apiKey,omitempty"`
VerifyTLS bool `json:"verifyTls"`
}
func (a *App) ListElabftwInstances(profileUUID string) ([]ElabftwInstance, error) {
profileUUID, err := a.requireUnlockedProfile(profileUUID)
if err != nil {
return nil, err
}
pdir, err := profileDir(profileUUID)
if err != nil {
return nil, err
}
db, err := OpenProfileDB(pdir)
if err != nil {
return nil, fmt.Errorf("open profile db: %w", err)
}
defer db.Close()
rows, err := db.Query(`
SELECT id, site_url, verify_tls
FROM elabftw_instances
ORDER BY site_url ASC
`)
if err != nil {
return nil, fmt.Errorf("query elabftw instances: %w", err)
}
defer rows.Close()
out := []ElabftwInstance{}
for rows.Next() {
var inst ElabftwInstance
if err := rows.Scan(&inst.ID, &inst.SiteURL, &inst.VerifyTLS); err != nil {
return nil, fmt.Errorf("scan elabftw instance: %w", err)
}
out = append(out, inst)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("rows error: %w", err)
}
return out, nil
}
func normalizeElabftwSiteURL(siteURL string) string {
siteURL = strings.TrimSpace(siteURL)
siteURL = strings.TrimRight(siteURL, "/")
return siteURL
}
func elabftwAPIBaseURL(siteURL string) string {
return normalizeElabftwSiteURL(siteURL) + "/api/v2"
}
func (a *App) AddElabftwInstance(profileUUID string, siteURL string, apiKey string, verifyTLS bool) (int64, error) {
profileUUID, err := a.requireUnlockedProfile(profileUUID)
if err != nil {
return 0, err
}
siteURL = normalizeElabftwSiteURL(siteURL)
apiKey = strings.TrimSpace(apiKey)
if siteURL == "" {
return 0, fmt.Errorf("Site URL is empty")
}
if apiKey == "" {
return 0, fmt.Errorf("API key is empty")
}
encryptedAPIKey, err := encryptString(a.activeKey, apiKey)
if err != nil {
return 0, fmt.Errorf("encrypt api key: %w", err)
}
pdir, err := profileDir(profileUUID)
if err != nil {
return 0, err
}
db, err := OpenProfileDB(pdir)
if err != nil {
return 0, fmt.Errorf("open profile db: %w", err)
}
defer db.Close()
res, err := db.Exec(`
INSERT INTO elabftw_instances (site_url, api_key, verify_tls)
VALUES (?, ?, ?)
`, siteURL, encryptedAPIKey, verifyTLS)
if err != nil {
return 0, fmt.Errorf("insert elabftw instance: %w", err)
}
id, err := res.LastInsertId()
if err != nil {
return 0, fmt.Errorf("get instance id: %w", err)
}
return id, nil
}
func (a *App) DeleteElabftwInstance(profileUUID string, id int64) error {
profileUUID, err := a.requireUnlockedProfile(profileUUID)
if err != nil {
return err
}
if id <= 0 {
return fmt.Errorf("Invalid instance id")
}
pdir, err := profileDir(profileUUID)
if err != nil {
return err
}
db, err := OpenProfileDB(pdir)
if err != nil {
return fmt.Errorf("open profile db: %w", err)
}
defer db.Close()
res, err := db.Exec(`
DELETE FROM elabftw_instances
WHERE id = ?
`, id)
if err != nil {
return fmt.Errorf("delete elabftw instance: %w", err)
}
rowsAffected, err := res.RowsAffected()
if err != nil {
return fmt.Errorf("get deleted rows count: %w", err)
}
if rowsAffected == 0 {
return fmt.Errorf("Instance not found")
}
return nil
}
// EDIT/UPDATE ELABFTW Instances
func (a *App) UpdateElabftwInstance(profileUUID string, id int64, siteURL string, apiKey string, verifyTLS bool) error {
profileUUID, err := a.requireUnlockedProfile(profileUUID)
if err != nil {
return err
}
if id <= 0 {
return fmt.Errorf("Invalid instance id")
}
siteURL = normalizeElabftwSiteURL(siteURL)
apiKey = strings.TrimSpace(apiKey)
if siteURL == "" {
return fmt.Errorf("Site URL is empty")
}
pdir, err := profileDir(profileUUID)
if err != nil {
return err
}
db, err := OpenProfileDB(pdir)
if err != nil {
return fmt.Errorf("open profile db: %w", err)
}
defer db.Close()
if apiKey == "" {
res, err := db.Exec(`
UPDATE elabftw_instances
SET site_url = ?, verify_tls = ?, modified_at = (strftime('%Y-%m-%dT%H:%M:%fZ','now'))
WHERE id = ?
`, siteURL, verifyTLS, id)
if err != nil {
return fmt.Errorf("update elabftw instance: %w", err)
}
rowsAffected, err := res.RowsAffected()
if err != nil {
return fmt.Errorf("get updated rows count: %w", err)
}
if rowsAffected == 0 {
return fmt.Errorf("Instance not found")
}
return nil
}
encryptedAPIKey, err := encryptString(a.activeKey, apiKey)
if err != nil {
return fmt.Errorf("encrypt api key: %w", err)
}
res, err := db.Exec(`
UPDATE elabftw_instances
SET site_url = ?, api_key = ?, verify_tls = ?, modified_at = (strftime('%Y-%m-%dT%H:%M:%fZ','now'))
WHERE id = ?
`, siteURL, encryptedAPIKey, verifyTLS, id)
if err != nil {
return fmt.Errorf("update elabftw instance: %w", err)
}
rowsAffected, err := res.RowsAffected()
if err != nil {
return fmt.Errorf("get updated rows count: %w", err)
}
if rowsAffected == 0 {
return fmt.Errorf("Instance not found")
}
return nil
}