better marshaling

This commit is contained in:
Zack Scholl 2018-09-25 18:38:19 -07:00
parent 7f0b919b0b
commit efa66f7cbe
2 changed files with 12 additions and 17 deletions

View File

@ -8,6 +8,7 @@ import (
"io/ioutil" "io/ioutil"
"net" "net"
"os" "os"
"strconv"
"strings" "strings"
"time" "time"
@ -223,13 +224,13 @@ func receive(forceSend int, serverAddress string, tcpPorts []string, isLocal boo
return err return err
} }
var bl models.BytesAndLocation // get location if TCP
err = json.Unmarshal(decrypted, &bl) var locationToWrite int
if err != nil { if !useWebsockets {
log.Error(err) pieces := bytes.SplitN(decrypted, []byte("-"), 2)
return err decrypted = pieces[1]
locationToWrite, _ = strconv.Atoi(string(pieces[0]))
} }
decrypted = bl.Bytes
// do decompression // do decompression
if fstats.IsCompressed && !fstats.IsDir { if fstats.IsCompressed && !fstats.IsDir {
@ -242,7 +243,7 @@ func receive(forceSend int, serverAddress string, tcpPorts []string, isLocal boo
log.Error(err) log.Error(err)
return err return err
} }
n, err = f.WriteAt(decrypted, bl.Location) n, err = f.WriteAt(decrypted, int64(locationToWrite))
} else { } else {
// write to file // write to file
n, err = f.Write(decrypted) n, err = f.Write(decrypted)

View File

@ -189,19 +189,13 @@ func send(forceSend int, serverAddress string, tcpPorts []string, isLocal bool,
compressedBytes = buffer[:bytesread] compressedBytes = buffer[:bytesread]
} }
// put number of byte read // if using TCP, prepend the location to write the data to in the resulting file
transferBytes, err := json.Marshal(models.BytesAndLocation{Bytes: compressedBytes, Location: currentPostition}) if !useWebsockets {
if err != nil { compressedBytes = append([]byte(fmt.Sprintf("%d-", currentPostition)), compressedBytes...)
dataChan <- DataChan{
b: nil,
bytesRead: 0,
err: err,
}
return
} }
// do encryption // do encryption
enc := crypt.Encrypt(transferBytes, sessionKey, !useEncryption) enc := crypt.Encrypt(compressedBytes, sessionKey, !useEncryption)
encBytes, err := json.Marshal(enc) encBytes, err := json.Marshal(enc)
if err != nil { if err != nil {
dataChan <- DataChan{ dataChan <- DataChan{