-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_uploads.go
More file actions
293 lines (260 loc) · 5.99 KB
/
Copy pathsync_uploads.go
File metadata and controls
293 lines (260 loc) · 5.99 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
/*
* 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
*
* Handle upload synchronization with eLabFTW entries.
*/
package main
import (
"bytes"
"database/sql"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
)
// pushUploadToRemoteEntity creates one upload on an eLabFTW experiment or item
// and returns the ID assigned to the new remote upload.
func (a *App) pushUploadToRemoteEntity(
profileUUID string,
instanceID int64,
entityType string,
remoteEntityID int64,
upload StoredUpload,
) (int64, error) {
remoteEntityType, err := elabftwUploadEntityType(entityType)
if err != nil {
return 0, err
}
encryptedPath, err := encryptedProfileUploadPath(
profileUUID,
upload.Hash,
)
if err != nil {
return 0, err
}
encryptedContent, err := os.ReadFile(encryptedPath)
if err != nil {
return 0, fmt.Errorf("read encrypted upload: %w", err)
}
plaintext, err := decryptRawBytes(a.activeKey, encryptedContent)
if err != nil {
return 0, fmt.Errorf("decrypt upload: %w", err)
}
// The encrypted content is no longer needed after decryption.
encryptedContent = nil
defer zeroBytes(plaintext)
// Stream the multipart request body directly to the HTTP request instead of
// buffering the complete payload in memory
pipeReader, pipeWriter := io.Pipe()
writer := multipart.NewWriter(pipeWriter)
contentType := writer.FormDataContentType()
// Report any error that occurs while producing the multipart body.
writeErr := make(chan error, 1)
// Build the multipart request body concurrently while the HTTP client reads
// from the pipe. This avoids creating a second full copy of the upload.
go func() {
fileWriter, err := writer.CreateFormFile("file", upload.RealName)
if err != nil {
_ = pipeWriter.CloseWithError(
fmt.Errorf("create upload form file: %w", err),
)
writeErr <- err
return
}
if _, err := io.Copy(fileWriter, bytes.NewReader(plaintext)); err != nil {
_ = pipeWriter.CloseWithError(
fmt.Errorf("write upload form file: %w", err),
)
writeErr <- err
return
}
if err := writer.Close(); err != nil {
_ = pipeWriter.CloseWithError(
fmt.Errorf("close multipart writer: %w", err),
)
writeErr <- err
return
}
_ = pipeWriter.Close()
writeErr <- nil
}()
path := fmt.Sprintf(
"/%s/%d/uploads",
remoteEntityType,
remoteEntityID,
)
resp, err := a.elabftwRequest(
profileUUID,
instanceID,
http.MethodPost,
path,
pipeReader,
true,
map[string]string{
"Content-Type": contentType,
},
)
if err != nil {
_ = pipeReader.Close()
<-writeErr
return 0, err
}
if err := <-writeErr; err != nil {
_ = resp.Body.Close()
return 0, err
}
// The new upload ID is returned through the Location header
location := resp.Header.Get("Location")
if err := decodeElabftwJSONResponse(resp, nil); err != nil {
return 0, err
}
remoteUploadID, err := remoteIDFromLocation(location)
if err != nil {
return 0, fmt.Errorf("parse remote upload id: %w", err)
}
return remoteUploadID, nil
}
// pushEntryUploadsToRemoteEntity pushes uploads attached to an entry that do
// not already have a remote mapping. Failures are returned as warnings because
// the parent entry may already have been successfully posted or patched
func (a *App) pushEntryUploadsToRemoteEntity(
profileUUID string,
db *sql.DB,
instanceID int64,
entryID int64,
entityType string,
remoteEntityID int64,
) []string {
uploads, err := listEntryUploadsFromDB(db, entryID)
if err != nil {
return []string{err.Error()}
}
warnings := make([]string, 0)
for _, upload := range uploads {
_, alreadyPushed, err := findRemoteUploadID(
db,
instanceID,
upload.ID,
remoteEntityID,
entityType,
)
if err != nil {
warnings = append(warnings, err.Error())
continue
}
// This local upload is already attached to this remote entity.
if alreadyPushed {
continue
}
remoteUploadID, err := a.pushUploadToRemoteEntity(
profileUUID,
instanceID,
entityType,
remoteEntityID,
upload,
)
if err != nil {
warnings = append(
warnings,
fmt.Sprintf(
"upload %q failed: %s",
upload.RealName,
err,
),
)
continue
}
if err := rememberRemoteUpload(
db,
instanceID,
entryID,
upload.ID,
remoteEntityID,
remoteUploadID,
entityType,
); err != nil {
warnings = append(
warnings,
fmt.Sprintf(
"remember remote upload %q: %s",
upload.RealName,
err,
),
)
}
}
return warnings
}
// findRemoteUploadID checks whether a local upload has already been pushed to
// the specified remote entity
func findRemoteUploadID(
db *sql.DB,
instanceID int64,
localUploadID int64,
remoteEntityID int64,
entityType string,
) (int64, bool, error) {
var remoteUploadID int64
err := db.QueryRow(`
SELECT remote_upload_id
FROM upload2remote
WHERE instance = ?
AND local_upload_id = ?
AND remote_entity_id = ?
AND type = ?
`,
instanceID,
localUploadID,
remoteEntityID,
entityType,
).Scan(&remoteUploadID)
if errors.Is(err, sql.ErrNoRows) {
return 0, false, nil
}
if err != nil {
return 0, false, fmt.Errorf("query upload2remote: %w", err)
}
return remoteUploadID, true, nil
}
// rememberRemoteUpload records the correspondence between a local upload and
// the remote upload created on an eLabFTW experiment or item
func rememberRemoteUpload(
db *sql.DB,
instanceID int64,
entryID int64,
localUploadID int64,
remoteEntityID int64,
remoteUploadID int64,
entityType string,
) error {
_, err := db.Exec(`
INSERT INTO upload2remote (
instance,
local_upload_id,
local_entry_id,
remote_entity_id,
remote_upload_id,
type
)
VALUES (?, ?, ?, ?, ?, ?)
`,
instanceID,
localUploadID,
entryID,
remoteEntityID,
remoteUploadID,
entityType,
)
if err != nil {
return fmt.Errorf("insert upload2remote: %w", err)
}
return nil
}