pub struct FileDownload { /* private fields */ }Expand description
A file download in progress, streamed from the device.
Wraps the active backend’s download body and tracks progress. Data is streamed directly from the device as chunks arrive, without buffering the entire file in memory.
§Important
On the USB backend the session is held while this download is active. You must either consume
the entire download or call cancel() before dropping it; cancelling drains the
pipe and frees the session.
§Example
use mtp_rs::mtp::MtpDevice;
use mtp_rs::{ByteRange, ObjectHandle};
use tokio::io::AsyncWriteExt;
let mut download = storage.download(handle, ByteRange::Full).await?;
println!("Downloading {} bytes...", download.size());
while let Some(chunk) = download.next_chunk().await {
let bytes = chunk?;
file.write_all(&bytes).await?;
println!("Progress: {:.1}%", download.progress() * 100.0);
}Implementations§
Source§impl FileDownload
impl FileDownload
Sourcepub fn size(&self) -> u64
pub fn size(&self) -> u64
Total file size in bytes (always the whole file, even for a ranged download).
Sourcepub fn bytes_received(&self) -> u64
pub fn bytes_received(&self) -> u64
Bytes received so far in this stream.
Sourcepub async fn cancel(&mut self, idle_timeout: Duration) -> Result<(), Error>
pub async fn cancel(&mut self, idle_timeout: Duration) -> Result<(), Error>
Cancel the in-progress download.
On the USB backend this uses the Still Image Class cancel mechanism to stop the transfer and
drain remaining data, leaving the session clean for the next operation. The idle_timeout
controls how long to wait during the pipe drain. If the download is already complete, this
is a no-op.
Sourcepub async fn next_chunk(&mut self) -> Option<Result<Bytes, Error>>
pub async fn next_chunk(&mut self) -> Option<Result<Bytes, Error>>
Get the next chunk of data. Returns None when the download is complete.