-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_transfer_handler.go
490 lines (406 loc) · 12 KB
/
file_transfer_handler.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
package ft
import (
"bytes"
"crypto/md5"
"errors"
"fmt"
"hash"
"io"
"mime"
"os"
"path/filepath"
"strings"
"github.com/apex/log"
"github.com/gorilla/websocket"
"github.com/materials-commons/hydra/pkg/mcdb/mcmodel"
"github.com/materials-commons/hydra/pkg/mcdb/stor"
"github.com/materials-commons/hydra/pkg/mcft/protocol"
"gorm.io/gorm"
)
var ErrAlreadyAuthenticated = errors.New("already authenticated")
var ErrBadProtocolSequence = errors.New("bad protocol sequence")
var ErrNotAuthenticated = errors.New("not authenticated")
type FileTransferHandler struct {
db *gorm.DB
ws *websocket.Conn
f *os.File
Project *mcmodel.Project
User mcmodel.User
File *mcmodel.File
projectStore stor.ProjectStor
fileStore stor.FileStor
convStore stor.ConversionStor
hasher hash.Hash
mcfsRoot string
}
func NewFileTransferHandler(ws *websocket.Conn, db *gorm.DB) *FileTransferHandler {
return &FileTransferHandler{
ws: ws,
db: db,
projectStore: stor.NewGormProjectStor(db),
fileStore: stor.NewGormFileStor(db, GetMCFSRoot()),
convStore: stor.NewGormConversionStor(db),
hasher: md5.New(),
mcfsRoot: GetMCFSRoot(),
}
}
func (h *FileTransferHandler) Run() error {
defer h.close()
if err := h.authenticate(); err != nil {
return err
}
var incomingRequest protocol.IncomingRequestType
for {
if err := h.ws.ReadJSON(&incomingRequest); err != nil {
//log.Errorf("Failed reading the incomingRequest: %s", err)
break
}
var err error
switch incomingRequest.RequestType {
case protocol.AuthenticateReq:
err = ErrAlreadyAuthenticated
case protocol.UploadFileReq:
return h.uploadFile()
case protocol.DownloadFileReq:
return h.downloadFile()
default:
err = fmt.Errorf("unknown request type: %d", incomingRequest.RequestType)
}
statusResponse := protocol.StatusResponse{
Status: "continue",
IsError: false,
}
if err != nil {
statusResponse.Status = fmt.Sprintf("%s", err)
statusResponse.IsError = true
_ = h.ws.WriteJSON(statusResponse)
return err
} else {
_ = h.ws.WriteJSON(statusResponse)
}
}
return nil
}
func (h *FileTransferHandler) close() {
if h.f != nil {
_ = h.f.Close()
finfo, err := os.Stat(h.File.ToUnderlyingFilePath(h.mcfsRoot))
if err != nil {
log.Errorf("Error stating file: %s", err)
return
}
checksum := fmt.Sprintf("%x", h.hasher.Sum(nil))
deleteFile, err := h.fileStore.DoneWritingToFile(h.File, checksum, finfo.Size(), h.convStore)
if err != nil {
log.Errorf("Error finishing file (%s/%d): %s", h.File.Name, h.File.ID, err)
return
}
// If deleteFile is true then DoneWritingToFile found a duplicate file and switched the pointer in the
// database to point at it. So we can delete the existing file.
if deleteFile {
if err := os.Remove(h.File.ToUnderlyingFilePathForUUID(h.mcfsRoot)); err != nil {
log.Errorf("Failed to remove file %s: %s", h.File.ToUnderlyingFilePathForUUID(h.mcfsRoot), err)
}
}
}
}
func (h *FileTransferHandler) authenticate() error {
var incomingRequest protocol.IncomingRequestType
if err := h.ws.ReadJSON(&incomingRequest); err != nil {
return err
}
if incomingRequest.RequestType != protocol.AuthenticateReq {
return errors.New("not authenticated")
}
var authReq protocol.AuthenticateRequest
if err := h.ws.ReadJSON(&authReq); err != nil {
return err
}
var user mcmodel.User
if err := h.db.Where("api_token = ?", authReq.APIToken).First(&user).Error; err != nil {
return err
}
h.User = user
if !h.projectStore.UserCanAccessProject(h.User.ID, authReq.ProjectID) {
return ErrNotAuthenticated
}
var err error
h.Project, err = h.projectStore.GetProjectByID(authReq.ProjectID)
if err != nil {
return err
}
return nil
}
func (h *FileTransferHandler) uploadFile() error {
var (
uploadReq protocol.UploadFileRequest
file *mcmodel.File
)
if err := h.ws.ReadJSON(&uploadReq); err != nil {
log.Errorf("Expected upload msg, got err: %s", err)
return err
}
dir, err := h.getOrCreateDirectory(filepath.Dir(uploadReq.Path))
if err != nil {
log.Errorf("getOrCreateDirectory failed for %s: %s", filepath.Dir(uploadReq.Path), err)
return err
}
name := filepath.Base(uploadReq.Path)
file, err = h.fileStore.CreateFile(name, h.Project.ID, dir.ID, h.User.ID, getMimeType(name))
if err != nil {
log.Errorf("CreateFile failed: %s", err)
return err
}
dirPath := file.ToUnderlyingDirPath(h.mcfsRoot)
if err := os.MkdirAll(dirPath, 0777); err != nil {
log.Errorf("Unable to create directory path %s to store file %s: %s", dirPath, name, err)
return err
}
h.File = file
h.f, err = os.Create(file.ToUnderlyingFilePath(h.mcfsRoot))
if err != nil {
log.Errorf("Unable to create file: %s", err)
return err
}
return h.runUploadFile()
}
func (h *FileTransferHandler) runUploadFile() error {
var incomingRequest protocol.IncomingRequestType
for {
if err := h.ws.ReadJSON(&incomingRequest); err != nil {
//log.Errorf("Failed reading the incomingRequest: %s", err)
break
}
var err error
switch incomingRequest.RequestType {
case protocol.FileBlockReq:
err = h.writeFileBlock()
case protocol.FinishUploadReq:
return h.computeAndValidateChecksum()
default:
err = fmt.Errorf("unknown request type: %d", incomingRequest.RequestType)
}
statusResponse := protocol.StatusResponse{
Status: "continue",
IsError: false,
}
if err != nil {
statusResponse.Status = fmt.Sprintf("%s", err)
statusResponse.IsError = true
_ = h.ws.WriteJSON(statusResponse)
return err
} else {
_ = h.ws.WriteJSON(statusResponse)
}
}
return nil
}
func (h *FileTransferHandler) getOrCreateDirectory(dirPath string) (*mcmodel.File, error) {
acquireProjectMutex(h.Project.ID)
defer releaseProjectMutex(h.Project.ID)
dir, err := h.fileStore.GetDirByPath(h.Project.ID, dirPath)
if err != nil {
dir, err = h.CreateDirectoryAll(dirPath)
if err != nil {
return nil, err
}
}
return dir, nil
}
func (h *FileTransferHandler) writeFileBlock() error {
if h.f == nil {
return ErrBadProtocolSequence
}
var fileBlockReq protocol.FileBlockRequest
if err := h.ws.ReadJSON(&fileBlockReq); err != nil {
log.Errorf("Expected FileBlock msg, got err: %s", err)
return err
}
// TODO: Put write into a loop to make sure we write all the blocks...
n, err := h.f.Write(fileBlockReq.Block)
if err != nil {
log.Errorf("Failed writing to file: %s", err)
}
// Compute checksum as we go
_, _ = io.Copy(h.hasher, bytes.NewBuffer(fileBlockReq.Block))
if n != len(fileBlockReq.Block) {
log.Errorf("Did not write all of block, wrote %d, length %d", n, len(fileBlockReq.Block))
err = errors.New("not all bytes written to file")
}
return err
}
func (h *FileTransferHandler) CreateDirectoryAll(dir string) (*mcmodel.File, error) {
dirs := strings.Split(dir, "/")
pathToCheck := "/"
parentDir, err := h.fileStore.GetDirByPath(h.Project.ID, "/")
if err != nil {
log.Errorf(" CreateDirectoryAll - FindDirByPath failed: %s", err)
return nil, err
}
for _, dirName := range dirs {
pathToCheck = filepath.Join(pathToCheck, dirName)
dirEntry, err := h.fileStore.CreateDirIfNotExists(parentDir.ID, pathToCheck, dirName, h.Project.ID, h.User.ID)
if err != nil {
log.Errorf(" CreateDirectoryAll - CreateDir failed: %s", err)
return nil, err
}
parentDir = dirEntry
}
return parentDir, nil
}
func (h *FileTransferHandler) fileNeedsConverting() bool {
switch h.File.MimeType {
case "application/msword",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/vnd.ms-powerpoint",
"application/vnd.openxmlformats-officedocument.presentationml.presentation":
// Office document that can be converted to PDF
return true
case "image/bmp",
"image/x-ms-bmp",
"image/tiff":
// images that need to be converted to JPEG to display on web
return true
default:
return false
}
}
func (h *FileTransferHandler) submitConversionJobOnFile() {
if _, err := h.convStore.AddFileToConvert(h.File); err != nil {
log.Errorf("Unable to submit conversion on file (%d)(%s): %s", h.File.ID, h.File.Name, err)
}
}
func (h *FileTransferHandler) computeAndValidateChecksum() error {
var (
finishUploadRequest protocol.FinishUploadRequest
statusResponse protocol.StatusResponse
)
if err := h.ws.ReadJSON(&finishUploadRequest); err != nil {
return err
}
checksum := fmt.Sprintf("%x", h.hasher.Sum(nil))
if checksum != finishUploadRequest.FileChecksum {
statusResponse.Status = fmt.Sprintf("checksums didn't match got (%s), expected (%s)", checksum, finishUploadRequest.FileChecksum)
statusResponse.IsError = true
} else {
statusResponse.Status = "checksums matched!"
statusResponse.IsError = false
}
return h.ws.WriteJSON(statusResponse)
}
func (h *FileTransferHandler) pointedAtExistingFile() bool {
switched, err := h.fileStore.PointAtExistingIfExists(h.File)
if err != nil {
return false
}
return switched
}
func (h *FileTransferHandler) downloadFile() error {
var (
downloadReq protocol.DownloadRequest
)
if err := h.ws.ReadJSON(&downloadReq); err != nil {
log.Errorf("Expected download msg, got err: %s", err)
return err
}
file, err := h.fileStore.GetFileByPath(h.Project.ID, downloadReq.Path)
if err != nil {
log.Errorf("GetFileByPath failed: %s", err)
return err
}
h.File = file
h.f, err = os.Open(file.ToUnderlyingFilePath(h.mcfsRoot))
if err != nil {
log.Errorf("Unable to open file: %s", err)
return err
}
defer func() {
_ = h.f.Close()
h.f = nil
}()
return h.runDownloadFile()
}
func (h *FileTransferHandler) runDownloadFile() error {
var incomingRequest protocol.IncomingRequestType
buffer := make([]byte, 32*1024*1024)
for {
if err := h.ws.ReadJSON(&incomingRequest); err != nil {
//log.Errorf("Failed reading the incomingRequest: %s", err)
break
}
var err error
switch incomingRequest.RequestType {
case protocol.FileBlockReq:
err = h.readFileBlock(buffer)
case protocol.FinishDownloadReq:
err = h.sendDownloadFinished()
// send expected checksum and size
// return h.computeAndValidateChecksum()
default:
err = fmt.Errorf("unknown request type: %d", incomingRequest.RequestType)
}
statusResponse := protocol.StatusResponse{
Status: "continue",
IsError: false,
}
if err != nil {
statusResponse.Status = fmt.Sprintf("%s", err)
statusResponse.IsError = true
_ = h.ws.WriteJSON(statusResponse)
return err
} else {
_ = h.ws.WriteJSON(statusResponse)
}
}
return nil
}
func (h *FileTransferHandler) readFileBlock(buffer []byte) error {
var req protocol.IncomingRequestType
fb := protocol.FileBlockRequest{}
n, err := h.f.Read(buffer)
if err != nil {
if err == io.EOF {
req.RequestType = protocol.FinishDownloadReq
return h.ws.WriteJSON(req)
}
return err
}
req.RequestType = protocol.FileBlockReq
if err := h.ws.WriteJSON(req); err != nil {
return err
}
fb.Block = buffer[:n]
if err := h.ws.WriteJSON(fb); err != nil {
return err
}
return nil
}
func (h *FileTransferHandler) sendDownloadFinished() error {
resp := protocol.FinishDownloadRequest{
Checksum: h.File.Checksum,
Size: int64(h.File.Size),
FileID: h.File.ID,
FileUUID: h.File.UUID,
}
return h.ws.WriteJSON(resp)
}
// getMimeType will determine the type of file from its extension. It strips out the extra information
// such as the charset and just returns the underlying type. It returns "unknown" for the mime type if
// the mime package is unable to determine the type.
func getMimeType(name string) string {
mimeType := mime.TypeByExtension(filepath.Ext(name))
if mimeType == "" {
return "unknown"
}
if mediaType, _, err := mime.ParseMediaType(mimeType); err == nil {
// If err is nil then we return the parsed mediaType
return mediaType
}
// If we are here then ParseMediaType returned an error, so brute force separating
// the string to get the media type
semicolon := strings.Index(mimeType, ";")
if semicolon == -1 {
return strings.TrimSpace(mimeType)
}
return strings.TrimSpace(mimeType[:semicolon])
}