Skip to content
Snippets Groups Projects
Commit a802e2b8 authored by Jono Wenger's avatar Jono Wenger
Browse files

Fix file transfer partStore and minor integration test fixes

parent 45bf3665
Branches
Tags
1 merge request!117Release
......@@ -18,10 +18,8 @@ import (
"gitlab.com/elixxir/crypto/contact"
ftCrypto "gitlab.com/elixxir/crypto/fileTransfer"
"gitlab.com/xx_network/primitives/id"
"gitlab.com/xx_network/primitives/netTime"
"gitlab.com/xx_network/primitives/utils"
"io/ioutil"
"strconv"
"time"
)
......@@ -186,23 +184,29 @@ func sendFile(filePath, fileType, filePreviewPath, filePreviewString,
}
}
// Truncate file path if it is too long to be a file name
fileName := filePath
if len(fileName) > ft.FileNameMaxLen {
fileName = fileName[:ft.FileNameMaxLen]
}
// Get recipient contact from file
recipient := getContactFromFile(recipientContactPath)
jww.DEBUG.Printf("Sending file %q of size %d to recipient %s.",
filePath, len(fileData), recipient.ID)
fileName, len(fileData), recipient.ID)
// Create sent progress callback that prints the results
progressCB := func(completed bool, sent, arrived, total uint16,
t interfaces.FilePartTracker, err error) {
jww.DEBUG.Printf("Sent progress callback for %q "+
"{completed: %t, sent: %d, arrived: %d, total: %d, err: %v}\n",
filePath, completed, sent, arrived, total, err)
fileName, completed, sent, arrived, total, err)
if (sent == 0 && arrived == 0) || (arrived == total) || completed ||
err != nil {
fmt.Printf("Sent progress callback for %q "+
"{completed: %t, sent: %d, arrived: %d, total: %d, err: %v}\n",
filePath, completed, sent, arrived, total, err)
fileName, completed, sent, arrived, total, err)
}
if completed {
......@@ -215,11 +219,11 @@ func sendFile(filePath, fileType, filePreviewPath, filePreviewString,
}
// Send the file
_, err = m.Send(filePath, fileType, fileData, recipient.ID, retry,
_, err = m.Send(fileName, fileType, fileData, recipient.ID, retry,
filePreviewData, progressCB, callbackPeriod)
if err != nil {
jww.FATAL.Panicf("Failed to send file %q to %s: %+v",
filePath, recipient.ID, err)
fileName, recipient.ID, err)
}
}
......@@ -236,9 +240,9 @@ func receiveNewFileTransfers(receive chan receivedFtResults, done,
"message.")
return
case r := <-receive:
jww.DEBUG.Printf("Received new file %q transfer %s from %s of size %d "+
"bytes with preview: %q",
r.fileName, r.tid, r.sender, r.size, r.preview)
jww.DEBUG.Printf("Received new file %q transfer %s of type %q "+
"from %s of size %d bytes with preview: %q",
r.fileName, r.tid, r.fileType, r.sender, r.size, r.preview)
fmt.Printf("Received new file transfer %q of size %d "+
"bytes with preview: %q\n", r.fileName, r.size, r.preview)
......@@ -305,16 +309,16 @@ func getContactFromFile(path string) contact.Contact {
// init initializes commands and flags for Cobra.
func init() {
ftCmd.Flags().String("sendFile", "",
"Sends a file to a recipient with with the contact at this path.")
"Sends a file to a recipient with with the contact file at this path.")
bindPFlagCheckErr("sendFile")
ftCmd.Flags().String("filePath", "testFile-"+timeNanoString()+".txt",
ftCmd.Flags().String("filePath", "",
"The path to the file to send. Also used as the file name.")
bindPFlagCheckErr("filePath")
ftCmd.Flags().String("fileType", "txt",
"8-byte file type.")
bindPFlagCheckErr("filePath")
bindPFlagCheckErr("fileType")
ftCmd.Flags().String("filePreviewPath", "",
"The path to the file preview to send. Set either this flag or "+
......@@ -336,11 +340,6 @@ func init() {
rootCmd.AddCommand(ftCmd)
}
// timeNanoString returns the current UNIX time in nanoseconds as a string.
func timeNanoString() string {
return strconv.Itoa(int(netTime.Now().UnixNano()))
}
// bindPFlagCheckErr binds the key to a pflag.Flag used by Cobra and prints an
// error if one occurs.
func bindPFlagCheckErr(key string) {
......
......@@ -74,7 +74,8 @@ func (ps *partStore) addPart(part []byte, partNum uint16) error {
ps.mux.Lock()
defer ps.mux.Unlock()
ps.parts[partNum] = part
ps.parts[partNum] = make([]byte, len(part))
copy(ps.parts[partNum], part)
err := ps.savePart(partNum)
if err != nil {
......@@ -90,7 +91,10 @@ func (ps *partStore) getPart(partNum uint16) ([]byte, bool) {
defer ps.mux.Unlock()
part, exists := ps.parts[partNum]
return part, exists
newPart := make([]byte, len(part))
copy(newPart, part)
return newPart, exists
}
// getFile returns all file parts concatenated into a single file. Returns the
......@@ -268,7 +272,8 @@ func partSliceToMap(parts ...[]byte) map[uint16][]byte {
// Add each file part to the map
for partNum, part := range parts {
partMap[uint16(partNum)] = part
partMap[uint16(partNum)] = make([]byte, len(part))
copy(partMap[uint16(partNum)], part)
}
return partMap
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment