]> git.r.bdr.sh - rbdr/mobius/blob - hotline/files.go
Initial implementation of file transfer resume
[rbdr/mobius] / hotline / files.go
1 package hotline
2
3 import (
4 "encoding/binary"
5 "errors"
6 "io/fs"
7 "io/ioutil"
8 "os"
9 "path/filepath"
10 "strings"
11 )
12
13 func downcaseFileExtension(filename string) string {
14 splitStr := strings.Split(filename, ".")
15 ext := strings.ToLower(
16 splitStr[len(splitStr)-1],
17 )
18
19 return ext
20 }
21
22 func fileTypeFromFilename(fn string) fileType {
23 ft, ok := fileTypes[downcaseFileExtension(fn)]
24 if ok {
25 return ft
26 }
27 return defaultFileType
28 }
29
30 func getFileNameList(filePath string) (fields []Field, err error) {
31 files, err := ioutil.ReadDir(filePath)
32 if err != nil {
33 return fields, nil
34 }
35
36 for _, file := range files {
37 var fileType []byte
38 var fnwi FileNameWithInfo
39 fileCreator := make([]byte, 4)
40 if !file.IsDir() {
41 fileType = []byte(fileTypeFromFilename(file.Name()).TypeCode)
42 fileCreator = []byte(fileTypeFromFilename(file.Name()).CreatorCode)
43
44 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(file.Size()))
45 copy(fnwi.Type[:], fileType[:])
46 copy(fnwi.Creator[:], fileCreator[:])
47 } else {
48 fileType = []byte("fldr")
49
50 dir, err := ioutil.ReadDir(filePath + "/" + file.Name())
51 if err != nil {
52 return fields, err
53 }
54 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(len(dir)))
55 copy(fnwi.Type[:], fileType[:])
56 copy(fnwi.Creator[:], fileCreator[:])
57 }
58
59 strippedName := strings.Replace(file.Name(), ".incomplete", "", -1)
60
61 nameSize := make([]byte, 2)
62 binary.BigEndian.PutUint16(nameSize, uint16(len(strippedName)))
63 copy(fnwi.NameSize[:], nameSize[:])
64
65 fnwi.name = []byte(strippedName)
66
67 b, err := fnwi.MarshalBinary()
68 if err != nil {
69 return nil, err
70 }
71 fields = append(fields, NewField(fieldFileNameWithInfo, b))
72 }
73
74 return fields, nil
75 }
76
77 func CalcTotalSize(filePath string) ([]byte, error) {
78 var totalSize uint32
79 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
80 if err != nil {
81 return err
82 }
83
84 if info.IsDir() {
85 return nil
86 }
87
88 totalSize += uint32(info.Size())
89
90 return nil
91 })
92 if err != nil {
93 return nil, err
94 }
95
96 bs := make([]byte, 4)
97 binary.BigEndian.PutUint32(bs, totalSize)
98
99 return bs, nil
100 }
101
102 func CalcItemCount(filePath string) ([]byte, error) {
103 var itemcount uint16
104 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
105 itemcount += 1
106
107 if err != nil {
108 return err
109 }
110
111 return nil
112 })
113 if err != nil {
114 return nil, err
115 }
116
117 bs := make([]byte, 2)
118 binary.BigEndian.PutUint16(bs, itemcount-1)
119
120 return bs, nil
121 }
122
123 func EncodeFilePath(filePath string) []byte {
124 pathSections := strings.Split(filePath, "/")
125 pathItemCount := make([]byte, 2)
126 binary.BigEndian.PutUint16(pathItemCount, uint16(len(pathSections)))
127
128 bytes := pathItemCount
129
130 for _, section := range pathSections {
131 bytes = append(bytes, []byte{0, 0}...)
132
133 pathStr := []byte(section)
134 bytes = append(bytes, byte(len(pathStr)))
135 bytes = append(bytes, pathStr...)
136 }
137
138 return bytes
139 }
140
141 const incompleteFileSuffix = ".incomplete"
142
143 // effectiveFile wraps os.Open to check for the presence of a partial file transfer as a fallback
144 func effectiveFile(filePath string) (*os.File, error) {
145 file, err := os.Open(filePath)
146 if err != nil && !errors.Is(err, fs.ErrNotExist) {
147 return nil, err
148 }
149
150 if errors.Is(err, fs.ErrNotExist) {
151 file, err = os.OpenFile(filePath+incompleteFileSuffix, os.O_APPEND|os.O_WRONLY, 0644)
152 if err != nil {
153 return nil, err
154 }
155 }
156 return file, nil
157 }