1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use super::*;

impl SwarmSSH {
    /// Create a download task, note that the execute command needs to be [`DownloadTask::activated`]
    ///
    /// # Arguments
    ///
    /// * `remote_path`:
    ///
    /// returns: Result<DownloadTask, QError>
    ///
    /// # Examples
    ///
    /// ```
    /// use swarm_ssh::SwarmSSH;
    /// ```
    pub fn download_task<P>(&self, remote_path: P) -> QResult<DownloadTask>
    where
        P: AsRef<Path>,
    {
        Ok(DownloadTask { target: remote_path.as_ref().to_path_buf(), session: &self.session })
    }
}

impl<'s> DownloadTask<'s> {
    /// Create a download task, note that the execute command needs to be [`DownloadTask::activated`]
    ///
    /// # Arguments
    ///
    /// * `remote_path`:
    ///
    /// returns: Result<DownloadTask, QError>
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use swarm_ssh::SwarmSSH;
    /// ```
    pub async fn execute(self) -> QResult<Vec<u8>> {
        // 下载文件
        let (mut scp, _) = self.session.scp_recv(&self.target)?;
        let mut buffer = Vec::new();
        scp.read_to_end(&mut buffer)?;
        // 关闭频道,等待全部内容传输完毕
        scp.send_eof()?;
        scp.wait_eof()?;
        scp.close()?;
        scp.wait_close()?;
        Ok(buffer)
    }
}