]> git.r.bdr.sh - rbdr/mobius/blob - hotline/file_path.go
Prevent user with accessAnyName from change name
[rbdr/mobius] / hotline / file_path.go
1 package hotline
2
3 import (
4 "bytes"
5 "encoding/binary"
6 "errors"
7 "io"
8 "path/filepath"
9 "strings"
10 )
11
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"
16 type FilePathItem struct {
17 Len byte
18 Name []byte
19 }
20
21 type FilePath struct {
22 ItemCount [2]byte
23 Items []FilePathItem
24 }
25
26 func (fp *FilePath) UnmarshalBinary(b []byte) error {
27 reader := bytes.NewReader(b)
28 err := binary.Read(reader, binary.BigEndian, &fp.ItemCount)
29 if err != nil && !errors.Is(err, io.EOF) {
30 return err
31 }
32 if errors.Is(err, io.EOF) {
33 return nil
34 }
35
36 for i := uint16(0); i < fp.Len(); i++ {
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})
54 }
55
56 return nil
57 }
58
59 func (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
67 func (fp *FilePath) IsUploadDir() bool {
68 if fp.Len() == 0 {
69 return false
70 }
71
72 return strings.Contains(strings.ToLower(string(fp.Items[fp.Len()-1].Name)), "upload")
73 }
74
75 func (fp *FilePath) Len() uint16 {
76 return binary.BigEndian.Uint16(fp.ItemCount[:])
77 }
78
79 func readPath(fileRoot string, filePath, fileName []byte) (fullPath string, err error) {
80 var fp FilePath
81 if filePath != nil {
82 if err = fp.UnmarshalBinary(filePath); err != nil {
83 return "", err
84 }
85 }
86
87 var subPath string
88 for _, pathItem := range fp.Items {
89 subPath = filepath.Join("/", subPath, string(pathItem.Name))
90 }
91
92 fullPath = filepath.Join(
93 fileRoot,
94 subPath,
95 filepath.Join("/", string(fileName)),
96 )
97
98 return fullPath, nil
99 }