]> git.r.bdr.sh - rbdr/mobius/blame - hotline/file_path.go
Convert hardcoded path separators to filepath.Join
[rbdr/mobius] / hotline / file_path.go
CommitLineData
6988a057
JH
1package hotline
2
3import (
00d1ef67 4 "bytes"
6988a057 5 "encoding/binary"
00d1ef67 6 "errors"
050407a3 7 "io"
f22acf38 8 "path/filepath"
7e2e07da 9 "strings"
6988a057
JH
10)
11
6988a057
JH
12// FilePathItem represents the file or directory portion of a delimited file path (e.g. foo and bar in "/foo/bar")
13// 00 00
14// 09
15// 73 75 62 66 6f 6c 64 65 72 // "subfolder"
16type FilePathItem struct {
17 Len byte
18 Name []byte
19}
20
6988a057 21type FilePath struct {
00d1ef67 22 ItemCount [2]byte
72dd37f1 23 Items []FilePathItem
6988a057
JH
24}
25
72dd37f1 26func (fp *FilePath) UnmarshalBinary(b []byte) error {
050407a3
JH
27 reader := bytes.NewReader(b)
28 err := binary.Read(reader, binary.BigEndian, &fp.ItemCount)
29 if err != nil && !errors.Is(err, io.EOF) {
00d1ef67
JH
30 return err
31 }
050407a3
JH
32 if errors.Is(err, io.EOF) {
33 return nil
34 }
6988a057 35
72dd37f1 36 for i := uint16(0); i < fp.Len(); i++ {
050407a3
JH
37 // skip two bytes for the file path delimiter
38 _, _ = reader.Seek(2, io.SeekCurrent)
39
40 // read the length of the next pathItem
41 segLen, err := reader.ReadByte()
42 if err != nil {
43 return err
44 }
45
46 pBytes := make([]byte, segLen)
47
48 _, err = reader.Read(pBytes)
49 if err != nil && !errors.Is(err, io.EOF) {
50 return err
51 }
52
53 fp.Items = append(fp.Items, FilePathItem{Len: segLen, Name: pBytes})
6988a057
JH
54 }
55
72dd37f1
JH
56 return nil
57}
58
7e2e07da
JH
59func (fp *FilePath) IsDropbox() bool {
60 if fp.Len() == 0 {
61 return false
62 }
63
64 return strings.Contains(strings.ToLower(string(fp.Items[fp.Len()-1].Name)), "drop box")
65}
66
67func (fp *FilePath) IsUploadDir() bool {
68 if fp.Len() == 0 {
69 return false
70 }
71
25f0d77d 72 return strings.Contains(strings.ToLower(string(fp.Items[fp.Len()-1].Name)), "upload")
7e2e07da
JH
73}
74
72dd37f1 75func (fp *FilePath) Len() uint16 {
00d1ef67 76 return binary.BigEndian.Uint16(fp.ItemCount[:])
6988a057
JH
77}
78
79func (fp *FilePath) String() string {
00d1ef67 80 out := []string{"/"}
72dd37f1 81 for _, i := range fp.Items {
6988a057
JH
82 out = append(out, string(i.Name))
83 }
00d1ef67 84
f22acf38 85 return filepath.Join(out...)
6988a057 86}
c5d9af5a 87
92a7e455
JH
88func readPath(fileRoot string, filePath, fileName []byte) (fullPath string, err error) {
89 var fp FilePath
90 if filePath != nil {
91 if err = fp.UnmarshalBinary(filePath); err != nil {
92 return "", err
93 }
94 }
95
f22acf38 96 fullPath = filepath.Join(
92a7e455
JH
97 "/",
98 fileRoot,
99 fp.String(),
f22acf38 100 filepath.Join("/", string(fileName)),
92a7e455
JH
101 )
102
103 return fullPath, nil
104}