]> git.r.bdr.sh - rbdr/mobius/blob - hotline/files.go
Add tests and clean up
[rbdr/mobius] / hotline / files.go
1 package hotline
2
3 import (
4 "encoding/binary"
5 "io/ioutil"
6 "os"
7 "path/filepath"
8 "strings"
9 )
10
11 const defaultCreator = "TTXT"
12 const defaultType = "TEXT"
13
14 var fileCreatorCodes = map[string]string{
15 "sit": "SIT!",
16 "pdf": "CARO",
17 }
18
19 var fileTypeCodes = map[string]string{
20 "sit": "SIT!",
21 "jpg": "JPEG",
22 "pdf": "PDF ",
23 }
24
25 func fileTypeFromFilename(fn string) string {
26 ext := strings.Split(fn, ".")
27 code := fileTypeCodes[ext[len(ext)-1]]
28
29 if code == "" {
30 code = defaultType
31 }
32
33 return code
34 }
35
36 func fileCreatorFromFilename(fn string) string {
37 ext := strings.Split(fn, ".")
38 code := fileCreatorCodes[ext[len(ext)-1]]
39 if code == "" {
40 code = defaultCreator
41 }
42
43 return code
44 }
45
46 func getFileNameList(filePath string) ([]Field, error) {
47 var fields []Field
48
49 files, err := ioutil.ReadDir(filePath)
50 if err != nil {
51 return fields, nil
52 }
53
54 for _, file := range files {
55 var fileType []byte
56 var fnwi FileNameWithInfo
57 fileCreator := make([]byte, 4)
58 //fileSize := make([]byte, 4)
59 if !file.IsDir() {
60 fileType = []byte(fileTypeFromFilename(file.Name()))
61 fileCreator = []byte(fileCreatorFromFilename(file.Name()))
62
63 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(file.Size()))
64 copy(fnwi.Type[:], fileType[:])
65 copy(fnwi.Creator[:], fileCreator[:])
66 } else {
67 fileType = []byte("fldr")
68
69 dir, err := ioutil.ReadDir(filePath + "/" + file.Name())
70 if err != nil {
71 return fields, err
72 }
73 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(len(dir)))
74 copy(fnwi.Type[:], fileType[:])
75 copy(fnwi.Creator[:], fileCreator[:])
76 }
77
78 nameSize := make([]byte, 2)
79 binary.BigEndian.PutUint16(nameSize, uint16(len(file.Name())))
80 copy(fnwi.NameSize[:], nameSize[:])
81
82 fnwi.name = []byte(file.Name())
83
84 b, err := fnwi.MarshalBinary()
85 if err != nil {
86 return nil, err
87 }
88 fields = append(fields, NewField(fieldFileNameWithInfo, b))
89 }
90
91 return fields, nil
92 }
93
94 func CalcTotalSize(filePath string) ([]byte, error) {
95 var totalSize uint32
96 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
97 if err != nil {
98 return err
99 }
100
101 if info.IsDir() {
102 return nil
103 }
104
105 totalSize += uint32(info.Size())
106
107 return nil
108 })
109 if err != nil {
110 return nil, err
111 }
112
113 bs := make([]byte, 4)
114 binary.BigEndian.PutUint32(bs, totalSize)
115
116 return bs, nil
117 }
118
119 func CalcItemCount(filePath string) ([]byte, error) {
120 var itemcount uint16
121 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
122 itemcount += 1
123
124 if err != nil {
125 return err
126 }
127
128 return nil
129 })
130 if err != nil {
131 return nil, err
132 }
133
134 bs := make([]byte, 2)
135 binary.BigEndian.PutUint16(bs, itemcount-1)
136
137 return bs, nil
138 }
139
140 func EncodeFilePath(filePath string) []byte {
141 pathSections := strings.Split(filePath, "/")
142 pathItemCount := make([]byte, 2)
143 binary.BigEndian.PutUint16(pathItemCount, uint16(len(pathSections)))
144
145 bytes := pathItemCount
146
147 for _, section := range pathSections {
148 bytes = append(bytes, []byte{0, 0}...)
149
150 pathStr := []byte(section)
151 bytes = append(bytes, byte(len(pathStr)))
152 bytes = append(bytes, pathStr...)
153 }
154
155 return bytes
156 }
157
158 func ReadFilePath(filePathFieldData []byte) string {
159 var fp FilePath
160 err := fp.UnmarshalBinary(filePathFieldData)
161 if err != nil {
162 // TODO
163 }
164 return fp.String()
165 }