]>
Commit | Line | Data |
---|---|---|
6988a057 JH |
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 { | |
43ecc0f4 JH |
55 | var fileType []byte |
56 | fileCreator := make([]byte, 4) | |
57 | fileSize := make([]byte, 4) | |
6988a057 | 58 | if !file.IsDir() { |
43ecc0f4 | 59 | fileType = []byte(fileTypeFromFilename(file.Name())) |
6988a057 | 60 | fileCreator = []byte(fileCreatorFromFilename(file.Name())) |
43ecc0f4 JH |
61 | |
62 | binary.BigEndian.PutUint32(fileSize, uint32(file.Size())) | |
6988a057 | 63 | } else { |
43ecc0f4 | 64 | fileType = []byte("fldr") |
6988a057 JH |
65 | |
66 | dir, err := ioutil.ReadDir(filePath + "/" + file.Name()) | |
67 | if err != nil { | |
68 | return fields, err | |
69 | } | |
43ecc0f4 | 70 | binary.BigEndian.PutUint32(fileSize, uint32(len(dir))) |
6988a057 JH |
71 | } |
72 | ||
73 | fields = append(fields, NewField( | |
74 | fieldFileNameWithInfo, | |
75 | FileNameWithInfo{ | |
76 | Type: fileType, | |
77 | Creator: fileCreator, | |
78 | FileSize: fileSize, | |
79 | NameScript: []byte{0, 0}, | |
43ecc0f4 | 80 | Name: []byte(file.Name()), |
6988a057 JH |
81 | }.Payload(), |
82 | )) | |
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 | func ReadFilePath(filePathFieldData []byte) string { | |
153 | fp := NewFilePath(filePathFieldData) | |
154 | return fp.String() | |
155 | } |