]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/files.go
Cleanup and backfill tests
[rbdr/mobius] / hotline / files.go
index bccab35868e0277ce50fad2c0ab25f2898951349..19375e9a831a83cfea1baad80d64b78a9a30ade3 100644 (file)
@@ -8,44 +8,24 @@ import (
        "strings"
 )
 
-const defaultCreator = "TTXT"
-const defaultType = "TEXT"
+func downcaseFileExtension(filename string) string {
+       splitStr := strings.Split(filename, ".")
+       ext := strings.ToLower(
+               splitStr[len(splitStr)-1],
+       )
 
-var fileCreatorCodes = map[string]string{
-       "sit": "SIT!",
-       "pdf": "CARO",
+       return ext
 }
 
-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
+func fileTypeFromFilename(fn string) fileType {
+       ft, ok := fileTypes[downcaseFileExtension(fn)]
+       if ok {
+               return ft
        }
-
-       return code
+       return defaultFileType
 }
 
-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
-
+func getFileNameList(filePath string) (fields []Field, err error) {
        files, err := ioutil.ReadDir(filePath)
        if err != nil {
                return fields, nil
@@ -55,10 +35,9 @@ func getFileNameList(filePath string) ([]Field, error) {
                var fileType []byte
                var fnwi FileNameWithInfo
                fileCreator := make([]byte, 4)
-               //fileSize := make([]byte, 4)
                if !file.IsDir() {
-                       fileType = []byte(fileTypeFromFilename(file.Name()))
-                       fileCreator = []byte(fileCreatorFromFilename(file.Name()))
+                       fileType = []byte(fileTypeFromFilename(file.Name()).TypeCode)
+                       fileCreator = []byte(fileTypeFromFilename(file.Name()).CreatorCode)
 
                        binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(file.Size()))
                        copy(fnwi.Type[:], fileType[:])
@@ -154,12 +133,3 @@ func EncodeFilePath(filePath string) []byte {
 
        return bytes
 }
-
-func ReadFilePath(filePathFieldData []byte) string {
-       var fp FilePath
-       err := fp.UnmarshalBinary(filePathFieldData)
-       if err != nil {
-               // TODO
-       }
-       return fp.String()
-}