aboutsummaryrefslogtreecommitdiff
path: root/hotline/files.go
blob: 19375e9a831a83cfea1baad80d64b78a9a30ade3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package hotline

import (
	"encoding/binary"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
)

func downcaseFileExtension(filename string) string {
	splitStr := strings.Split(filename, ".")
	ext := strings.ToLower(
		splitStr[len(splitStr)-1],
	)

	return ext
}

func fileTypeFromFilename(fn string) fileType {
	ft, ok := fileTypes[downcaseFileExtension(fn)]
	if ok {
		return ft
	}
	return defaultFileType
}

func getFileNameList(filePath string) (fields []Field, err error) {
	files, err := ioutil.ReadDir(filePath)
	if err != nil {
		return fields, nil
	}

	for _, file := range files {
		var fileType []byte
		var fnwi FileNameWithInfo
		fileCreator := make([]byte, 4)
		if !file.IsDir() {
			fileType = []byte(fileTypeFromFilename(file.Name()).TypeCode)
			fileCreator = []byte(fileTypeFromFilename(file.Name()).CreatorCode)

			binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(file.Size()))
			copy(fnwi.Type[:], fileType[:])
			copy(fnwi.Creator[:], fileCreator[:])
		} else {
			fileType = []byte("fldr")

			dir, err := ioutil.ReadDir(filePath + "/" + file.Name())
			if err != nil {
				return fields, err
			}
			binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(len(dir)))
			copy(fnwi.Type[:], fileType[:])
			copy(fnwi.Creator[:], fileCreator[:])
		}

		nameSize := make([]byte, 2)
		binary.BigEndian.PutUint16(nameSize, uint16(len(file.Name())))
		copy(fnwi.NameSize[:], nameSize[:])

		fnwi.name = []byte(file.Name())

		b, err := fnwi.MarshalBinary()
		if err != nil {
			return nil, err
		}
		fields = append(fields, NewField(fieldFileNameWithInfo, b))
	}

	return fields, nil
}

func CalcTotalSize(filePath string) ([]byte, error) {
	var totalSize uint32
	err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}

		if info.IsDir() {
			return nil
		}

		totalSize += uint32(info.Size())

		return nil
	})
	if err != nil {
		return nil, err
	}

	bs := make([]byte, 4)
	binary.BigEndian.PutUint32(bs, totalSize)

	return bs, nil
}

func CalcItemCount(filePath string) ([]byte, error) {
	var itemcount uint16
	err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
		itemcount += 1

		if err != nil {
			return err
		}

		return nil
	})
	if err != nil {
		return nil, err
	}

	bs := make([]byte, 2)
	binary.BigEndian.PutUint16(bs, itemcount-1)

	return bs, nil
}

func EncodeFilePath(filePath string) []byte {
	pathSections := strings.Split(filePath, "/")
	pathItemCount := make([]byte, 2)
	binary.BigEndian.PutUint16(pathItemCount, uint16(len(pathSections)))

	bytes := pathItemCount

	for _, section := range pathSections {
		bytes = append(bytes, []byte{0, 0}...)

		pathStr := []byte(section)
		bytes = append(bytes, byte(len(pathStr)))
		bytes = append(bytes, pathStr...)
	}

	return bytes
}