diff --git a/src/gopher.rs b/src/gopher.rs index f5be137..fbd58eb 100644 --- a/src/gopher.rs +++ b/src/gopher.rs @@ -112,7 +112,7 @@ fn clean_response(res: &mut String) { }) } -/// Downloads a binary to disk to a provided file name. +/// Downloads menu or text to disk as `filename`. /// Allows canceling with Ctrl-c, but it's /// kind of hacky - needs the UI receiver passed in. /// Returns a tuple of: @@ -134,7 +134,7 @@ pub fn download_url_with_filename( .create_new(true) .append(true) .open(&path) - .map_err(|e| error!("`open` error: {}", e))?; + .map_err(|e| error!("{}", e))?; let mut buf = [0; 1024]; let mut bytes = 0; diff --git a/src/ui.rs b/src/ui.rs index 8961d91..286d1f7 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -260,7 +260,6 @@ impl UI { }) } - /// Download a binary file. Used by `open()` internally. fn download(&mut self, url: &str) -> Result<()> { let url = url.to_string(); @@ -686,21 +685,24 @@ impl UI { 'b' => self.open("Bookmarks", "gopher://phetch/1/bookmarks")?, 'd' => { let url = match self.views.get(self.focused) { - Some(view)=> String::from(view.url()), - None => {return Err(error!("Could not get url from view"));}, + Some(view) => String::from(view.url()), + None => return Err(error!("Could not get URL from view")), }; + let url = url.as_str(); + if url.starts_with("gopher://phetch/") { + return Err(error!("Can't download internal phetch pages.")); + } + let u = gopher::parse_url(&url); - let default_filename = u - .sel - .split_terminator('/') - .rev() - .next() - .unwrap_or(""); - if let Some(filename) = self.prompt("Provide a filepath: ", default_filename){ - match self.download_file_with_filename(url.as_str(), String::from(filename)){ + let default_filename = u.sel.split_terminator('/').rev().next().unwrap_or(""); + if let Some(filename) = self.prompt("Save to disk as: ", default_filename) { + if filename.trim().is_empty() { + return Err(error!("Please provide a filename.")); + } + match self.download_file_with_filename(url, String::from(filename)) { Ok(()) => (), - Err(e) => return Err(error!("Save failed: {}", e)), + Err(e) => return Err(error!("Download failed: {}", e)), } } }