]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/files.go
Implement file preview
[rbdr/mobius] / hotline / files.go
index 0c233348c0a093af58383a590725ba76648efa71..478041ba21066453d8a30a308544f76739f6fb33 100644 (file)
@@ -2,45 +2,29 @@ package hotline
 
 import (
        "encoding/binary"
 
 import (
        "encoding/binary"
+       "errors"
+       "io/fs"
        "io/ioutil"
        "os"
        "path/filepath"
        "strings"
 )
 
        "io/ioutil"
        "os"
        "path/filepath"
        "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
-       }
-
-       return code
-}
-
-func fileCreatorFromFilename(fn string) string {
-       ext := strings.Split(fn, ".")
-       code := fileCreatorCodes[ext[len(ext)-1]]
-       if code == "" {
-               code = defaultCreator
+func fileTypeFromFilename(fn string) fileType {
+       ft, ok := fileTypes[downcaseFileExtension(fn)]
+       if ok {
+               return ft
        }
        }
-
-       return code
+       return defaultFileType
 }
 
 func getFileNameList(filePath string) (fields []Field, err error) {
 }
 
 func getFileNameList(filePath string) (fields []Field, err error) {
@@ -53,10 +37,9 @@ func getFileNameList(filePath string) (fields []Field, err error) {
                var fileType []byte
                var fnwi FileNameWithInfo
                fileCreator := make([]byte, 4)
                var fileType []byte
                var fnwi FileNameWithInfo
                fileCreator := make([]byte, 4)
-               //fileSize := make([]byte, 4)
                if !file.IsDir() {
                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[:])
 
                        binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(file.Size()))
                        copy(fnwi.Type[:], fileType[:])
@@ -73,11 +56,13 @@ func getFileNameList(filePath string) (fields []Field, err error) {
                        copy(fnwi.Creator[:], fileCreator[:])
                }
 
                        copy(fnwi.Creator[:], fileCreator[:])
                }
 
+               strippedName := strings.Replace(file.Name(), ".incomplete", "", -1)
+
                nameSize := make([]byte, 2)
                nameSize := make([]byte, 2)
-               binary.BigEndian.PutUint16(nameSize, uint16(len(file.Name())))
+               binary.BigEndian.PutUint16(nameSize, uint16(len(strippedName)))
                copy(fnwi.NameSize[:], nameSize[:])
 
                copy(fnwi.NameSize[:], nameSize[:])
 
-               fnwi.name = []byte(file.Name())
+               fnwi.name = []byte(strippedName)
 
                b, err := fnwi.MarshalBinary()
                if err != nil {
 
                b, err := fnwi.MarshalBinary()
                if err != nil {
@@ -152,3 +137,21 @@ func EncodeFilePath(filePath string) []byte {
 
        return bytes
 }
 
        return bytes
 }
+
+const incompleteFileSuffix = ".incomplete"
+
+// effectiveFile wraps os.Open to check for the presence of a partial file transfer as a fallback
+func effectiveFile(filePath string) (*os.File, error) {
+       file, err := os.Open(filePath)
+       if err != nil && !errors.Is(err, fs.ErrNotExist) {
+               return nil, err
+       }
+
+       if errors.Is(err, fs.ErrNotExist) {
+               file, err = os.OpenFile(filePath+incompleteFileSuffix, os.O_APPEND|os.O_WRONLY, 0644)
+               if err != nil {
+                       return nil, err
+               }
+       }
+       return file, nil
+}