Crate gday_file_transfer

Source
Expand description

This library lets you offer and transfer files to another peer, assuming you already have a reliable connection established.

§Example

The peers first establish a direct TCP connection using gday_hole_punch, and encrypt it with gday_encryption.

Peer A and peer B are on different computers in this example.

// Peer A offers files and folders they'd like to send
let paths_to_send = ["folder/to/send/".into(), "a/file.txt".into()];
let offer = create_file_offer(&paths_to_send)?;
write_to_async(&offer.offer, &mut stream1).await?;

// Peer B responds to the offer
let offer_msg: FileOfferMsg = read_from_async(&mut stream2).await?;
let requests_msg = FileRequestsMsg::accept_only_new_and_interrupted(
    &offer_msg,
    Path::new("save/the/files/here/"),
)?;
write_to_async(requests_msg, &mut stream2).await?;

// Peer A sends the accepted files
let requests_msg: FileRequestsMsg = read_from_async(&mut stream1).await?;
send_files(&offer, &requests_msg, &mut stream1, |progress| {}).await?;

// Peer B receives the accepted files
let save_path = Path::new("save/the/files/here/");
receive_files(
    &offer_msg,
    &requests_msg,
    save_path,
    &mut stream2,
    |progress| {},
)
.await?;

Structs§

FileMetadata
Information about an offered file.
FileOfferMsg
The sending peer sends this message to offer files, and the receiver replies with FileRequestsMsg.
FileRequestsMsg
The receiving peer replies with this message after getting a FileOfferMsg.
LocalFileOffer
The sending peer uses this struct to store its FileOfferMsg and a mapping from the shortened offered paths to the local on-disk file paths.
SingleFileRequest
A part of FileRequestsMsg
TmpInfoFile
Information about the file currently being downloaded. Saved in TMP_INFO_FILE as json before the download, and deleted after the download.
TransferReport
Holds the status of a file transfer

Enums§

Error
gday_file_transfer error.

Constants§

PROTOCOL_VERSION
Version of the protocol. Different numbers wound indicate incompatible protocol breaking changes.
TMP_DOWNLOAD_FILE
TMP_INFO_FILE

Functions§

already_exists
Returns true iff a file is already saved at get_last_occupied_version(path) with the same length as in metadata.
create_file_offer
Returns a LocalFileOffer referring to all the files and directories within paths.
delete_tmp_info_file
detect_interrupted_download
Checks if TMP_DOWNLOAD_FILE and TMP_INFO_FILE in download_dir indicate a file download was interrupted, and offer is re-offering that same file.
get_download_path
Joins save_dir and offered_path.
get_last_occupied_version
Returns the occupied path with the greatest numerical suffix.
get_unoccupied_version
Returns a version of path that isn’t occupied.
read_from
Reads a message from reader using serde_json.
read_from_async
Asynchronously reads a message from reader using serde_json.
read_tmp_info_file
receive_files
Receives the requested files from reader.
send_files
Transfers the requested files to writer.
write_tmp_info_file
write_to
Writes msg to writer using serde_json, and flushes.
write_to_async
Asynchronously writes msg to writer using serde_json, and flushes.