]>
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 | ||
2728d12b JH |
11 | func downcaseFileExtension(filename string) string { |
12 | splitStr := strings.Split(filename, ".") | |
13 | ext := strings.ToLower( | |
14 | splitStr[len(splitStr)-1], | |
15 | ) | |
6988a057 | 16 | |
2728d12b | 17 | return ext |
6988a057 JH |
18 | } |
19 | ||
2728d12b JH |
20 | func fileTypeFromFilename(fn string) fileType { |
21 | ft, ok := fileTypes[downcaseFileExtension(fn)] | |
22 | if ok { | |
23 | return ft | |
6988a057 | 24 | } |
2728d12b | 25 | return defaultFileType |
6988a057 JH |
26 | } |
27 | ||
c5d9af5a | 28 | func getFileNameList(filePath string) (fields []Field, err error) { |
6988a057 JH |
29 | files, err := ioutil.ReadDir(filePath) |
30 | if err != nil { | |
31 | return fields, nil | |
32 | } | |
33 | ||
34 | for _, file := range files { | |
43ecc0f4 | 35 | var fileType []byte |
72dd37f1 | 36 | var fnwi FileNameWithInfo |
43ecc0f4 | 37 | fileCreator := make([]byte, 4) |
6988a057 | 38 | if !file.IsDir() { |
2728d12b JH |
39 | fileType = []byte(fileTypeFromFilename(file.Name()).TypeCode) |
40 | fileCreator = []byte(fileTypeFromFilename(file.Name()).CreatorCode) | |
43ecc0f4 | 41 | |
72dd37f1 JH |
42 | binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(file.Size())) |
43 | copy(fnwi.Type[:], fileType[:]) | |
44 | copy(fnwi.Creator[:], fileCreator[:]) | |
6988a057 | 45 | } else { |
43ecc0f4 | 46 | fileType = []byte("fldr") |
6988a057 JH |
47 | |
48 | dir, err := ioutil.ReadDir(filePath + "/" + file.Name()) | |
49 | if err != nil { | |
50 | return fields, err | |
51 | } | |
72dd37f1 JH |
52 | binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(len(dir))) |
53 | copy(fnwi.Type[:], fileType[:]) | |
54 | copy(fnwi.Creator[:], fileCreator[:]) | |
6988a057 JH |
55 | } |
56 | ||
72dd37f1 JH |
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)) | |
6988a057 JH |
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 | } |