Downloading a file
A very useful feature of an FTP server is the ability to download files. So, it's time to add the command to do so.
First of all, we add the case in the handle_cmd()
method:
#[async] fn handle_cmd(mut self, cmd: Command) -> Result<Self> { match cmd { Command::Retr(file) => self = await!(self.retr(file))?, // … } }
Here is the start of the retr()
function:
use std::fs::File; use std::io::Read; use error::Error; #[async] fn retr(mut self, path: PathBuf) -> Result<Self> { if self.data_writer.is_some() { let path = self.cwd.join(path); let (new_self, res) = self.complete_path(path.clone()); self = new_self; if let Ok(path) = res { if path.is_file() { self = await!(self.send(Answer::new(ResultCode::DataConnectionAlreadyOpen, "Starting to send file...")))?; let mut file = File::open(path)?; let mut out = vec![]; file.read_to_end...