-func transferFile(conn net.Conn, dst string) error {
- const buffSize = 1024
- buf := make([]byte, buffSize)
-
- // Read first chunk of bytes from conn; this will be the Flat File Object and initial chunk of file bytes
- if _, err := conn.Read(buf); err != nil {
- return err
- }
- ffo := ReadFlattenedFileObject(buf)
- payloadLen := len(ffo.Payload())
- fileSize := int(binary.BigEndian.Uint32(ffo.FlatFileDataForkHeader.DataSize))
-
- newFile, err := os.Create(dst)
- if err != nil {
- return err
- }
- defer func() { _ = newFile.Close() }()
- if _, err := newFile.Write(buf[payloadLen:]); err != nil {
- return err
- }
- receivedBytes := buffSize - payloadLen
-
- for {
- if (fileSize - receivedBytes) < buffSize {
- _, err := io.CopyN(newFile, conn, int64(fileSize-receivedBytes))
- return err
- }
-
- // Copy N bytes from conn to upload file
- n, err := io.CopyN(newFile, conn, buffSize)
- if err != nil {
- return err
- }
- receivedBytes += int(n)
- }
-}
-
-// 00 28 // DataSize
-// 00 00 // IsFolder
-// 00 02 // PathItemCount
-//
-// 00 00
-// 09
-// 73 75 62 66 6f 6c 64 65 72 // "subfolder"
-//
-// 00 00
-// 15
-// 73 75 62 66 6f 6c 64 65 72 2d 74 65 73 74 66 69 6c 65 2d 35 6b // "subfolder-testfile-5k"
-func readFolderUpload(buf []byte) folderUpload {
- dataLen := binary.BigEndian.Uint16(buf[0:2])
-
- fu := folderUpload{
- DataSize: buf[0:2], // Size of this structure (not including data size element itself)
- IsFolder: buf[2:4],
- PathItemCount: buf[4:6],
- FileNamePath: buf[6 : dataLen+2],
- }
-
- return fu
-}
-
-type folderUpload struct {
- DataSize []byte
- IsFolder []byte
- PathItemCount []byte
- FileNamePath []byte
-}
-
-func (fu *folderUpload) FormattedPath() string {
- pathItemLen := binary.BigEndian.Uint16(fu.PathItemCount)
-
- var pathSegments []string
- pathData := fu.FileNamePath
-
- for i := uint16(0); i < pathItemLen; i++ {
- segLen := pathData[2]
- pathSegments = append(pathSegments, string(pathData[3:3+segLen]))
- pathData = pathData[3+segLen:]
- }
-
- return strings.Join(pathSegments, pathSeparator)
-}
-