]> git.r.bdr.sh - rbdr/mobius/blob - hotline/file_transfer.go
Initial implementation of file transfer resume
[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 fileResumeData *FileResumeData
27 }
28
29 func (ft *FileTransfer) String() string {
30 percentComplete := 10
31 out := fmt.Sprintf("%s\t %v", ft.FileName, percentComplete)
32
33 return out
34 }
35
36 func (ft *FileTransfer) ItemCount() int {
37 return int(binary.BigEndian.Uint16(ft.FolderItemCount))
38 }
39
40 // 00 28 // DataSize
41 // 00 00 // IsFolder
42 // 00 02 // PathItemCount
43 //
44 // 00 00
45 // 09
46 // 73 75 62 66 6f 6c 64 65 72 // "subfolder"
47 //
48 // 00 00
49 // 15
50 // 73 75 62 66 6f 6c 64 65 72 2d 74 65 73 74 66 69 6c 65 2d 35 6b // "subfolder-testfile-5k"
51 func readFolderUpload(buf []byte) folderUpload {
52 dataLen := binary.BigEndian.Uint16(buf[0:2])
53
54 fu := folderUpload{
55 DataSize: [2]byte{buf[0], buf[1]}, // Size of this structure (not including data size element itself)
56 IsFolder: [2]byte{buf[2], buf[3]},
57 PathItemCount: [2]byte{buf[4], buf[5]},
58 FileNamePath: buf[6 : dataLen+2],
59 }
60
61 return fu
62 }
63
64
65 func (fu *folderUpload) UnmarshalBinary(b []byte) error {
66 fu.DataSize = [2]byte{b[0], b[1]}
67 fu.IsFolder = [2]byte{b[2], b[3]}
68 fu.PathItemCount = [2]byte{b[4], b[5]}
69
70 return nil
71 }
72
73 type folderUpload struct {
74 DataSize [2]byte
75 IsFolder [2]byte
76 PathItemCount [2]byte
77 FileNamePath []byte
78 }
79
80 func (fu *folderUpload) FormattedPath() string {
81 pathItemLen := binary.BigEndian.Uint16(fu.PathItemCount[:])
82
83 var pathSegments []string
84 pathData := fu.FileNamePath
85
86 for i := uint16(0); i < pathItemLen; i++ {
87 segLen := pathData[2]
88 pathSegments = append(pathSegments, string(pathData[3:3+segLen]))
89 pathData = pathData[3+segLen:]
90 }
91
92 return strings.Join(pathSegments, pathSeparator)
93 }