]>
Commit | Line | Data |
---|---|---|
1 | package hotline | |
2 | ||
3 | import ( | |
4 | "bufio" | |
5 | "crypto/rand" | |
6 | "encoding/binary" | |
7 | "errors" | |
8 | "fmt" | |
9 | "io" | |
10 | "io/fs" | |
11 | "log/slog" | |
12 | "math" | |
13 | "os" | |
14 | "path/filepath" | |
15 | "slices" | |
16 | "strings" | |
17 | "sync" | |
18 | ) | |
19 | ||
20 | // Folder download actions. Send by the client to indicate the next action the server should take | |
21 | // for a folder download. | |
22 | const ( | |
23 | DlFldrActionSendFile = 1 | |
24 | DlFldrActionResumeFile = 2 | |
25 | DlFldrActionNextFile = 3 | |
26 | ) | |
27 | ||
28 | // File transfer types | |
29 | type FileTransferType uint8 | |
30 | ||
31 | const ( | |
32 | FileDownload = FileTransferType(0) | |
33 | FileUpload = FileTransferType(1) | |
34 | FolderDownload = FileTransferType(2) | |
35 | FolderUpload = FileTransferType(3) | |
36 | BannerDownload = FileTransferType(4) | |
37 | ) | |
38 | ||
39 | type FileTransferID [4]byte | |
40 | ||
41 | type FileTransferMgr interface { | |
42 | Add(ft *FileTransfer) | |
43 | Get(id FileTransferID) *FileTransfer | |
44 | Delete(id FileTransferID) | |
45 | } | |
46 | ||
47 | type MemFileTransferMgr struct { | |
48 | fileTransfers map[FileTransferID]*FileTransfer | |
49 | ||
50 | mu sync.Mutex | |
51 | } | |
52 | ||
53 | func NewMemFileTransferMgr() *MemFileTransferMgr { | |
54 | return &MemFileTransferMgr{ | |
55 | fileTransfers: make(map[FileTransferID]*FileTransfer), | |
56 | } | |
57 | } | |
58 | ||
59 | func (ftm *MemFileTransferMgr) Add(ft *FileTransfer) { | |
60 | ftm.mu.Lock() | |
61 | defer ftm.mu.Unlock() | |
62 | ||
63 | _, _ = rand.Read(ft.RefNum[:]) | |
64 | ||
65 | ftm.fileTransfers[ft.RefNum] = ft | |
66 | ||
67 | ft.ClientConn.ClientFileTransferMgr.Add(ft.Type, ft) | |
68 | } | |
69 | ||
70 | func (ftm *MemFileTransferMgr) Get(id FileTransferID) *FileTransfer { | |
71 | ftm.mu.Lock() | |
72 | defer ftm.mu.Unlock() | |
73 | ||
74 | return ftm.fileTransfers[id] | |
75 | } | |
76 | ||
77 | func (ftm *MemFileTransferMgr) Delete(id FileTransferID) { | |
78 | ftm.mu.Lock() | |
79 | defer ftm.mu.Unlock() | |
80 | ||
81 | ft := ftm.fileTransfers[id] | |
82 | ||
83 | ft.ClientConn.ClientFileTransferMgr.Delete(ft.Type, id) | |
84 | ||
85 | delete(ftm.fileTransfers, id) | |
86 | ||
87 | } | |
88 | ||
89 | type FileTransfer struct { | |
90 | FileRoot string | |
91 | FileName []byte | |
92 | FilePath []byte | |
93 | RefNum [4]byte | |
94 | Type FileTransferType | |
95 | TransferSize []byte | |
96 | FolderItemCount []byte | |
97 | FileResumeData *FileResumeData | |
98 | Options []byte | |
99 | bytesSentCounter *WriteCounter | |
100 | ClientConn *ClientConn | |
101 | } | |
102 | ||
103 | // WriteCounter counts the number of bytes written to it. | |
104 | type WriteCounter struct { | |
105 | mux sync.Mutex | |
106 | Total int64 // Total # of bytes written | |
107 | } | |
108 | ||
109 | // Write implements the io.Writer interface. | |
110 | // | |
111 | // Always completes and never returns an error. | |
112 | func (wc *WriteCounter) Write(p []byte) (int, error) { | |
113 | wc.mux.Lock() | |
114 | defer wc.mux.Unlock() | |
115 | n := len(p) | |
116 | wc.Total += int64(n) | |
117 | return n, nil | |
118 | } | |
119 | ||
120 | func (cc *ClientConn) NewFileTransfer(transferType FileTransferType, fileroot string, fileName, filePath, size []byte) *FileTransfer { | |
121 | ft := &FileTransfer{ | |
122 | FileName: fileName, | |
123 | FileRoot: fileroot, | |
124 | FilePath: filePath, | |
125 | Type: transferType, | |
126 | TransferSize: size, | |
127 | ClientConn: cc, | |
128 | bytesSentCounter: &WriteCounter{}, | |
129 | } | |
130 | ||
131 | cc.Server.FileTransferMgr.Add(ft) | |
132 | ||
133 | return ft | |
134 | } | |
135 | ||
136 | // String returns a string representation of a file transfer and its progress for display in the GetInfo window | |
137 | // Example: | |
138 | // MasterOfOrionII1.4.0. 0% 197.9M | |
139 | func (ft *FileTransfer) String() string { | |
140 | trunc := fmt.Sprintf("%.21s", ft.FileName) | |
141 | return fmt.Sprintf("%-21s %.3s%% %6s\n", trunc, ft.percentComplete(), ft.formattedTransferSize()) | |
142 | } | |
143 | ||
144 | func (ft *FileTransfer) percentComplete() string { | |
145 | ft.bytesSentCounter.mux.Lock() | |
146 | defer ft.bytesSentCounter.mux.Unlock() | |
147 | return fmt.Sprintf( | |
148 | "%v", | |
149 | math.RoundToEven(float64(ft.bytesSentCounter.Total)/float64(binary.BigEndian.Uint32(ft.TransferSize))*100), | |
150 | ) | |
151 | } | |
152 | ||
153 | func (ft *FileTransfer) formattedTransferSize() string { | |
154 | sizeInKB := float32(binary.BigEndian.Uint32(ft.TransferSize)) / 1024 | |
155 | if sizeInKB >= 1024 { | |
156 | return fmt.Sprintf("%.1fM", sizeInKB/1024) | |
157 | } else { | |
158 | return fmt.Sprintf("%.0fK", sizeInKB) | |
159 | } | |
160 | } | |
161 | ||
162 | func (ft *FileTransfer) ItemCount() int { | |
163 | return int(binary.BigEndian.Uint16(ft.FolderItemCount)) | |
164 | } | |
165 | ||
166 | type folderUpload struct { | |
167 | DataSize [2]byte | |
168 | IsFolder [2]byte | |
169 | PathItemCount [2]byte | |
170 | FileNamePath []byte | |
171 | } | |
172 | ||
173 | //func (fu *folderUpload) Write(p []byte) (int, error) { | |
174 | // if len(p) < 7 { | |
175 | // return 0, errors.New("buflen too short") | |
176 | // } | |
177 | // copy(fu.DataSize[:], p[0:2]) | |
178 | // copy(fu.IsFolder[:], p[2:4]) | |
179 | // copy(fu.PathItemCount[:], p[4:6]) | |
180 | // | |
181 | // fu.FileNamePath = make([]byte, binary.BigEndian.Uint16(fu.DataSize[:])-4) // -4 to subtract the path separator bytes TODO: wat | |
182 | // n, err := io.ReadFull(rwc, fu.FileNamePath) | |
183 | // if err != nil { | |
184 | // return 0, err | |
185 | // } | |
186 | // | |
187 | // return n + 6, nil | |
188 | //} | |
189 | ||
190 | func (fu *folderUpload) FormattedPath() string { | |
191 | pathItemLen := binary.BigEndian.Uint16(fu.PathItemCount[:]) | |
192 | ||
193 | var pathSegments []string | |
194 | pathData := fu.FileNamePath | |
195 | ||
196 | // TODO: implement scanner interface instead? | |
197 | for i := uint16(0); i < pathItemLen; i++ { | |
198 | segLen := pathData[2] | |
199 | pathSegments = append(pathSegments, string(pathData[3:3+segLen])) | |
200 | pathData = pathData[3+segLen:] | |
201 | } | |
202 | ||
203 | return filepath.Join(pathSegments...) | |
204 | } | |
205 | ||
206 | type FileHeader struct { | |
207 | Size [2]byte // Total size of FileHeader payload | |
208 | Type [2]byte // 0 for file, 1 for dir | |
209 | FilePath []byte // encoded file path | |
210 | ||
211 | readOffset int // Internal offset to track read progress | |
212 | } | |
213 | ||
214 | func NewFileHeader(fileName string, isDir bool) FileHeader { | |
215 | fh := FileHeader{ | |
216 | FilePath: EncodeFilePath(fileName), | |
217 | } | |
218 | if isDir { | |
219 | fh.Type = [2]byte{0x00, 0x01} | |
220 | } | |
221 | ||
222 | encodedPathLen := uint16(len(fh.FilePath) + len(fh.Type)) | |
223 | binary.BigEndian.PutUint16(fh.Size[:], encodedPathLen) | |
224 | ||
225 | return fh | |
226 | } | |
227 | ||
228 | func (fh *FileHeader) Read(p []byte) (int, error) { | |
229 | buf := slices.Concat( | |
230 | fh.Size[:], | |
231 | fh.Type[:], | |
232 | fh.FilePath, | |
233 | ) | |
234 | ||
235 | if fh.readOffset >= len(buf) { | |
236 | return 0, io.EOF // All bytes have been read | |
237 | } | |
238 | ||
239 | n := copy(p, buf[fh.readOffset:]) | |
240 | fh.readOffset += n | |
241 | ||
242 | return n, nil | |
243 | } | |
244 | ||
245 | func DownloadHandler(w io.Writer, fullPath string, fileTransfer *FileTransfer, fs FileStore, rLogger *slog.Logger, preserveForks bool) error { | |
246 | //s.Stats.DownloadCounter += 1 | |
247 | //s.Stats.DownloadsInProgress += 1 | |
248 | //defer func() { | |
249 | // s.Stats.DownloadsInProgress -= 1 | |
250 | //}() | |
251 | ||
252 | var dataOffset int64 | |
253 | if fileTransfer.FileResumeData != nil { | |
254 | dataOffset = int64(binary.BigEndian.Uint32(fileTransfer.FileResumeData.ForkInfoList[0].DataSize[:])) | |
255 | } | |
256 | ||
257 | fw, err := NewFileWrapper(fs, fullPath, 0) | |
258 | if err != nil { | |
259 | return fmt.Errorf("reading file header: %v", err) | |
260 | } | |
261 | ||
262 | rLogger.Info("Download file", "filePath", fullPath) | |
263 | ||
264 | // If file transfer options are included, that means this is a "quick preview" request. In this case skip sending | |
265 | // the flat file info and proceed directly to sending the file data. | |
266 | if fileTransfer.Options == nil { | |
267 | if _, err = io.Copy(w, fw.Ffo); err != nil { | |
268 | return fmt.Errorf("send flat file object: %v", err) | |
269 | } | |
270 | } | |
271 | ||
272 | file, err := fw.dataForkReader() | |
273 | if err != nil { | |
274 | return fmt.Errorf("open data fork reader: %v", err) | |
275 | } | |
276 | ||
277 | br := bufio.NewReader(file) | |
278 | if _, err := br.Discard(int(dataOffset)); err != nil { | |
279 | return fmt.Errorf("seek to resume offsent: %v", err) | |
280 | } | |
281 | ||
282 | if _, err = io.Copy(w, io.TeeReader(br, fileTransfer.bytesSentCounter)); err != nil { | |
283 | return fmt.Errorf("send data fork: %v", err) | |
284 | } | |
285 | ||
286 | // If the client requested to resume transfer, do not send the resource fork header. | |
287 | if fileTransfer.FileResumeData == nil { | |
288 | err = binary.Write(w, binary.BigEndian, fw.rsrcForkHeader()) | |
289 | if err != nil { | |
290 | return fmt.Errorf("send resource fork header: %v", err) | |
291 | } | |
292 | } | |
293 | ||
294 | rFile, _ := fw.rsrcForkFile() | |
295 | //if err != nil { | |
296 | // // return fmt.Errorf("open resource fork file: %v", err) | |
297 | //} | |
298 | ||
299 | _, _ = io.Copy(w, io.TeeReader(rFile, fileTransfer.bytesSentCounter)) | |
300 | //if err != nil { | |
301 | // // return fmt.Errorf("send resource fork data: %v", err) | |
302 | //} | |
303 | ||
304 | return nil | |
305 | } | |
306 | ||
307 | func UploadHandler(rwc io.ReadWriter, fullPath string, fileTransfer *FileTransfer, fileStore FileStore, rLogger *slog.Logger, preserveForks bool) error { | |
308 | var file *os.File | |
309 | ||
310 | // A file upload has two possible cases: | |
311 | // 1) Upload a new file | |
312 | // 2) Resume a partially transferred file | |
313 | // We have to infer which case applies by inspecting what is already on the filesystem | |
314 | ||
315 | // Check for existing file. If found, do not proceed. This is an invalid scenario, as the file upload transaction | |
316 | // handler should have returned an error to the client indicating there was an existing file present. | |
317 | _, err := os.Stat(fullPath) | |
318 | if err == nil { | |
319 | return fmt.Errorf("existing file found: %s", fullPath) | |
320 | } | |
321 | if errors.Is(err, fs.ErrNotExist) { | |
322 | // If not found, open or create a new .incomplete file | |
323 | file, err = os.OpenFile(fullPath+IncompleteFileSuffix, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) | |
324 | if err != nil { | |
325 | return err | |
326 | } | |
327 | } | |
328 | ||
329 | f, err := NewFileWrapper(fileStore, fullPath, 0) | |
330 | if err != nil { | |
331 | return err | |
332 | } | |
333 | ||
334 | rLogger.Info("File upload started", "dstFile", fullPath) | |
335 | ||
336 | rForkWriter := io.Discard | |
337 | iForkWriter := io.Discard | |
338 | if preserveForks { | |
339 | rForkWriter, err = f.rsrcForkWriter() | |
340 | if err != nil { | |
341 | return err | |
342 | } | |
343 | ||
344 | iForkWriter, err = f.InfoForkWriter() | |
345 | if err != nil { | |
346 | return err | |
347 | } | |
348 | } | |
349 | ||
350 | if err := receiveFile(rwc, file, rForkWriter, iForkWriter, fileTransfer.bytesSentCounter); err != nil { | |
351 | rLogger.Error(err.Error()) | |
352 | } | |
353 | ||
354 | if err := file.Close(); err != nil { | |
355 | return err | |
356 | } | |
357 | ||
358 | if err := fileStore.Rename(fullPath+".incomplete", fullPath); err != nil { | |
359 | return err | |
360 | } | |
361 | ||
362 | rLogger.Info("File upload complete", "dstFile", fullPath) | |
363 | ||
364 | return nil | |
365 | } | |
366 | ||
367 | func DownloadFolderHandler(rwc io.ReadWriter, fullPath string, fileTransfer *FileTransfer, fileStore FileStore, rLogger *slog.Logger, preserveForks bool) error { | |
368 | // Folder Download flow: | |
369 | // 1. Get filePath from the transfer | |
370 | // 2. Iterate over files | |
371 | // 3. For each fileWrapper: | |
372 | // Send fileWrapper header to client | |
373 | // The client can reply in 3 ways: | |
374 | // | |
375 | // 1. If type is an odd number (unknown type?), or fileWrapper download for the current fileWrapper is completed: | |
376 | // client sends []byte{0x00, 0x03} to tell the server to continue to the next fileWrapper | |
377 | // | |
378 | // 2. If download of a fileWrapper is to be resumed: | |
379 | // client sends: | |
380 | // []byte{0x00, 0x02} // download folder action | |
381 | // [2]byte // Resume data size | |
382 | // []byte fileWrapper resume data (see myField_FileResumeData) | |
383 | // | |
384 | // 3. Otherwise, download of the fileWrapper is requested and client sends []byte{0x00, 0x01} | |
385 | // | |
386 | // When download is requested (case 2 or 3), server replies with: | |
387 | // [4]byte - fileWrapper size | |
388 | // []byte - Flattened File Object | |
389 | // | |
390 | // After every fileWrapper download, client could request next fileWrapper with: | |
391 | // []byte{0x00, 0x03} | |
392 | // | |
393 | // This notifies the server to send the next item header | |
394 | ||
395 | basePathLen := len(fullPath) | |
396 | ||
397 | rLogger.Info("Start folder download", "path", fullPath) | |
398 | ||
399 | nextAction := make([]byte, 2) | |
400 | if _, err := io.ReadFull(rwc, nextAction); err != nil { | |
401 | return err | |
402 | } | |
403 | ||
404 | i := 0 | |
405 | err := filepath.Walk(fullPath+"/", func(path string, info os.FileInfo, err error) error { | |
406 | //s.Stats.DownloadCounter += 1 | |
407 | i += 1 | |
408 | ||
409 | if err != nil { | |
410 | return err | |
411 | } | |
412 | ||
413 | // skip dot files | |
414 | if strings.HasPrefix(info.Name(), ".") { | |
415 | return nil | |
416 | } | |
417 | ||
418 | hlFile, err := NewFileWrapper(fileStore, path, 0) | |
419 | if err != nil { | |
420 | return err | |
421 | } | |
422 | ||
423 | subPath := path[basePathLen+1:] | |
424 | ||
425 | if i == 1 { | |
426 | return nil | |
427 | } | |
428 | ||
429 | fileHeader := NewFileHeader(subPath, info.IsDir()) | |
430 | if _, err := io.Copy(rwc, &fileHeader); err != nil { | |
431 | return fmt.Errorf("error sending file header: %w", err) | |
432 | } | |
433 | ||
434 | // Read the client's Next Action request | |
435 | if _, err := io.ReadFull(rwc, nextAction); err != nil { | |
436 | return err | |
437 | } | |
438 | ||
439 | var dataOffset int64 | |
440 | ||
441 | switch nextAction[1] { | |
442 | case DlFldrActionResumeFile: | |
443 | // get size of resumeData | |
444 | resumeDataByteLen := make([]byte, 2) | |
445 | if _, err := io.ReadFull(rwc, resumeDataByteLen); err != nil { | |
446 | return err | |
447 | } | |
448 | ||
449 | resumeDataLen := binary.BigEndian.Uint16(resumeDataByteLen) | |
450 | resumeDataBytes := make([]byte, resumeDataLen) | |
451 | if _, err := io.ReadFull(rwc, resumeDataBytes); err != nil { | |
452 | return err | |
453 | } | |
454 | ||
455 | var frd FileResumeData | |
456 | if err := frd.UnmarshalBinary(resumeDataBytes); err != nil { | |
457 | return err | |
458 | } | |
459 | dataOffset = int64(binary.BigEndian.Uint32(frd.ForkInfoList[0].DataSize[:])) | |
460 | case DlFldrActionNextFile: | |
461 | // client asked to skip this file | |
462 | return nil | |
463 | } | |
464 | ||
465 | if info.IsDir() { | |
466 | return nil | |
467 | } | |
468 | ||
469 | rLogger.Info("File download started", | |
470 | "fileName", info.Name(), | |
471 | "TransferSize", fmt.Sprintf("%x", hlFile.Ffo.TransferSize(dataOffset)), | |
472 | ) | |
473 | ||
474 | // Send file size to client | |
475 | if _, err := rwc.Write(hlFile.Ffo.TransferSize(dataOffset)); err != nil { | |
476 | rLogger.Error(err.Error()) | |
477 | return fmt.Errorf("error sending file size: %w", err) | |
478 | } | |
479 | ||
480 | // Send ffo bytes to client | |
481 | _, err = io.Copy(rwc, hlFile.Ffo) | |
482 | if err != nil { | |
483 | return fmt.Errorf("error sending flat file object: %w", err) | |
484 | } | |
485 | ||
486 | file, err := fileStore.Open(path) | |
487 | if err != nil { | |
488 | return fmt.Errorf("error opening file: %w", err) | |
489 | } | |
490 | ||
491 | // wr := bufio.NewWriterSize(rwc, 1460) | |
492 | if _, err = io.Copy(rwc, io.TeeReader(file, fileTransfer.bytesSentCounter)); err != nil { | |
493 | return fmt.Errorf("error sending file: %w", err) | |
494 | } | |
495 | ||
496 | if nextAction[1] != 2 && hlFile.Ffo.FlatFileHeader.ForkCount[1] == 3 { | |
497 | err = binary.Write(rwc, binary.BigEndian, hlFile.rsrcForkHeader()) | |
498 | if err != nil { | |
499 | return fmt.Errorf("error sending resource fork header: %w", err) | |
500 | } | |
501 | ||
502 | rFile, err := hlFile.rsrcForkFile() | |
503 | if err != nil { | |
504 | return fmt.Errorf("error opening resource fork: %w", err) | |
505 | } | |
506 | ||
507 | if _, err = io.Copy(rwc, io.TeeReader(rFile, fileTransfer.bytesSentCounter)); err != nil { | |
508 | return fmt.Errorf("error sending resource fork: %w", err) | |
509 | } | |
510 | } | |
511 | ||
512 | // Read the client's Next Action request. This is always 3, I think? | |
513 | if _, err := io.ReadFull(rwc, nextAction); err != nil && err != io.EOF { | |
514 | return fmt.Errorf("error reading client next action: %w", err) | |
515 | } | |
516 | ||
517 | return nil | |
518 | }) | |
519 | ||
520 | if err != nil { | |
521 | return err | |
522 | } | |
523 | ||
524 | return nil | |
525 | } | |
526 | ||
527 | func UploadFolderHandler(rwc io.ReadWriter, fullPath string, fileTransfer *FileTransfer, fileStore FileStore, rLogger *slog.Logger, preserveForks bool) error { | |
528 | ||
529 | // Check if the target folder exists. If not, create it. | |
530 | if _, err := fileStore.Stat(fullPath); os.IsNotExist(err) { | |
531 | if err := fileStore.Mkdir(fullPath, 0777); err != nil { | |
532 | return err | |
533 | } | |
534 | } | |
535 | ||
536 | // Begin the folder upload flow by sending the "next file action" to client | |
537 | if _, err := rwc.Write([]byte{0, DlFldrActionNextFile}); err != nil { | |
538 | return err | |
539 | } | |
540 | ||
541 | fileSize := make([]byte, 4) | |
542 | ||
543 | for i := 0; i < fileTransfer.ItemCount(); i++ { | |
544 | //s.Stats.UploadCounter += 1 | |
545 | ||
546 | var fu folderUpload | |
547 | // TODO: implement io.Writer on folderUpload and replace this | |
548 | if _, err := io.ReadFull(rwc, fu.DataSize[:]); err != nil { | |
549 | return err | |
550 | } | |
551 | if _, err := io.ReadFull(rwc, fu.IsFolder[:]); err != nil { | |
552 | return err | |
553 | } | |
554 | if _, err := io.ReadFull(rwc, fu.PathItemCount[:]); err != nil { | |
555 | return err | |
556 | } | |
557 | fu.FileNamePath = make([]byte, binary.BigEndian.Uint16(fu.DataSize[:])-4) // -4 to subtract the path separator bytes TODO: wat | |
558 | if _, err := io.ReadFull(rwc, fu.FileNamePath); err != nil { | |
559 | return err | |
560 | } | |
561 | ||
562 | if fu.IsFolder == [2]byte{0, 1} { | |
563 | if _, err := os.Stat(filepath.Join(fullPath, fu.FormattedPath())); os.IsNotExist(err) { | |
564 | if err := os.Mkdir(filepath.Join(fullPath, fu.FormattedPath()), 0777); err != nil { | |
565 | return err | |
566 | } | |
567 | } | |
568 | ||
569 | // Tell client to send next file | |
570 | if _, err := rwc.Write([]byte{0, DlFldrActionNextFile}); err != nil { | |
571 | return err | |
572 | } | |
573 | } else { | |
574 | nextAction := DlFldrActionSendFile | |
575 | ||
576 | // Check if we have the full file already. If so, send dlFldrAction_NextFile to client to skip. | |
577 | _, err := os.Stat(filepath.Join(fullPath, fu.FormattedPath())) | |
578 | if err != nil && !errors.Is(err, fs.ErrNotExist) { | |
579 | return err | |
580 | } | |
581 | if err == nil { | |
582 | nextAction = DlFldrActionNextFile | |
583 | } | |
584 | ||
585 | // Check if we have a partial file already. If so, send dlFldrAction_ResumeFile to client to resume upload. | |
586 | incompleteFile, err := os.Stat(filepath.Join(fullPath, fu.FormattedPath()+IncompleteFileSuffix)) | |
587 | if err != nil && !errors.Is(err, fs.ErrNotExist) { | |
588 | return err | |
589 | } | |
590 | if err == nil { | |
591 | nextAction = DlFldrActionResumeFile | |
592 | } | |
593 | ||
594 | if _, err := rwc.Write([]byte{0, uint8(nextAction)}); err != nil { | |
595 | return err | |
596 | } | |
597 | ||
598 | switch nextAction { | |
599 | case DlFldrActionNextFile: | |
600 | continue | |
601 | case DlFldrActionResumeFile: | |
602 | offset := make([]byte, 4) | |
603 | binary.BigEndian.PutUint32(offset, uint32(incompleteFile.Size())) | |
604 | ||
605 | file, err := os.OpenFile(fullPath+"/"+fu.FormattedPath()+IncompleteFileSuffix, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | |
606 | if err != nil { | |
607 | return err | |
608 | } | |
609 | ||
610 | fileResumeData := NewFileResumeData([]ForkInfoList{*NewForkInfoList(offset)}) | |
611 | ||
612 | b, _ := fileResumeData.BinaryMarshal() | |
613 | ||
614 | bs := make([]byte, 2) | |
615 | binary.BigEndian.PutUint16(bs, uint16(len(b))) | |
616 | ||
617 | if _, err := rwc.Write(append(bs, b...)); err != nil { | |
618 | return err | |
619 | } | |
620 | ||
621 | if _, err := io.ReadFull(rwc, fileSize); err != nil { | |
622 | return err | |
623 | } | |
624 | ||
625 | if err := receiveFile(rwc, file, io.Discard, io.Discard, fileTransfer.bytesSentCounter); err != nil { | |
626 | rLogger.Error(err.Error()) | |
627 | } | |
628 | ||
629 | err = os.Rename(fullPath+"/"+fu.FormattedPath()+".incomplete", fullPath+"/"+fu.FormattedPath()) | |
630 | if err != nil { | |
631 | return err | |
632 | } | |
633 | ||
634 | case DlFldrActionSendFile: | |
635 | if _, err := io.ReadFull(rwc, fileSize); err != nil { | |
636 | return err | |
637 | } | |
638 | ||
639 | filePath := filepath.Join(fullPath, fu.FormattedPath()) | |
640 | ||
641 | hlFile, err := NewFileWrapper(fileStore, filePath, 0) | |
642 | if err != nil { | |
643 | return err | |
644 | } | |
645 | ||
646 | rLogger.Info("Starting file transfer", "path", filePath, "fileNum", i+1, "fileSize", binary.BigEndian.Uint32(fileSize)) | |
647 | ||
648 | incWriter, err := hlFile.incFileWriter() | |
649 | if err != nil { | |
650 | return err | |
651 | } | |
652 | ||
653 | rForkWriter := io.Discard | |
654 | iForkWriter := io.Discard | |
655 | if preserveForks { | |
656 | iForkWriter, err = hlFile.InfoForkWriter() | |
657 | if err != nil { | |
658 | return err | |
659 | } | |
660 | ||
661 | rForkWriter, err = hlFile.rsrcForkWriter() | |
662 | if err != nil { | |
663 | return err | |
664 | } | |
665 | } | |
666 | if err := receiveFile(rwc, incWriter, rForkWriter, iForkWriter, fileTransfer.bytesSentCounter); err != nil { | |
667 | return err | |
668 | } | |
669 | ||
670 | if err := os.Rename(filePath+".incomplete", filePath); err != nil { | |
671 | return err | |
672 | } | |
673 | } | |
674 | ||
675 | // Tell client to send next fileWrapper | |
676 | if _, err := rwc.Write([]byte{0, DlFldrActionNextFile}); err != nil { | |
677 | return err | |
678 | } | |
679 | } | |
680 | } | |
681 | rLogger.Info("Folder upload complete") | |
682 | return nil | |
683 | } |