diff options
| author | Jeff Halter <868228+jhalter@users.noreply.github.com> | 2021-07-28 18:21:52 -0700 |
|---|---|---|
| committer | Jeff Halter <868228+jhalter@users.noreply.github.com> | 2021-07-28 14:21:52 -0700 |
| commit | 22c599abc18895f73e96095f35b71cf3357d41b4 (patch) | |
| tree | 482ef57d386c955692ea43c43e4655b3c3763499 /hotline/files.go | |
| parent | 71c56068adca18f76ebee86355f000a3e51d3127 (diff) | |
Move code to hotline dir
Diffstat (limited to 'hotline/files.go')
| -rw-r--r-- | hotline/files.go | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/hotline/files.go b/hotline/files.go new file mode 100644 index 0000000..69c22b3 --- /dev/null +++ b/hotline/files.go @@ -0,0 +1,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() +} |