]> git.r.bdr.sh - rbdr/mobius/blob - hotline/file_transfer.go
90dbbe669a2c9391afbb481f81f5c27bcded0eaa
[rbdr/mobius] / hotline / file_transfer.go
1 package hotline
2
3 import (
4 "encoding/binary"
5 "fmt"
6 "path/filepath"
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 type folderUpload struct {
42 DataSize [2]byte
43 IsFolder [2]byte
44 PathItemCount [2]byte
45 FileNamePath []byte
46 }
47
48 func (fu *folderUpload) FormattedPath() string {
49 pathItemLen := binary.BigEndian.Uint16(fu.PathItemCount[:])
50
51 var pathSegments []string
52 pathData := fu.FileNamePath
53
54 // TODO: implement scanner interface instead?
55 for i := uint16(0); i < pathItemLen; i++ {
56 segLen := pathData[2]
57 pathSegments = append(pathSegments, string(pathData[3:3+segLen]))
58 pathData = pathData[3+segLen:]
59 }
60
61 return filepath.Join(pathSegments...)
62 }