]> git.r.bdr.sh - rbdr/mobius/blob - hotline/files.go
Merge pull request #9 from jhalter/move_file_types_to_config_file
[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 func downcaseFileExtension(filename string) string {
12 splitStr := strings.Split(filename, ".")
13 ext := strings.ToLower(
14 splitStr[len(splitStr)-1],
15 )
16
17 return ext
18 }
19
20 func fileTypeFromFilename(fn string) fileType {
21 ft, ok := fileTypes[downcaseFileExtension(fn)]
22 if ok {
23 return ft
24 }
25 return defaultFileType
26 }
27
28 func getFileNameList(filePath string) (fields []Field, err error) {
29 files, err := ioutil.ReadDir(filePath)
30 if err != nil {
31 return fields, nil
32 }
33
34 for _, file := range files {
35 var fileType []byte
36 var fnwi FileNameWithInfo
37 fileCreator := make([]byte, 4)
38 if !file.IsDir() {
39 fileType = []byte(fileTypeFromFilename(file.Name()).TypeCode)
40 fileCreator = []byte(fileTypeFromFilename(file.Name()).CreatorCode)
41
42 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(file.Size()))
43 copy(fnwi.Type[:], fileType[:])
44 copy(fnwi.Creator[:], fileCreator[:])
45 } else {
46 fileType = []byte("fldr")
47
48 dir, err := ioutil.ReadDir(filePath + "/" + file.Name())
49 if err != nil {
50 return fields, err
51 }
52 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(len(dir)))
53 copy(fnwi.Type[:], fileType[:])
54 copy(fnwi.Creator[:], fileCreator[:])
55 }
56
57 nameSize := make([]byte, 2)
58 binary.BigEndian.PutUint16(nameSize, uint16(len(file.Name())))
59 copy(fnwi.NameSize[:], nameSize[:])
60
61 fnwi.name = []byte(file.Name())
62
63 b, err := fnwi.MarshalBinary()
64 if err != nil {
65 return nil, err
66 }
67 fields = append(fields, NewField(fieldFileNameWithInfo, b))
68 }
69
70 return fields, nil
71 }
72
73 func CalcTotalSize(filePath string) ([]byte, error) {
74 var totalSize uint32
75 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
76 if err != nil {
77 return err
78 }
79
80 if info.IsDir() {
81 return nil
82 }
83
84 totalSize += uint32(info.Size())
85
86 return nil
87 })
88 if err != nil {
89 return nil, err
90 }
91
92 bs := make([]byte, 4)
93 binary.BigEndian.PutUint32(bs, totalSize)
94
95 return bs, nil
96 }
97
98 func CalcItemCount(filePath string) ([]byte, error) {
99 var itemcount uint16
100 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
101 itemcount += 1
102
103 if err != nil {
104 return err
105 }
106
107 return nil
108 })
109 if err != nil {
110 return nil, err
111 }
112
113 bs := make([]byte, 2)
114 binary.BigEndian.PutUint16(bs, itemcount-1)
115
116 return bs, nil
117 }
118
119 func EncodeFilePath(filePath string) []byte {
120 pathSections := strings.Split(filePath, "/")
121 pathItemCount := make([]byte, 2)
122 binary.BigEndian.PutUint16(pathItemCount, uint16(len(pathSections)))
123
124 bytes := pathItemCount
125
126 for _, section := range pathSections {
127 bytes = append(bytes, []byte{0, 0}...)
128
129 pathStr := []byte(section)
130 bytes = append(bytes, byte(len(pathStr)))
131 bytes = append(bytes, pathStr...)
132 }
133
134 return bytes
135 }