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