]> git.r.bdr.sh - rbdr/mobius/blob - hotline/file_transfer.go
Fix getFileNameList when broken symlink present
[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 options []byte
28 }
29
30 func (ft *FileTransfer) String() string {
31 percentComplete := 10
32 out := fmt.Sprintf("%s\t %v", ft.FileName, percentComplete)
33
34 return out
35 }
36
37 func (ft *FileTransfer) ItemCount() int {
38 return int(binary.BigEndian.Uint16(ft.FolderItemCount))
39 }
40
41 // 00 28 // DataSize
42 // 00 00 // IsFolder
43 // 00 02 // PathItemCount
44 //
45 // 00 00
46 // 09
47 // 73 75 62 66 6f 6c 64 65 72 // "subfolder"
48 //
49 // 00 00
50 // 15
51 // 73 75 62 66 6f 6c 64 65 72 2d 74 65 73 74 66 69 6c 65 2d 35 6b // "subfolder-testfile-5k"
52 func readFolderUpload(buf []byte) folderUpload {
53 dataLen := binary.BigEndian.Uint16(buf[0:2])
54
55 fu := folderUpload{
56 DataSize: [2]byte{buf[0], buf[1]}, // Size of this structure (not including data size element itself)
57 IsFolder: [2]byte{buf[2], buf[3]},
58 PathItemCount: [2]byte{buf[4], buf[5]},
59 FileNamePath: buf[6 : dataLen+2],
60 }
61
62 return fu
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 }