aboutsummaryrefslogtreecommitdiff
path: root/files.go
blob: 69c22b33b9d0728f3ac7e11c70d699fa6aa57a20 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package hotline

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

const defaultCreator = "TTXT"
const defaultType = "TEXT"

var fileCreatorCodes = map[string]string{
	"sit": "SIT!",
	"pdf": "CARO",
}

var fileTypeCodes = map[string]string{
	"sit": "SIT!",
	"jpg": "JPEG",
	"pdf": "PDF ",
}

func fileTypeFromFilename(fn string) string {
	ext := strings.Split(fn, ".")
	code := fileTypeCodes[ext[len(ext)-1]]

	if code == "" {
		code = defaultType
	}

	return code
}

func fileCreatorFromFilename(fn string) string {
	ext := strings.Split(fn, ".")
	code := fileCreatorCodes[ext[len(ext)-1]]
	if code == "" {
		code = defaultCreator
	}

	return code
}

func getFileNameList(filePath string) ([]Field, error) {
	var fields []Field

	files, err := ioutil.ReadDir(filePath)
	if err != nil {
		return fields, nil
	}

	for _, file := range files {
		var fileType string
		var fileCreator []byte
		var fileSize uint32
		if !file.IsDir() {
			fileType = fileTypeFromFilename(file.Name())
			fileCreator = []byte(fileCreatorFromFilename(file.Name()))
			fileSize = uint32(file.Size())
		} else {
			fileType = "fldr"
			fileCreator = make([]byte, 4)

			dir, err := ioutil.ReadDir(filePath + "/" + file.Name())
			if err != nil {
				return fields, err
			}
			fileSize = uint32(len(dir))
		}

		fields = append(fields, NewField(
			fieldFileNameWithInfo,
			FileNameWithInfo{
				Type:       fileType,
				Creator:    fileCreator,
				FileSize:   fileSize,
				NameScript: []byte{0, 0},
				Name:       file.Name(),
			}.Payload(),
		))
	}

	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
}

func ReadFilePath(filePathFieldData []byte) string {
	fp := NewFilePath(filePathFieldData)
	return fp.String()
}