]> git.r.bdr.sh - rbdr/mobius/blob - hotline/files.go
7c2cd1a8bf3688ec8e56ead5b632a69cca470280
[rbdr/mobius] / hotline / files.go
1 package hotline
2
3 import (
4 "encoding/binary"
5 "errors"
6 "io/fs"
7 "io/ioutil"
8 "os"
9 "path/filepath"
10 "strings"
11 )
12
13 func downcaseFileExtension(filename string) string {
14 splitStr := strings.Split(filename, ".")
15 ext := strings.ToLower(
16 splitStr[len(splitStr)-1],
17 )
18
19 return ext
20 }
21
22 func fileTypeFromFilename(fn string) fileType {
23 ft, ok := fileTypes[downcaseFileExtension(fn)]
24 if ok {
25 return ft
26 }
27 return defaultFileType
28 }
29
30 func fileTypeFromInfo(info os.FileInfo) (ft fileType, err error) {
31 if info.IsDir() {
32 ft.CreatorCode = "n/a "
33 ft.TypeCode = "fldr"
34 } else {
35 ft = fileTypeFromFilename(info.Name())
36 }
37
38 return ft, nil
39 }
40
41 func getFileNameList(filePath string) (fields []Field, err error) {
42 files, err := ioutil.ReadDir(filePath)
43 if err != nil {
44 return fields, nil
45 }
46
47 for _, file := range files {
48 var fileType []byte
49 var fnwi FileNameWithInfo
50 fileCreator := make([]byte, 4)
51 if !file.IsDir() {
52 fileType = []byte(fileTypeFromFilename(file.Name()).TypeCode)
53 fileCreator = []byte(fileTypeFromFilename(file.Name()).CreatorCode)
54
55 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(file.Size()))
56 copy(fnwi.Type[:], fileType[:])
57 copy(fnwi.Creator[:], fileCreator[:])
58 } else {
59 fileType = []byte("fldr")
60
61 dir, err := ioutil.ReadDir(filePath + "/" + file.Name())
62 if err != nil {
63 return fields, err
64 }
65 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(len(dir)))
66 copy(fnwi.Type[:], fileType[:])
67 copy(fnwi.Creator[:], fileCreator[:])
68 }
69
70 strippedName := strings.Replace(file.Name(), ".incomplete", "", -1)
71
72 nameSize := make([]byte, 2)
73 binary.BigEndian.PutUint16(nameSize, uint16(len(strippedName)))
74 copy(fnwi.NameSize[:], nameSize[:])
75
76 fnwi.name = []byte(strippedName)
77
78 b, err := fnwi.MarshalBinary()
79 if err != nil {
80 return nil, err
81 }
82 fields = append(fields, NewField(fieldFileNameWithInfo, b))
83 }
84
85 return fields, nil
86 }
87
88 func CalcTotalSize(filePath string) ([]byte, error) {
89 var totalSize uint32
90 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
91 if err != nil {
92 return err
93 }
94
95 if info.IsDir() {
96 return nil
97 }
98
99 totalSize += uint32(info.Size())
100
101 return nil
102 })
103 if err != nil {
104 return nil, err
105 }
106
107 bs := make([]byte, 4)
108 binary.BigEndian.PutUint32(bs, totalSize)
109
110 return bs, nil
111 }
112
113 func CalcItemCount(filePath string) ([]byte, error) {
114 var itemcount uint16
115 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
116 itemcount += 1
117
118 if err != nil {
119 return err
120 }
121
122 return nil
123 })
124 if err != nil {
125 return nil, err
126 }
127
128 bs := make([]byte, 2)
129 binary.BigEndian.PutUint16(bs, itemcount-1)
130
131 return bs, nil
132 }
133
134 func EncodeFilePath(filePath string) []byte {
135 pathSections := strings.Split(filePath, "/")
136 pathItemCount := make([]byte, 2)
137 binary.BigEndian.PutUint16(pathItemCount, uint16(len(pathSections)))
138
139 bytes := pathItemCount
140
141 for _, section := range pathSections {
142 bytes = append(bytes, []byte{0, 0}...)
143
144 pathStr := []byte(section)
145 bytes = append(bytes, byte(len(pathStr)))
146 bytes = append(bytes, pathStr...)
147 }
148
149 return bytes
150 }
151
152 const incompleteFileSuffix = ".incomplete"
153
154 // effectiveFile wraps os.Open to check for the presence of a partial file transfer as a fallback
155 func effectiveFile(filePath string) (*os.File, error) {
156 file, err := os.Open(filePath)
157 if err != nil && !errors.Is(err, fs.ErrNotExist) {
158 return nil, err
159 }
160
161 if errors.Is(err, fs.ErrNotExist) {
162 file, err = os.OpenFile(filePath+incompleteFileSuffix, os.O_APPEND|os.O_WRONLY, 0644)
163 if err != nil {
164 return nil, err
165 }
166 }
167 return file, nil
168 }