]>
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 | ||
c5d9af5a | 46 | func getFileNameList(filePath string) (fields []Field, err error) { |
6988a057 JH |
47 | files, err := ioutil.ReadDir(filePath) |
48 | if err != nil { | |
49 | return fields, nil | |
50 | } | |
51 | ||
52 | for _, file := range files { | |
43ecc0f4 | 53 | var fileType []byte |
72dd37f1 | 54 | var fnwi FileNameWithInfo |
43ecc0f4 | 55 | fileCreator := make([]byte, 4) |
72dd37f1 | 56 | //fileSize := make([]byte, 4) |
6988a057 | 57 | if !file.IsDir() { |
43ecc0f4 | 58 | fileType = []byte(fileTypeFromFilename(file.Name())) |
6988a057 | 59 | fileCreator = []byte(fileCreatorFromFilename(file.Name())) |
43ecc0f4 | 60 | |
72dd37f1 JH |
61 | binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(file.Size())) |
62 | copy(fnwi.Type[:], fileType[:]) | |
63 | copy(fnwi.Creator[:], fileCreator[:]) | |
6988a057 | 64 | } else { |
43ecc0f4 | 65 | fileType = []byte("fldr") |
6988a057 JH |
66 | |
67 | dir, err := ioutil.ReadDir(filePath + "/" + file.Name()) | |
68 | if err != nil { | |
69 | return fields, err | |
70 | } | |
72dd37f1 JH |
71 | binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(len(dir))) |
72 | copy(fnwi.Type[:], fileType[:]) | |
73 | copy(fnwi.Creator[:], fileCreator[:]) | |
6988a057 JH |
74 | } |
75 | ||
72dd37f1 JH |
76 | nameSize := make([]byte, 2) |
77 | binary.BigEndian.PutUint16(nameSize, uint16(len(file.Name()))) | |
78 | copy(fnwi.NameSize[:], nameSize[:]) | |
79 | ||
80 | fnwi.name = []byte(file.Name()) | |
81 | ||
82 | b, err := fnwi.MarshalBinary() | |
83 | if err != nil { | |
84 | return nil, err | |
85 | } | |
86 | fields = append(fields, NewField(fieldFileNameWithInfo, b)) | |
6988a057 JH |
87 | } |
88 | ||
89 | return fields, nil | |
90 | } | |
91 | ||
92 | func CalcTotalSize(filePath string) ([]byte, error) { | |
93 | var totalSize uint32 | |
94 | err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error { | |
95 | if err != nil { | |
96 | return err | |
97 | } | |
98 | ||
99 | if info.IsDir() { | |
100 | return nil | |
101 | } | |
102 | ||
103 | totalSize += uint32(info.Size()) | |
104 | ||
105 | return nil | |
106 | }) | |
107 | if err != nil { | |
108 | return nil, err | |
109 | } | |
110 | ||
111 | bs := make([]byte, 4) | |
112 | binary.BigEndian.PutUint32(bs, totalSize) | |
113 | ||
114 | return bs, nil | |
115 | } | |
116 | ||
117 | func CalcItemCount(filePath string) ([]byte, error) { | |
118 | var itemcount uint16 | |
119 | err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error { | |
120 | itemcount += 1 | |
121 | ||
122 | if err != nil { | |
123 | return err | |
124 | } | |
125 | ||
126 | return nil | |
127 | }) | |
128 | if err != nil { | |
129 | return nil, err | |
130 | } | |
131 | ||
132 | bs := make([]byte, 2) | |
133 | binary.BigEndian.PutUint16(bs, itemcount-1) | |
134 | ||
135 | return bs, nil | |
136 | } | |
137 | ||
138 | func EncodeFilePath(filePath string) []byte { | |
139 | pathSections := strings.Split(filePath, "/") | |
140 | pathItemCount := make([]byte, 2) | |
141 | binary.BigEndian.PutUint16(pathItemCount, uint16(len(pathSections))) | |
142 | ||
143 | bytes := pathItemCount | |
144 | ||
145 | for _, section := range pathSections { | |
146 | bytes = append(bytes, []byte{0, 0}...) | |
147 | ||
148 | pathStr := []byte(section) | |
149 | bytes = append(bytes, byte(len(pathStr))) | |
150 | bytes = append(bytes, pathStr...) | |
151 | } | |
152 | ||
153 | return bytes | |
154 | } |