]> git.r.bdr.sh - rbdr/mobius/blob - hotline/file_transfer.go
Cleanup and backfill tests
[rbdr/mobius] / hotline / file_transfer.go
1 package hotline
2
3 import (
4 "encoding/binary"
5 "fmt"
6 "strings"
7 )
8
9 // File transfer types
10 const (
11 FileDownload = 0
12 FileUpload = 1
13 FolderDownload = 2
14 FolderUpload = 3
15 )
16
17 type FileTransfer struct {
18 FileName []byte
19 FilePath []byte
20 ReferenceNumber []byte
21 Type int
22 TransferSize []byte // total size of all items in the folder. Only used in FolderUpload action
23 FolderItemCount []byte
24 BytesSent int
25 clientID uint16
26 }
27
28 func (ft *FileTransfer) String() string {
29 percentComplete := 10
30 out := fmt.Sprintf("%s\t %v", ft.FileName, percentComplete)
31
32 return out
33 }
34
35 // 00 28 // DataSize
36 // 00 00 // IsFolder
37 // 00 02 // PathItemCount
38 //
39 // 00 00
40 // 09
41 // 73 75 62 66 6f 6c 64 65 72 // "subfolder"
42 //
43 // 00 00
44 // 15
45 // 73 75 62 66 6f 6c 64 65 72 2d 74 65 73 74 66 69 6c 65 2d 35 6b // "subfolder-testfile-5k"
46 func readFolderUpload(buf []byte) folderUpload {
47 dataLen := binary.BigEndian.Uint16(buf[0:2])
48
49 fu := folderUpload{
50 DataSize: [2]byte{buf[0], buf[1]}, // Size of this structure (not including data size element itself)
51 IsFolder: [2]byte{buf[2], buf[3]},
52 PathItemCount: [2]byte{buf[4], buf[5]},
53 FileNamePath: buf[6 : dataLen+2],
54 }
55
56 return fu
57 }
58
59
60 func (fu *folderUpload) UnmarshalBinary(b []byte) error {
61 fu.DataSize = [2]byte{b[0], b[1]}
62 fu.IsFolder = [2]byte{b[2], b[3]}
63 fu.PathItemCount = [2]byte{b[4], b[5]}
64
65 return nil
66 }
67
68 type folderUpload struct {
69 DataSize [2]byte
70 IsFolder [2]byte
71 PathItemCount [2]byte
72 FileNamePath []byte
73 }
74
75 func (fu *folderUpload) FormattedPath() string {
76 pathItemLen := binary.BigEndian.Uint16(fu.PathItemCount[:])
77
78 var pathSegments []string
79 pathData := fu.FileNamePath
80
81 for i := uint16(0); i < pathItemLen; i++ {
82 segLen := pathData[2]
83 pathSegments = append(pathSegments, string(pathData[3:3+segLen]))
84 pathData = pathData[3+segLen:]
85 }
86
87 return strings.Join(pathSegments, pathSeparator)
88 }